Monday, 4 March 2013

Assignment Operators



4. Assignment Operators

The Assignment Operator evaluates an expression on the right of the expression and substitutes it to the value or variable on the left of the expression.

Example

x = a + b

Here the value of a + b is evaluated and substituted to the variable x.

In addition, C has a set of shorthand assignment operators of the form.

var oper = exp;

Here var is a variable, exp is an expression and oper is a C binary arithmetic operator. The operator oper = is known as shorthand assignment operator
Example

x + = 1 is same as x = x + 1

The commonly used shorthand assignment operators are as follows

Shorthand assignment operators  .



Example for using shorthand assignment operator

.
#define N 100 //creates a variable N with constant value 100
#define A 2 //creates a variable A with constant value 2

main() //start of the program
{
int a; //variable a declaration
a = A; //assigns value 2 to a

while (a < N) //while value of a is less than N
{ //evaluate or do the following
printf(“%d \n”,a); //print the current value of a
a *= a; //shorthand form of a = a * a
} //end of the loop
} //end of the program
.

Output

2
4
16
 

No comments:

Post a Comment