All Exams Test series for 1 year @ ₹349 only
Question

What is the output of the following code written in C?

void main()

{

printf("%d%d%d",62,062,0x62);

}

The correct answer is

625098

Understanding C Program Output with Number Literals

The provided C code snippet demonstrates how different types of integer literals (decimal, octal, and hexadecimal) are interpreted and printed using the printf function with the %d format specifier.

Let's break down the printf statement: printf("%d%d%d", 62, 062, 0x62);

The %d format specifier is used to print an integer value in its decimal representation.

The arguments provided to printf are:

  • 62: This is a standard decimal integer literal. Its value is simply 62.
  • 062: This literal starts with a 0, indicating it is an octal (base-8) integer literal. To find its decimal value, we convert from base 8 to base 10. $$ 062_8 = 0 \times 8^2 + 6 \times 8^1 + 2 \times 8^0 $$ $$ = 0 \times 64 + 6 \times 8 + 2 \times 1 $$ $$ = 0 + 48 + 2 = 50_{10} $$ So, the value of 062 is 50 in decimal.
  • 0x62: This literal starts with 0x, indicating it is a hexadecimal (base-16) integer literal. To find its decimal value, we convert from base 16 to base 10. $$ 0x62_{16} = 6 \times 16^1 + 2 \times 16^0 $$ $$ = 6 \times 16 + 2 \times 1 $$ $$ = 96 + 2 = 98_{10} $$ So, the value of 0x62 is 98 in decimal.

The printf function will print these decimal values consecutively without any separators as specified by the format string "%d%d%d".

The values to be printed are:

  • First %d prints the decimal value of 62, which is 62.
  • Second %d prints the decimal value of 062, which is 50.
  • Third %d prints the decimal value of 0x62, which is 98.

Combining these outputs in order gives the final result: 62 followed by 50 followed by 98.

Output string: "62" + "50" + "98" = "625098"

Number Literal Conversions Summary

Literal Type Decimal Value
62 Decimal 62
062 Octal 50
0x62 Hexadecimal 98

Therefore, the output of the code is 625098.

Revision Table: C Integer Literals and printf

Literal Format Base Prefix printf Format Specifier for Decimal Output
Decimal 10 None %d
Octal 8 0 %d (interprets octal literal, prints decimal value) or %o (prints octal value)
Hexadecimal 16 0x or 0X %d (interprets hex literal, prints decimal value) or %x/%X (prints hex value)

Additional Information on C Number Systems

C supports different number literal formats to allow programmers to represent values conveniently based on context. Understanding these formats is crucial for correctly interpreting code.

  • Decimal: The most common format. Digits 0-9. No prefix required.
  • Octal: Used less frequently in modern code, but important to recognize. Digits 0-7. Must be prefixed with a single 0.
  • Hexadecimal: Very common, especially in low-level programming, bitwise operations, and memory addresses. Digits 0-9 and letters A-F (case-insensitive). Must be prefixed with 0x or 0X.

When using printf with %d, the literal's base (octal, hex) is used to determine the *value*, but that value is then *printed* in base 10. If you wanted to print the octal or hexadecimal representation of a number, you would use %o or %x/%X respectively.

Was this answer helpful?

Important Questions from Programming in C

  1. Which among the following is a valid string function?

  2. A function which calls itself is called a

  3. Which of the following function sets first n characters of a string to a given character?

  4. Which of the following statement provides an easy way to dispatch execution to different parts of code based on the value of the expression?

  5. Which of the following operators has left to right associativity?

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App