What will be the output of the following code? #include <stdio.h> int main() {…

2023

What will be the output of the following code?

#include <stdio.h>

int main()

{

int a, b, c;

a = 0x10;

b = 010;

c = a + b;

printf("%d", c);

return 0;

}

  1. A.

    20

  2. B.

    24

  3. C.

    Garbage

  4. D.

    Error

Attempted by 1124 students.

Show answer & explanation

Correct answer: B

Final output: 24

Explanation:

  • 0x10 is a hexadecimal literal. In decimal, 0x10 = 16.

  • 010 is an octal literal because it starts with a leading 0. In decimal, 010 (octal) = 8.

  • Add the values: 16 + 8 = 24.

  • printf("%d", c) prints the decimal value of c, so the program outputs 24.

Note: The code is valid C; there is no runtime garbage or compilation error in this example.

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Mppsc Assistant Professor