Assignment operator in C programming

Assignment Operator in C

Assignment Operator in C

The assignment operator in C is used to assign values to variables. It is one of the most basic and commonly used operators in C programming. The assignment operator is represented by the equal sign (=).

Basic Syntax

The syntax for using the assignment operator is as follows:

variable = value;

Here, variable is the name of the variable you want to assign a value to, and value is the value you want to assign.

Examples

Here are some examples demonstrating how to use the assignment operator:

Example 1: Basic Assignment

int a;    // Declare an integer variable
a = 10;    // Assign the value 10 to the variable a

In this example, the integer variable a is declared and then assigned the value 10.

Example 2: Assignment with Initialization

int b = 20;   // Declare and initialize an integer variable

In this example, the integer variable b is both declared and initialized to 20 in a single statement.

Example 3: Using the Assignment Operator with Expressions

int x = 5;
int y;
y = x + 10;   // Assign the result of x + 10 to y

Here, the result of the expression x + 10 is assigned to the variable y. The variable x is first assigned the value 5, and then y is assigned the value of 15.

Note: The assignment operator can be combined with other operators to perform operations and assignments in a single step. For example, += can be used to add and assign simultaneously.

Comments

Popular Posts