71

I print out the output of C preprocessor by using

gcc -E a.c

The output contains many lines like

# 1 "a.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "a.c"
# 1 "c:\\mingw\\bin\\../lib/gcc/mingw32/4.5.0/../../../../include/stdio.h" 1 3
# 19 "c:\\mingw\\bin\\../lib/gcc/mingw32/4.5.0/../../../../include/stdio.h" 3
# 1 "c:\\mingw\\bin\\../lib/gcc/mingw32/4.5.0/../../../../include/_mingw.h" 1 3
# 31 "c:\\mingw\\bin\\../lib/gcc/mingw32/4.5.0/../../../../include/_mingw.h" 3
       
# 32 "c:\\mingw\\bin\\../lib/gcc/mingw32/4.5.0/../../../../include/_mingw.h" 3
# 20 "c:\\mingw\\bin\\../lib/gcc/mingw32/4.5.0/../../../../include/stdio.h" 2 3

I've never seen this kind of syntax in C. Can someone explain what this is doing?

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
user607722
  • 1,614
  • 2
  • 12
  • 19
  • 5
    How to get rid of them: http://stackoverflow.com/questions/2946898/how-to-remove-lines-added-by-default-by-the-c-preprocessor-to-the-top-of-the-out – Ciro Santilli OurBigBook.com Jan 17 '16 at 11:55
  • I tried "gcc -E main.c -o main2.c" with a simple example on my own. I saw that it starts with # 0 "main.c" (so this refers to line 0), but in the fourth line, the preprocessor stated #1 "main.c". Why did the preprocessor start in line 0 for me and why did it increment, while the posted example starts at line 1? – Dan Nov 20 '22 at 17:37

3 Answers3

82

These lines are hints for debugging (where the code following the line actually came from)

# line-number "source-file" [flags]

Meaning of flags (space separated):

  • 1 - Start of a new file
  • 2 - Returning to previous file
  • 3 - Following text comes from a system header file (#include <> vs #include "")
  • 4 - Following text should be treated as being wrapped in an implicit extern "C" block.
jdehaan
  • 19,700
  • 6
  • 57
  • 97
50

These linemarkers are mentioned in man gcc for -P option.

The -P option is specifically meant to get rid of these lines for clarity:

gcc -E -P source.c

See detailed documentation (answered before).

uvsmtid
  • 4,187
  • 4
  • 38
  • 64
2

Those are line synchronization directives, which allow gcc to give correct error messages for errors in #included files. Other preprocessors (such as yacc/bison) use the same mechanism to relate C errors to the correct lines in the input .y file.

geekosaur
  • 59,309
  • 11
  • 123
  • 114