0% found this document useful (0 votes)
23 views

Assignment Operators in C

The document discusses assignment operators in C language. It explains the simple assignment operator and augmented assignment operators. It provides examples of using various assignment operators in C programs and the output obtained from executing those programs.

Uploaded by

michal hana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Assignment Operators in C

The document discusses assignment operators in C language. It explains the simple assignment operator and augmented assignment operators. It provides examples of using various assignment operators in C programs and the output obtained from executing those programs.

Uploaded by

michal hana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

6/16/24, 11:51 AM Assignment Operators in C

Assignment Operators in C
In C language, the assignment operator stores a certain value in an already declared
variable. A variable in C can be assigned the value in the form of a literal, another
variable, or an expression.

The value to be assigned forms the right-hand operand, whereas the variable to be
assigned should be the operand to the left of the "=" symbol, which is defined as a
simple assignment operator in C.

In addition, C has several augmented assignment operators.

The following table lists the assignment operators supported by the C language −

Operator Description Example

C = A + B will
Simple assignment operator. Assigns values
= assign the value of
from right side operands to left side operand
A + B to C

Add AND assignment operator. It adds the right


C += A is equivalent
+= operand to the left operand and assign the
to C = C + A
result to the left operand.

Subtract AND assignment operator. It subtracts


C -= A is equivalent
-= the right operand from the left operand and
to C = C - A
assigns the result to the left operand.

Multiply AND assignment operator. It multiplies


C *= A is equivalent
*= the right operand with the left operand and
to C = C * A
assigns the result to the left operand.

Divide AND assignment operator. It divides the


C /= A is equivalent
/= left operand with the right operand and assigns
to C = C / A
the result to the left operand.

Modulus AND assignment operator. It takes C %= A is


%= modulus using two operands and assigns the equivalent to C = C
result to the left operand. %A

C <<= 2 is same as
<<= Left shift AND assignment operator.
C = C << 2

https://www.tutorialspoint.com/cprogramming/c_assignment_operators.htm 1/6
6/16/24, 11:51 AM Assignment Operators in C

C >>= 2 is same as
>>= Right shift AND assignment operator.
C = C >> 2

C &= 2 is same as C
&= Bitwise AND assignment operator.
=C&2

C ^= 2 is same as C
^= Bitwise exclusive OR and assignment operator.
=C^2

C |= 2 is same as C
|= Bitwise inclusive OR and assignment operator.
=C|2

Simple Assignment Operator (=)


The = operator is one of the most frequently used operators in C. As per the ANSI C
standard, all the variables must be declared in the beginning. Variable declaration
after the first processing statement is not allowed.

You can declare a variable to be assigned a value later in the code, or you can
initialize it at the time of declaration.

You can use a literal, another variable, or an expression in the assignment


statement.

int x = 10; // declaration with initialization


int y; // declaration
y = 20; // assignment later
int z = x + y; // assign an expression
int d = 3, f = 5; // definition and initializing d and f.
char x = 'x'; // the variable x has the value 'x'.

Once a variable of a certain type is declared, it cannot be assigned a value of any


other type. In such a case the C compiler reports a type mismatch error.

In C, the expressions that refer to a memory location are called "lvalue" expressions.
A lvalue may appear as either the left-hand or right-hand side of an assignment.

On the other hand, the term rvalue refers to a data value that is stored at some
address in memory. A rvalue is an expression that cannot have a value assigned to it
which means an rvalue may appear on the right-hand side but not on the left-hand
side of an assignment.

Variables are lvalues and so they may appear on the left-hand side of an
assignment. Numeric literals are rvalues and so they may not be assigned and

https://www.tutorialspoint.com/cprogramming/c_assignment_operators.htm 2/6
6/16/24, 11:51 AM Assignment Operators in C

cannot appear on the left-hand side. Take a look at the following valid and invalid
statements −

int g = 20; // valid statement


10 = 20; // invalid statement

Augmented Assignment Operators


In addition to the = operator, C allows you to combine arithmetic and bitwise
operators with the = symbol to form augmented or compound assignment operator.
The augmented operators offer a convenient shortcut for combining arithmetic or
bitwise operation with assignment.

Example 1

For example, the expression "a += b" has the same effect of performing "a + b" first
and then assigning the result back to the variable "a".

#include <stdio.h>

int main(){

int a = 10;
int b = 20;

a += b;
printf("a: %d", a);

return 0;
}

Output

Run the code and check its output −

a: 30

Example 2

Similarly, the expression "a <<= b" has the same effect of performing "a << b" first
and then assigning the result back to the variable "a".

https://www.tutorialspoint.com/cprogramming/c_assignment_operators.htm 3/6
6/16/24, 11:51 AM Assignment Operators in C

#include <stdio.h>

int main(){

int a = 60;
int b = 2;

a <<= b;
printf("a: %d", a);

return 0;
}

Output

Run the code and check its output −

a: 240

Example 3

Here is a C program that demonstrates the use of assignment operators in C −

#include <stdio.h>

int main(){

int a = 21;
int c ;

c = a;
printf("Line 1 - = Operator Example, Value of c = %d\n", c );

c += a;
printf("Line 2 - += Operator Example, Value of c = %d\n", c );

c -= a;
printf("Line 3 - -= Operator Example, Value of c = %d\n", c );

c *= a;
printf("Line 4 - *= Operator Example, Value of c = %d\n", c );

https://www.tutorialspoint.com/cprogramming/c_assignment_operators.htm 4/6
6/16/24, 11:51 AM Assignment Operators in C

c /= a;
printf("Line 5 - /= Operator Example, Value of c = %d\n", c );

c = 200;
c %= a;
printf("Line 6 - %%= Operator Example, Value of c = %d\n", c );

c <<= 2;
printf("Line 7 - <<= Operator Example, Value of c = %d\n", c );

c >>= 2;
printf("Line 8 - >>= Operator Example, Value of c = %d\n", c );

c &= 2;
printf("Line 9 - &= Operator Example, Value of c = %d\n", c );

c ^= 2;
printf("Line 10 - ^= Operator Example, Value of c = %d\n", c );

c |= 2;
printf("Line 11 - |= Operator Example, Value of c = %d\n", c );

return 0;
}

Output

When you compile and execute the above program, it will produce the following
result −

Line 1 - = Operator Example, Value of c = 21


Line 2 - += Operator Example, Value of c = 42
Line 3 - -= Operator Example, Value of c = 21
Line 4 - *= Operator Example, Value of c = 441
Line 5 - /= Operator Example, Value of c = 21
Line 6 - %= Operator Example, Value of c = 11
Line 7 - <<= Operator Example, Value of c = 44
Line 8 - >>= Operator Example, Value of c = 11
Line 9 - &= Operator Example, Value of c = 2

https://www.tutorialspoint.com/cprogramming/c_assignment_operators.htm 5/6
6/16/24, 11:51 AM Assignment Operators in C

Line 10 - ^= Operator Example, Value of c = 0


Line 11 - |= Operator Example, Value of c = 2

https://www.tutorialspoint.com/cprogramming/c_assignment_operators.htm 6/6

You might also like