Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the preprocessor do with "# <number> <filename>"?

I've just encountered a C file which contains both preprocessor directives and lines that look like this:

# 9 "filename"

I have never seen such lines before. What do they mean? I'm guessing these are preprocessor directives, but what does the preprocessor do with them?

Also, for some of the lines the string doesn't even represent an existing filename...

like image 907
Oak Avatar asked Nov 01 '12 14:11

Oak


1 Answers

I believe it's another way of using the #line preprocessor directive.

For example you could write:

// you could write #line 7 "filename"  or
// # 7 "filename"  or
// # 7  or
#line 7
int main(void)
{
      printf("%d\n", __LINE__);

And all of them would give you (in this case) 10 on stdout.

And a note about the "filename" part it's optional and unverified (that's why it can be anything, even a file that doesn't exist). Its use is explained in the link I provided -
If you specify a file name, the compiler views the next line as part of the specified file. If you do not specify a file name, the compiler views the next line as part of the current source file.

like image 182
Mike Avatar answered Oct 22 '22 00:10

Mike