1

Sorry if my question is very basic. I would like to understand the output produced by the preprocessor cpp. Let's say i have a very basic following program.

#include <stdio.h>
#include <stdlib.h>

int x=100;
int main ()
{
    printf ("\n Welcome..\n");
}

I execute the following command.

cpp main.c  main.i

in main.i

# 1 "/usr/include/stdio.h" 1 3 4

What is the meaning of the above line ?..

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Whoami
  • 13,930
  • 19
  • 84
  • 140

1 Answers1

5

The gcc documentation explains the C preprocessor output aptly.

Here are the relevant sections:

The output from the C preprocessor looks much like the input, except that all preprocessing directive lines have been replaced with blank lines and all comments with spaces. Long runs of blank lines are discarded.

Source file name and line number information is conveyed by lines of the form

# linenum filename flags

These are called linemarkers. They are inserted as needed into the output (but never within a string or character constant). They mean that the following line originated in file filename at line linenum. filename will never contain any non-printing characters; they are replaced with octal escape sequences.

After the file name comes zero or more flags, which are 1, 2, 3, or 4. If there are multiple flags, spaces separate them. Here is what the flags mean:

1 This indicates the start of a new file.
2 This indicates returning to a file (after having included another file).
3 This indicates that the following text comes from a system header file, so certain warnings should be suppressed.
4 This indicates that the following text should be treated as being wrapped in an implicit extern "C" block.

Community
  • 1
  • 1
rob05c
  • 1,223
  • 1
  • 9
  • 18
  • @Whoami It's good that you want to understand the preprocessor. I've run into several bizarre C/C++ issues in the past, for which looking at the preprocessed output was the only way to figure out what was going on. This is especially true of large projects with huge numbers of `#include`s – rob05c Mar 02 '12 at 14:31