What is the output of the following C program? main() { int a=10; int b; b=a++ + 20; printf("a=%d b=%d",a,b); }
Reveal answer
Fill a bubble to check yourself
What is the output of the following C program? main() { int a=10; int b; b=a++ + 20; printf("a=%d b=%d",a,b); }
a=11 b=31
a=10 b=30
a=11 b=30
None of the Above
The post-increment operator a++ uses the current value of a (10) in the expression, then increments a to 11. So b = 10 + 20 = 30, and after the expression a becomes 11. The printf outputs a=11 b=30. Option B is wrong because a does change. Option A is wrong because b is 30, not 31.
To determine the output of the given C program, let's go through the code step by step:
a with a value of 10.b.a (10) to b and then increment the value of a by 1 using the post-increment operator (a++).
a becomes 11 and b becomes 10.a and b using printf() function with the format string "a=%d b=%d".
a and b are 11 and 10 respectively.Therefore, the correct answer is:
C. a=11 b=30