What will be output of the following program?
#include<stdio.h> 
int main(){
    int i=1;
    i=2+2*i++;
    printf("%d",i);
    return 0;
}
Explanation
Output: 
Turbo C++ 3.0: 5
Turbo C ++4.5: 5
Linux GCC: 5
Visual C++: 5
Explanation:
i++ i.e. when postfix increment operator is used any expression the it first assign the its value in the expression the it increments the value of variable by one. So,
i++ i.e. when postfix increment operator is used any expression the it first assign the its value in the expression the it increments the value of variable by one. So,
i = 2 + 2 * 1 
i = 4 
Now i will be incremented by one so i = 4 + 1 = 5
 
No comments:
Post a Comment