Radha Gogia
Radha Gogia

Reputation: 793

Will the below declaration of the variable lead to lexical error or syntactical error?

If I declare the variable like

int a/*comment*/ ; //This does not give any error .
int a/*comment*/bc; This gives error

Now I am not getting the reason behind this , According to me when the character a is read for the first time after that symbol / is read so is it that it switches to some other state of DFA for recognizing some other pattern hence no error while in the second case after comment is read it finds some other sequence which couldn't belong to the formal pattern hence it gets halted in some non-final state of finite automaton due to which it gives an error .

Please clear this confusion .

Upvotes: 6

Views: 135

Answers (3)

Vlad from Moscow
Vlad from Moscow

Reputation: 311068

According to the C Standard (5.1.1.2 Translation phases)

3. ...Each comment is replaced by one space character. 

Thus this line

int a/*comment*/bc; 

after the translation phase is equivalent to

int a bc; 

But you could write :)

int a\
bc; 

provided that bc; starts at the first position of the next line.

Upvotes: 6

Alex Celeste
Alex Celeste

Reputation: 13380

C11 standard section 5.1.1.2 "Translation phases", phase 3:

...Each comment is replaced by one space character. ...

Comments are replaced during (well, just before) the preprocessing phase of C compilation. This is before "real" parsing occurs. Comments are therefore considered equivalent to whitespace in the main part of the C language.

Upvotes: 6

Karoly Horvath
Karoly Horvath

Reputation: 96266

During preprocessing comments are replaced with a single whitespace.

Your code becomes:

int a bc;

Upvotes: 5

Related Questions