Multiple choice technology programming languages

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); }

  1. a=11 b=31

  2. a=10 b=30

  3. a=11 b=30

  4. None of the Above

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

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.

AI explanation

To determine the output of the given C program, let's go through the code step by step:

  1. Initialize the variable a with a value of 10.
  2. Declare the variable b.
  3. Assign the value of a (10) to b and then increment the value of a by 1 using the post-increment operator (a++).
    • After this step, a becomes 11 and b becomes 10.
  4. Print the values of a and b using printf() function with the format string "a=%d b=%d".
    • The values of a and b are 11 and 10 respectively.

Therefore, the correct answer is:

C. a=11 b=30