C - Assignment Operators

<<Previous

Next >>





Assignment Operator

Assignment Operator is used to assign a value to the variable. This is called simple assignment operator. The arithmetic or any other operators would not be useful without an assignment operator. The assignment operator takes value on the right hand side and puts it into the variable on left hand side.

Syntax

		variable-name = value;

Examples

int x=10;
float y=20;
String s="hello";
char gender='M';
int a;
a=20;

Compund Assignment Operators

Sometimes, the left hand side variable is repeated immediately on the right side of the expression. For example, x = x + y. These type of expressions can also be written in compressed form, that is x += y. This kind of (+=) operator is called a compound assignment operator. Compound assignment operators are used as shown below:

Operators  DescriptionExample  Meaning  
+=Addition and Assignment operators. It adds the value of A and B and assigns the result to A.x += y x = x + y
- =Subtraction and Assignment operators. It subtracts the value of B from A and assigns the result to A. x -= y x = x - y
* =Multiplication and Assignment operators. It multiplies the value of A and B and assigns the result to A. x *= yx = x * y
/=Division and Assignment operators. It divides the value of A by B and assigns the result to A. x /= y x = x / y
%=Modulus and Assignment operators. It divides the value of A by B and stores the remainter to A.x %= y x = x%y
&=Bitwise AND and Assignment operators. It performs the bitwise AND with A and B and assigns the result to A. x &= y x = x & y
|=Bitwise OR and Assignment operators. It performs the bitwise OR with A and B and assigns the result to A.x |= y x = x | y
^=Bitwise XOR and Assignment operators. It performs the bitwise XOR with A and B and assigns the result to A. x ^= y x = x ^ y
>>=Bitwise Right Shift and Assignment operators. It performs the bitwise right shift with A and assigns the result to A. x >>= y x = x >> y
<<=Bitwise Left Shift and Assignment operators. It performs the bitwise left shift with A and assigns the result to A. x <<= y x = x << y
Operators  DescriptionExample  Meaning  
+=Addition and Assignment operators. It adds the value of A and B and assigns the result to A.x += y x = x + y
- =Subtraction and Assignment operators. It subtracts the value of B from A and assigns the result to A. x -= y x = x - y
* =Multiplication and Assignment operators. It multiplies the value of A and B and assigns the result to A. x *= yx = x * y
/=Division and Assignment operators. It divides the value of A by B and assigns the result to A. x /= y x = x / y
%=Modulus and Assignment operators. It divides the value of A by B and stores the remainter to A.x %= y x = x%y
&=Bitwise AND and Assignment operators. It performs the bitwise AND with A and B and assigns the result to A. x &= y x = x & y
|=Bitwise OR and Assignment operators. It performs the bitwise OR with A and B and assigns the result to A.x |= y x = x | y
^=Bitwise XOR and Assignment operators. It performs the bitwise XOR with A and B and assigns the result to A. x ^= y x = x ^ y
>>=Bitwise Right Shift and Assignment operators. It performs the bitwise right shift with A and assigns the result to A. x >>= y x = x >> y
<<=Bitwise Left Shift and Assignment operators. It performs the bitwise left shift with A and assigns the result to A. x <<= y x = x << y

Assigning Same value to Multiple Variables

There is also possible to assign a single value to multiple variables. This kind of assignment is called nested assignment or multiple assignment.

Syntax

    variable 1 = variable 2 = variable 3.......variable N = single value;

Examples

   x = y = z = 10;
   a = b = k =(x+y+z);

Complete Example

Following is an example C Program using Assignment Operators.

#include <stdio.h>

int main()
{
   /* Simple Assignment*/
   int a,k;
   int b,j;
   float c=30.0;
   float d=5.0;

   /*Nested or Multiple Assignment */
   a = k = 5;
   b = j = 3;

   /*Compound Assignment*/
   a += b;
   printf("After Add and Assign :%d \n",a);

   k -= j;
   printf("After Subtract and Assign :%d \n",k);

   a *= b;
   printf("After Multiple and Assign :%d \n",a);

   c /= d;
   printf("After Divide and Assign :%f \n",c);

   j %= k;
   printf("After Modulo and Assign :%d \n",j);

   j &= k;
   printf("After Bitwise And and Assign :%d \n",j);

   j |= k;
   printf("After Bitwise OR and Assign :%d \n",j);

   a ^= b;
   printf("After Bitwise XOR and Assign :%d \n",a);

   a <<= 2;
   printf ("After Bitwise Left Shift and Assign :%d \n",a);

   a >>= 3;
   printf ("After Bitwise Right Shift and Assign :%d \n",a);
    return(0);
 }
 

Output

After Add and Assign :8
After Subtract and Assign :2
After Multiple and Assign :24
After Divide and Assign :6.000000
After Modulo and Assign :1
After Bitwise And and Assign :0
After Bitwise OR and Assign :2
After Bitwise XOR and Assign :27
After Bitwise Left Shift and Assign :108
After Bitwise Right Shift and Assign :13

<< Previous

Next >>







C - Assignment Operators

<<Previous

Next >>





Assignment Operator

Assignment Operator is used to assign a value to the variable. This is called simple assignment operator. The arithmetic or any other operators would not be useful without an assignment operator. The assignment operator takes value on the right hand side and puts it into the variable on left hand side.

Syntax

		variable-name = value;

Examples

int x=10;
float y=20;
String s="hello";
char gender='M';
int a;
a=20;

Compund Assignment Operators

Sometimes, the left hand side variable is repeated immediately on the right side of the expression. For example, x = x + y. These type of expressions can also be written in compressed form, that is x += y. This kind of (+=) operator is called a compound assignment operator. Compound assignment operators are used as shown below:

Operators  DescriptionExample  Meaning  
+=Addition and Assignment operators. It adds the value of A and B and assigns the result to A.x += y x = x + y
- =Subtraction and Assignment operators. It subtracts the value of B from A and assigns the result to A. x -= y x = x - y
* =Multiplication and Assignment operators. It multiplies the value of A and B and assigns the result to A. x *= yx = x * y
/=Division and Assignment operators. It divides the value of A by B and assigns the result to A. x /= y x = x / y
%=Modulus and Assignment operators. It divides the value of A by B and stores the remainter to A.x %= y x = x%y
&=Bitwise AND and Assignment operators. It performs the bitwise AND with A and B and assigns the result to A. x &= y x = x & y
|=Bitwise OR and Assignment operators. It performs the bitwise OR with A and B and assigns the result to A.x |= y x = x | y
^=Bitwise XOR and Assignment operators. It performs the bitwise XOR with A and B and assigns the result to A. x ^= y x = x ^ y
>>=Bitwise Right Shift and Assignment operators. It performs the bitwise right shift with A and assigns the result to A. x >>= y x = x >> y
<<=Bitwise Left Shift and Assignment operators. It performs the bitwise left shift with A and assigns the result to A. x <<= y x = x << y
Operators  DescriptionExample  Meaning  
+=Addition and Assignment operators. It adds the value of A and B and assigns the result to A.x += y x = x + y
- =Subtraction and Assignment operators. It subtracts the value of B from A and assigns the result to A. x -= y x = x - y
* =Multiplication and Assignment operators. It multiplies the value of A and B and assigns the result to A. x *= yx = x * y
/=Division and Assignment operators. It divides the value of A by B and assigns the result to A. x /= y x = x / y
%=Modulus and Assignment operators. It divides the value of A by B and stores the remainter to A.x %= y x = x%y
&=Bitwise AND and Assignment operators. It performs the bitwise AND with A and B and assigns the result to A. x &= y x = x & y
|=Bitwise OR and Assignment operators. It performs the bitwise OR with A and B and assigns the result to A.x |= y x = x | y
^=Bitwise XOR and Assignment operators. It performs the bitwise XOR with A and B and assigns the result to A. x ^= y x = x ^ y
>>=Bitwise Right Shift and Assignment operators. It performs the bitwise right shift with A and assigns the result to A. x >>= y x = x >> y
<<=Bitwise Left Shift and Assignment operators. It performs the bitwise left shift with A and assigns the result to A. x <<= y x = x << y

Assigning Same value to Multiple Variables

There is also possible to assign a single value to multiple variables. This kind of assignment is called nested assignment or multiple assignment.

Syntax

    variable 1 = variable 2 = variable 3.......variable N = single value;

Examples

   x = y = z = 10;
   a = b = k =(x+y+z);

Complete Example

Following is an example C Program using Assignment Operators.

#include <stdio.h>

int main()
{
   /* Simple Assignment*/
   int a,k;
   int b,j;
   float c=30.0;
   float d=5.0;

   /*Nested or Multiple Assignment */
   a = k = 5;
   b = j = 3;

   /*Compound Assignment*/
   a += b;
   printf("After Add and Assign :%d \n",a);

   k -= j;
   printf("After Subtract and Assign :%d \n",k);

   a *= b;
   printf("After Multiple and Assign :%d \n",a);

   c /= d;
   printf("After Divide and Assign :%f \n",c);

   j %= k;
   printf("After Modulo and Assign :%d \n",j);

   j &= k;
   printf("After Bitwise And and Assign :%d \n",j);

   j |= k;
   printf("After Bitwise OR and Assign :%d \n",j);

   a ^= b;
   printf("After Bitwise XOR and Assign :%d \n",a);

   a <<= 2;
   printf ("After Bitwise Left Shift and Assign :%d \n",a);

   a >>= 3;
   printf ("After Bitwise Right Shift and Assign :%d \n",a);
    return(0);
 }
 

Output

After Add and Assign :8
After Subtract and Assign :2
After Multiple and Assign :24
After Divide and Assign :6.000000
After Modulo and Assign :1
After Bitwise And and Assign :0
After Bitwise OR and Assign :2
After Bitwise XOR and Assign :27
After Bitwise Left Shift and Assign :108
After Bitwise Right Shift and Assign :13

<< Previous

Next >>







Learn about Hashing and Hash table

Learn Expression Tree

OS Kernel Architecture

OS Arcitecture of Windows, Linux and MacOS

Learn OS scheduling Algorithms