The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+18 votes
5.7k 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 (59.4k points)
edited by | 5.7k views
0
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

+36 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 Loyal (9.2k points)
edited by
0

@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 Boss (15.6k points)
0
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...?
+11

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 Boss (15.6k points)
0
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 ?
+4

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

has 10 tokens

 
answered by Boss (12.3k points)
edited by
0
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 Active (5k points)
+3
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 (233 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

34,943 questions
41,958 answers
119,194 comments
41,472 users