Pages

Saturday, February 13, 2010

C Operators ASSIGNMENT OPERATOR

Introduction
The assignment operator is used for assigning the value of an expression to a variable. The general format for an assignment operator is var = expression.

You can use other formats such as var += expression, which means var = var + expression.

Program
#include

main( )
{
int a,b,c,d;
printf("ENTER VALUES OF a,b, c, d");
scanf("%d%d%d",&a,&b,&c);
a += b*c+d;
printf("\n a = %d",a);
}

Input
a = 5, b= 5, c = 7, d = 8.
Output
ENTER VALUES OF a,b, c, d
5
5
7
8
a = 48

Explanation
The assignment operators have the lowest priority and they are evaluated from right to left. The assignment operators are as follows:

=, +=, -=, *=, /=, %=.

Suppose the expression is

a = 5;
a += 5*7+8;

You will get the value 48. It is evaluated by the following steps:

5*7 = 35.

35+8 = 43.

a += 43 means a = a + 43 which gives the value 48.

You can assign a value to multiple variables in one statement as:
i = j = k = 10 which gives value 10 to i, j, k.

No comments:

Post a Comment