Will the variable declaration below lead to a lexical or syntax error?

If I declare a variable as

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

Now I do not understand the reason for this. According to me, when the character a is read the first time after this character / is read, it is that it switches to another DFA state to recognize another pattern, there is no error, and in the second case, after reading the comment, it finds some other a sequence that cannot belong to a formal template, so it stops in some final state of a finite state machine, because of which it gives an error.

Please clear this confusion.

+6
source share
3 answers

In accordance with standard C (5.1.1.2 translation steps)

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

So this line

 int a/*comment*/bc; 

after the translation phase is equivalent

 int a bc; 

But you could write :)

 int a\ bc; 

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

+6
source

Standard Section C11 5.1.1.2 "Translation Phases", phase 3:

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

Comments are replaced during (well, just before ) the C compilation preprocessing phase. This happens before the "real" parsing happens. Therefore, comments are considered equivalent to spaces in the main body of C.

+6
source

During preprocessing, comments are replaced with a single space.

Your code will look like this:

 int a bc; 
+5
source

All Articles