Machine-translated by Google. Questions are authored in English.
Top

Discussion

What is the output of this C code?

int main()
{
int var = 010;
printf("%d", var);
}

 

  • A.2
  • B.8
  • C.9
  • D.10

Answer: B

Correct answer: 8

A numeric literal beginning with 0 is octal in C, so 010 is 8 in decimal, not 10.

\[ 010_8 = 1 \times 8^1 + 0 \times 8^0 = 8 \]

This is a well-known source of bugs, particularly where numbers are padded to a fixed width - a date field written as 08 or 09 is not even valid octal and fails to compile, while 010 compiles and silently means 8. Hexadecimal requires the explicit 0x prefix, but octal needs only the leading zero.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.