The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+16 votes
4.4k views

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$
asked in Compiler Design by Veteran (68.8k points)
edited by | 4.4k views
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

?

6 Answers

+31 votes
Best answer

answer - C

Tokens are:

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

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

+8 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.;

answered by Veteran (15.8k points)
Is the string inside the print statement considered as a single token (token no. 3)...?
yes!
Thanks for the reply...

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

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)
+4 votes

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

 

answered by Veteran (15.8k points)
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 ?

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.
yes, the format specifiers are handled inside "printf". Not a C token.
What is the token category of & here? Is it special symbol?
it is an operator- returns the address of operand.
Okey
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?
+1 vote
printf("i=%d, &i=%x", i, &i);

has 10 tokens

 
answered by Veteran (15.8k points)
edited by
it is not appearing as it should, why??
+1 vote
if it is  i++   in place of &i   then answer will be 10 ??
answered by Loyal (4.7k points)
yes bcoz both(i++ and &i ) have 2 tokens.
+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.
answered by (175 points)


Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

32,505 questions
39,217 answers
109,115 comments
36,599 users