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.
To count tokens, one can make use of NLTK's FreqDist class from the probability package. The N() method can then be used to count how many tokens a text or corpus contains.
There are 6 types of C tokens : identifiers, keywords, constants, operators, string literals and other separators.
Count number of tokens : int main() { int a = 10, b = 20; printf("sum is :%d",a+b); return 0; } Answer: Total number of token: 27.
One can define tokens in C as the smallest individual elements in a program that is meaningful to the functioning of a compiler. A token is the smallest unit used in a C program. Each and every punctuation and word that you come across in a C program is token.
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.
This looks very much like a school assignment or something, but depending on whether or not whitespace counts: 10 or 12 (or 13, if whitespace counts and there is an ending newline)
'printf' '(' '"i = %d, &i = %x"' ',' 'i' ',' '&' 'i' ')' ';'
1 2 3 4 5 6 7 8 9 10
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With