GATE CSE 2000 | Question: 1.18, ISRO2015-25
in Compiler Design edited by
26,995 views
26 votes

The number of tokens in the following C statement is

printf("i=%d, &i=%x", i, &i);
  1. $3$
  2. $26$
  3. $10$
  4. $21$
in Compiler Design edited by
27k views

4 Comments

Is white space considered as a token ? and if yes doesn't the above programs have 2 white spaces :

1) after string constant

2) after variable i

?
0
In C , whitespace is not considered as a token class...
0
2
WhiteSpaces will be removed by the lexical analyser from the source code, this is one of the jobs of LA.
1

6 Answers

48 votes
 
Best answer

answer - C

Tokens are:

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

7 Comments

@ankit in case of distinct token count the two i identifiers are considered same or different?

0
What would be the answer if they would have asked to find the number of lexemes?
1
I think they will consider same
0
here the identifier 'i' will be counted as many times as it appears in the source code, but will be given only one token number in the symbol table?
please correct me
1

here the identifier 'i' will be counted as many times as it appears in the source code, but will be given only one token number in the symbol table?

Yes 

0
There is line of declaration and line of usage column in symbol table. There just the line number where occurrence of i is used/declared can be given.
0
10 votes

printf("i=%d,&i=%x",i&i);  it has 9 token but option are not matching 

the same question came in gate 2001  
printf("i=%d,&i=%x",i,&i); with this statement it has 10 token

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

5 Comments

Is the string inside the print statement considered as a single token (token no. 3)...?
0
yes!
0
Thanks for the reply...

Actually my doubt is about the quotes (" " ) shouldn't we count that also...?
0

C tokens are of six types. They are,

  1. Keywords               (int, while,etc),
  2. Identifiers               ( main, total,etc),
  3. Constants              ( 10, 20),
  4. Strings                    ( “total”, “hello”),
  5. Special symbols  ( (,), {,} etc),
  6. Operators              ( +, /,-,*,etc)
20

printf("i=%d,&i=%x",i&i); --- comma is missing, that is why geetting 9 as count.

0
4 votes

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

 

7 Comments

Why is 3."i=%d, &i=%x" taken as one token ? I thought " , i, = , %d , & , i , = ,%x were all separate tokens Why is , and i duplicated twice in the count ?
0

Types of token in c

  1. Keywords               (eg: int, while),
  2. Identifiers               (eg: main, total),
  3. Constants              (eg: 10, 20),
  4. Strings                    (eg: “total”, “hello”) 
  5. Special symbols  (eg: (, {),
  6. Operators              (eg: +, /,-,*)
    when compiler meets " it generate one token for whole string as it means nothing specifically in C just a string.
    i is not duplicated i means value of i and &i means address of i.
5
yes, the format specifiers are handled inside "printf". Not a C token.
1
What is the token category of & here? Is it special symbol?
0
it is an operator- returns the address of operand.
1
Okey
0
Arjun Sir, In the complete reference it has discussed three types of reference-VARIABLE,NUMBER and DELIMITER.Is it correct or the above types of tokens is right?
0
1 vote
if it is  i++   in place of &i   then answer will be 10 ??
by

1 comment

yes bcoz both(i++ and &i ) have 2 tokens.
4
1 vote
Number of tokens(i.e valid lexemes that matches with their pattern ) are

 1)printf   

2). (

3)"i=%d, &i=%x"

4.) ,

5) i

6) ,

7) &

8) i

9) )

10) ;

Hence ,total no of tokens =10  ,Answer C is correct.
0 votes
printf("i=%d, &i=%x", i, &i);

has 10 tokens

 
edited by

1 comment

edited by
it is not appearing as it should, why??
0
Answer:

Related questions