Home Contact About

Assignment Operator

The assignment operator (=) in C is used to assign a value to a variable. It takes the value on the right-hand side and assigns it to the variable on the
left-hand side. The syntax for the assignment operator is:
variable = value;

For example, int x;
x = 10;

This will assign the value 10 to x.

The assignment operator can also be combined with arithmetic operators to perform arithmetic operations and assign the result to a variable. For example,
the following code adds 5 to the variable 'x' and assigns the result to 'y':
int x=10;
int y;
y = x+5;

The assignment operator can also be used to assign the value of one variable to another variable. For example, the following code assigns the value of 'x' to 'y':
int x = 10;
int y;
y = x;

In C, the assignment operator has a lower precedence than arithmetic operators, so expressions with both arithmetic and assignment operators are evaluated from
right to left. For example, the following code first multiplies 5 by 2 and then adds the result to 'x':
int x = 10;
x = 5*2+x;

It is important to note that the assignment operator only assigns the value on the right side to the variable on the left side. It does not compare or test for
equality. To test for equality, the relational operator (==) should be used instead. For example, the following code tests whether 'x' is equal to 10:
int x = 10;
if(x==10){
//do something
}

In C, the assignment operator can also be combined with other operators to form compound assignment operators. Compound assignment operators perform an arithmetic
operation and assign the result to the variable on the left side in a single statement. The syntax for compound assignment operators is:
variable operator = value;

For example, the following code adds 5 to 'x' and assigns the result to 'x':
int x=10;
x+ = 5;

The compound assignment operators in C include:

Understanding the assignment operator and its various uses is important for programming
in C, as it is one of the fundamental operators in the language.