ctoken

Calculating tokens in c statement


The number of tokens in the following C statement.

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

I think there are 12 tokens here. But my answer is wrong.

Can anybody tell me how to find the tokens in the above C statement?

PS: I know that a token is source-program text that the compiler does not break down into component elements.


Solution

  • As far as I understand C code parsing, the tokens are (10 in total):

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

    I don't count white space, it's generally meaningless and only serves as a separator between other tokens, and I don't break down the string literal into pieces, because it's an integral entity of its own.