The number of tokens in the following C statement is
printf("i=%d, &i=%x", i, &i);View worked solution
Answer: C
A token is the smallest individual unit in a source program that is meaningful to the compiler. The lexical analyzer scans the input character-by-character and groups them into tokens.
Let's break down the given C statement into its constituent tokens:
| Token Number | Token | Type |
|---|---|---|
| 1 | printf | Identifier |
| 2 | ( | Separator / Parenthesis |
| 3 | "i=%d, &i=%x" | String Literal |
| 4 | , | Separator / Comma |
| 5 | i | Identifier |
| 6 | , | Separator / Comma |
| 7 | & | Operator (Address-of) |
| 8 | i | Identifier |
| 9 | ) | Separator / Parenthesis |
| 10 | ; | Punctuation / Semicolon |
Note that the entire string "i=%d, &i=%x" is treated as a single string literal token by the lexical analyzer.
Therefore, the total number of tokens is 10.
Free practice · Your answers on this page are not saved.
