Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why # followed by a number seems do nothing in C plus plus

Tags:

c++

comments

Repro steps:

insert the following line into any line of your c++ source code.

#1234

Any line including the first line, the last line. Even you can input between function header and body like this.

int foo()
#1234
{
return 0;
}

The number can be very long, I tested more than 170 characters. If you add any non-numeric character, you will get an compile error.

My question is: why # followed by a number doesn't break the compile, while # followed by a non-numeric character does.

Thanks for your time, everyone.

like image 801
Zhiyong Wu Avatar asked May 13 '13 09:05

Zhiyong Wu


2 Answers

That is a line directive. Most preprocessors output these to tell the compiler which lines it actually is in the original source file.

As the preprocessor can add many (sometimes hundreds or even thousands) lines to the source it provides to the compiler, the compiler needs to way to keep track of the line numbers of the original source file. This is done through special directives such as that.

like image 143
Some programmer dude Avatar answered Oct 23 '22 03:10

Some programmer dude


When I compile it with GCC, I get the following warning:

warning: style of line directive is a GCC extension [enabled by default]

In other words, this is not Standard C++, but a specific compiler extension (a preprocessor extension in this case and, in particular, a line directive).

You should therefore refer to the compiler's documentation to check what exactly is allowed and what is not. For instance, see this.

like image 45
Andy Prowl Avatar answered Oct 23 '22 04:10

Andy Prowl