Question 2770 – TCP/IP-Layers
November 19, 2023Question 7811 – Computer-Networks
November 19, 2023Question 11430 – Operator
What is the output of the following C program
#include int main() { int i=5; printf("%d%d%d", i++, i ,++i); return 0; }
Correct Answer: C
Question 2 Explanation:
int i=5;
printf(“%d%d%d”, i++, i ,++i);
In printf() the firstly the calculation is done from right to left and then printing is done from left to right.
So first ++i will increase the value of i by 1 and make it 6. Now the i will be read simply for second calculation.And now i++ will be calculated which will increment the value of i by 1 and make it 7, but since it is post increment so first 6 will be printed and then increment will be done and then for rest two 7 and 7 will be printed .Hence the output will be 6,7,7.
printf(“%d%d%d”, i++, i ,++i);
In printf() the firstly the calculation is done from right to left and then printing is done from left to right.
So first ++i will increase the value of i by 1 and make it 6. Now the i will be read simply for second calculation.And now i++ will be calculated which will increment the value of i by 1 and make it 7, but since it is post increment so first 6 will be printed and then increment will be done and then for rest two 7 and 7 will be printed .Hence the output will be 6,7,7.
7 6 6
6 7 8
6 7 7
5 6 7
Subscribe
Login
0 Comments