May 25, 2018

Assignment Operator in C


   In C language, the assignment operator is responsible for assigning values to the variables. 


They are of two types:

  • Simple assignment operator
  • Compound assignment operators

 
Types of assignment operators
Types of assignment operators




Simple Assignment Operator

The simple assignment operator (=) causes the value of the right hand side operand to be stored in the left hand side operand. The operand on the left hand side should be a variable, while the operand on the right hand side can be any variable, constant or expression. Here are some examples of assignment expressions:

x = 7              /* 7 is assigned to x */
y = 3              /* 3 is assigned to y */
z = x + y + 10     /* Value of expression x+y+10 is assigned to z */
x = y              /* Value of y is assigned to x */
y = x              /* Value of x is assigned to y */
x = z              /* Value of z is assigned to x */


The value that is being assigned is considered as value of the assignment expression. For example x=11 is an assignment expression whose value is 11.

We can have multiple assignment expressions also, for example:

x = y = z = 15

Here all the three variables x, y, z will be assigned value 15, and the value of the whole expression will be 15.


Note: If we put a semicolon(;) after the assignment expression then it becomes an assignment statement. For example these are assignment statements:

x = 7;
y = 3;
z = x + y + 10;
x = y;
y = x;
x = y = z = 15;



The following program demonstrates Simple assignment operator.

#include <stdio.h>

int main(void)
{
  
    int x,y,z,k;
  
    x=10;
    y=x;
    z=x+y;
    k=z-10;
  
    printf("Value of x = %d \n", x);
    printf("Value of y = %d \n", y);
    printf("Value of z = %d \n", z);
    printf("Value of k = %d \n", k);
  
    return 0;
}

Output:

Value of x = 10
Value of y = 10
Value of z = 20
Value of k = 10



Compound Assignment Operators

Compound-assignment operators provide a shorter syntax for assigning the result of an arithmetic or bitwise operator. They perform the operation on the two operands before assigning the result to the first operand. For example:

x = x + 5 can also be written as:
x += 5

Here += is a compound assignment operator.

Similarly we have other compound assignment operators:


Arithmetic compound assignment operators

Operator Symbol Description
Addition += x += 5 is equivalent to x = x + 5
Subtraction -= y -= 5 is equivalent to y = y - 5
Multiplication *= z *= 5 is equivalent to z = z *5
Division /= a /= 5 is equivalent to a = a / 5
Modulus %= b %= 5 is equivalent to b = b % 5


The following program demonstrates Arithmetic compound assignment operator.

#include <stdio.h>

int main(void)
{
  
    int x=10,y=4;
     
    x += y;
    printf("+= Operator Example, Value of x = %d \n", x);

    x -= y;
    printf("
-= Operator Example, Value of x = %d \n", x);
    x *= y;
    printf(
"*= Operator Example, Value of x = %d \n", x);
    x /= y;
    printf(
"/= Operator Example, Value of x = %d \n", x);
 
   x %= y;
    printf(
"%%= Operator Example, Value of x = %d \n", x);
       
    return 0;
}

Output:

+= Operator Example, Value of x = 14
-= Operator Example, Value of x = 10
*= Operator Example, Value of x = 40
/= Operator Example, Value of x = 10
%= Operator Example, Value of x = 2



Bitwise compound assignment operators 

Operator Symbol Description
Left Shift <<= c <<= 2 is equivalent to c = c <<2
Right Shift >>= c >>= 2 is equivalent to c = c >>2
Bitwise AND &= c &= 2 is equivalent to c = c & 2
Bitwise XOR ^= c ^= 2 is equivalent to c = c ^ 2
Bitwise OR |= c |= 2 is equivalent to c = c | 2


The following program demonstrates Bitwise compound assignment operator.

#include <stdio.h>

int main(void)
{
  
    int x=10;
     
    x >>= 2;
    printf("
>>= Operator Example, Value of x = %d \n", x);
       
    return 0;
}

Output:

>>= Operator Example, Value of x = 2


next    Operator Precedence and Associativity in C
top    Index
prev    Bitwise Operators and sizeof() Operator in C







No comments:

Post a Comment