GATE / CS ARCHIVE

GATE CSE 2000 / QUESTION 1.18

Lexical tokens in a printf statement

Compiler Design · Lexical Analysis · Full GATE CSE 2000 paper

GATE 2000, Q1.18 1 mark · MCQ
Report issue

Lexical Analysis

Question 1.18, GATE 2000: Count tokens in the statement printf("i=%d, &i=%x", i, &i);. Choices:3,26,10,21.
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

Step 1: The entire quoted format string is one string-literal token, regardless of its spaces, percent signs, commas or ampersand.

Step 2: The tokens are printf, (, the string literal, comma, i, comma, &, i, ), semicolon: ten in total.

Hence C. The count concerns lexical analysis, not characters or semantic correctness of the format specifier for a pointer; such a formatting issue does not split the string into more tokens.

Original paper, page 3