What will be output of the following program?
#include<stdio.h>
int main(){
int i=5,j;
j=++i+++i+++i;
printf("%d %d",i,j);
return 0;
}
Explanation
Output:
Turbo C++ 3.0: 8 24
Turbo C ++4.5: Compilation error
Linux GCC: Compilation
error
Visual C++: Compilation
error
Explanation:
Rule :- ++ is pre increment operator so in any arithmetic expression it first increment the value of variable by one in whole expression then starts assigning the final value of variable in the expression.
Compiler will treat this expression j = ++i+++i+++i; as
i = ++i + ++i + ++i;
Initial value of i = 5 due to three pre increment operator
final value of i=8.
Now final value of i i.e. 8 will assigned to each variable
as shown in the following figure:
So, j=8+8+8
j=24 and
i=8
No comments:
Post a Comment