SOURAV KABIRAJ
SOURAV KABIRAJ

Reputation: 91

Number of C Tokens

What is the number of tokens in the following C statement?

printf("i = %d, &i = %x", i, &i);

The answer is 10, please explain why this is?

I have searched from http://www.c4learn.com/c-programming/c-tokens-keywords-identifiers/ and from https://msdn.microsoft.com/en-us/library/c6sb2c6b.aspx and from my opinion its 9.

printf - 1
() - 2
"i = %d, &i = %x" - 1
, - 2
i,&i - 2
; - 1

total = 1 + 2 + 1 + 2 + 2 + 1 = 9

I am totally new to c so please help me

Upvotes: 0

Views: 443

Answers (3)

Polish
Polish

Reputation: 466

Following are the tokens ->

  1. printf
  2. (
  3. "i = %d, &i = %x"
  4. ,
  5. i
  6. ,
  7. &
  8. i
  9. )
  10. ;

Upvotes: 0

NlightNFotis
NlightNFotis

Reputation: 9803

It's really not possible to say the correct number of tokens in this statement, without having the lexical specification of the C language in front of us, but with a quick estimate you might get something like this:

printf("i = %d, &i = %x", i, &i);

1. printf - Identifier
2. (      - Left Parenthesis
3. "      - String start (all subtokens count as one)
3.1 i      
3.2 =      
3.3 %      
3.4 d      
3.5 &      
3.6 i
3.7 equals
3.8 %
3.9 x
3.10 "        - string termination
4. ,          - comma (arguments separator)
5. i          - Identifier
6. ,          - comma (arguments separator)
7. &          - address of operator
8. i          - Identifier
9. )          - Right Parenthesis
10. ;         - Statement delimiter

Everything under 3 marks a single string, I have just layed it out in order to make it a little more clear to you.

To get an even more authoritative answer than our guess, you could write whatever it is that you want in a file, say experiment.c and then have clang output the tokens with clang --cc1 -dump-tokens experiment.c

Upvotes: 2

too honest for this site
too honest for this site

Reputation: 12263

Just put a space between everything so that nothing changes in the semantics:

printf ( "i = %d, &i = %x" , i , & i ) ;

Then count the parts between the spaces: 10 tokens! Note that tokens are something like "words", dots, etc. in natural language. They have nothing to do with semantics (which is part of the parser).

Note this is not particularily related to C only (except for which tokens are valid), but general for processing any language. Read about compiler construction. There are some good books along (e.g. N. Wirth's classic is available for free download)

Upvotes: 4

Related Questions