### Monday, December 7, 2015

# Tricky concepts of Operators in C Programming Language

We assume that you know the basics of C Programming.

1.

int c=10,a=5,d;

d=a=c;

After the execution of above statement d will have the value 10.

Explanation: Equality operator associates Right to Left, i.e. in the above statement first c is assigned to a.

The expression a=c evaluates to the value of a after the assignment takes place. Then this value is assigned to d.

2.

int x=2;

x*=3+2;

After the execution of above statement x will have the value 10.

Explanation: Right Expression is evaluated first & then the result is multiplied to the left variable. Also note that "+" operator has higher precedence over "*=" operator.

3.

x*=y=z=4;

if x was initially 10, then after execution of the above statement x=40, y=4 and z=4

4.

x=y==z;

if y & z have same values, (i.e. if y=4, and z=4), then y==z will return the value 1, which will be stored in x. If y is not equal to z, (i.e. if y=5, and z=4), then y==z will return the value 0 which will be stored in x.

5.

x=1; z=0;

z=x++ - 1;

The above statement will make value of z=0 & x=2.

Explanation: When increment operator (i.e. ++) is used as a postfix then the statement is evaluated first & then the variable will be incremented. When increment operator (i.e. ++) is used as a prefix then the variable will be incremented first & then the statement will be evaluated.

6.

int i=3,j;

j=++i * ++i * ++i;

When executed we will get i=6 and j=216.

Explanation: The value of

7.

int i=3,j;

j=i++ * i++ * i++;

When executed we will get i=6 and j=27.

Explanation: The value of

1.

int c=10,a=5,d;

d=a=c;

After the execution of above statement d will have the value 10.

Explanation: Equality operator associates Right to Left, i.e. in the above statement first c is assigned to a.

The expression a=c evaluates to the value of a after the assignment takes place. Then this value is assigned to d.

2.

int x=2;

x*=3+2;

After the execution of above statement x will have the value 10.

Explanation: Right Expression is evaluated first & then the result is multiplied to the left variable. Also note that "+" operator has higher precedence over "*=" operator.

3.

x*=y=z=4;

if x was initially 10, then after execution of the above statement x=40, y=4 and z=4

4.

x=y==z;

if y & z have same values, (i.e. if y=4, and z=4), then y==z will return the value 1, which will be stored in x. If y is not equal to z, (i.e. if y=5, and z=4), then y==z will return the value 0 which will be stored in x.

5.

x=1; z=0;

z=x++ - 1;

The above statement will make value of z=0 & x=2.

Explanation: When increment operator (i.e. ++) is used as a postfix then the statement is evaluated first & then the variable will be incremented. When increment operator (i.e. ++) is used as a prefix then the variable will be incremented first & then the statement will be evaluated.

6.

int i=3,j;

j=++i * ++i * ++i;

When executed we will get i=6 and j=216.

Explanation: The value of

**i**is used after it is incremented thrice. Therefore j=6*6*6=216.7.

int i=3,j;

j=i++ * i++ * i++;

When executed we will get i=6 and j=27.

Explanation: The value of

**i**is incremented only after the execution of the statement is over. Therefore j=3*3*3=27.
8.

int x=8;

x-=--x - x--;

After execution x will be 6.

Hint: Evaluate right side first.

9.

x=3; y=2; z=1;

printf("%d",x<y ? x++ : y++);

We know that since condition is false y++ will be evaluated. On execution, the above statement will print 2. Only after printing the value of y will be incremented.

10.

x=3; y=3; z=1;

printf("%d",z+=x<y ? x++ : y++);

On execution, the above statement will print 4. Only after printing the value of y will be incremented.

11.

i = (j++, k++);

Here, the comma operator is used to execute three expressions in one line: assign k to i, increment j, and increment k. The value that i receives is always the rightmost expression. if i=0, j=10, k=20 initially, after execution: i=20, j=11, k=21;

11.

i = (j++, k++);

Here, the comma operator is used to execute three expressions in one line: assign k to i, increment j, and increment k. The value that i receives is always the rightmost expression. if i=0, j=10, k=20 initially, after execution: i=20, j=11, k=21;

Subscribe to:
Post Comments (Atom)

## No comments:

## Post a Comment