Discussion
What is the output of this C code?
int main()
{
int var = 010;
printf("%d", var);
}
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.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development