Saturday 13 April 2013

Operator's tricky program 12


#include<stdio.h>
int main(){
     int i=5;
     int a=++i + ++i + ++i;
     printf("%d",a);
     return 0;
}

Explanation
Output: 
Turbo C++ 3.0: 21

Turbo C ++4.5: 21

Linux GCC: 22

Visual C++: 24


Explanation:
Rule : ++ (in ++i) is pre increment operator so in any arithmetic expression it first increment the value of variable by one in whole equation up to break point then start assigning the value of variable in the equation. There are many break point operators in. For example:

(1) Declaration statement.
(2) && or operator.
(3) Comma (,) operator etc.

In the following expression:
int a=++i + ++i + ++i;

Here break point is due to declaration .It break after each increment i.e. (initial value of i=5) after first increment value 6 assign to variable i then in next increment will occur and so on.
So, a = 6 + 7 + 8;

No comments:

Post a Comment