1

Following line is written in the C program

in/*hello*/t k; error or not

According to me,first preprocessor would remove the comments from the code and then code will go to compiler so the code that would go to compiler is

int k;

which is perfectly ok.

but actually when I am running this on gcc compiler it's giving compiler error as

in,k,t is not defined

AstroCB
  • 12,337
  • 20
  • 57
  • 73
Jatin Khurana
  • 1,155
  • 8
  • 15

3 Answers3

9

The comment in the code will be replaced to a white space by the compiler. So

in/*hello*/t k;

will become

in t k;

which is not correct.

C11 §5.1.1.2 Translation phases

3 The source file is decomposed into preprocessing tokens and sequences of white-space characters (including comments). A source file shall not end in a partial preprocessing token or in a partial comment. Each comment is replaced by one space character. New-line characters are retained. Whether each nonempty sequence of white-space characters other than new-line is retained or replaced by one space character is implementation-defined.

Community
  • 1
  • 1
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
3

From C11 5.1.1.2 Translation phases (my emphasis)

The source file is decomposed into preprocessing tokens and sequences of white-space characters (including comments). A source file shall not end in a partial preprocessing token or in a partial comment. Each comment is replaced by one space character

So, as Yu Hao notes, your comment is replaced by a space by the preprocessor. You can test this yourself by using gcc -R your_file.c (gcc) or cl /EP your_file.c (msvc) to view the output from the preprocessor.

simonc
  • 41,632
  • 12
  • 85
  • 103
1

Pre-processor does not remove the comments from the code. What it does is ignore whatever is there in the comment and read the next meaningful character putting a blank space(White Space) for comments. SO you'll get the error as these variables are not defined.

Prakash
  • 579
  • 4
  • 14