1

I'm running a prova.c file on my terminal.
I ran GCC with the command line:

gcc -Wall -std=c99 -E prova.c  

prova.c:

 int main(int argc, char* argv[]) {
           int a=1;
           int b=2;
           return a+b;   
    }


stdout:

# 1 "prova.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "prova.c"
int main(int argc, char* argv[]) {
  int a=1;
  int b=2;
  return a+b;
}

What does every line with the hash in the beginning mean?
Why do we need them to get generated?
If you have time I would like to have an explanation for every single line more than the general answer. I know some of them are line markers.
If you know resources on the topic sharing them would be awesome.

  • What did you expect when passing the -E switch to gcc? – n. m. could be an AI Feb 21 '17 at 10:06
  • I'm not saying I didn't expect them, I'm just wondering why they're generated and what do they exactly mean. They must have some sort of utility or meaning, otherewise there would be no point in generating them! – Gabriele Scarlatti Feb 21 '17 at 10:36
  • If you have passed -E at random without knowing what it does, just stop doing this. If you knew something about -E beforehand, tell us what it was. – n. m. could be an AI Feb 21 '17 at 10:48
  • I passed it becuase I wanted to see how my program expanded after preprocessing stage. Before asking this question, I 've made some search online and I find out they're line markers, but couldn't understand clealry what is thier role, if they will be helpful for the compiler the linker.... – Gabriele Scarlatti Feb 21 '17 at 12:30
  • For example, I know # 1 "prova.c" according to gcc documentation means that the following line is originated at line 1 in file "prova.c", so? what does it mean "the following line is originated"? To me it doesn't make sense. Thank you very much for your help! – Gabriele Scarlatti Feb 21 '17 at 12:30
  • 1
    The word "following" means what it normally means, i.e. "the one right after this one". The word "originated" means what it normally means, i.e. "came from". So the line that is right after `# 1 "prova.c"` comes from line 1 in file `"prova.c"`. Except in your case it's not quite true, because the line after the first `# 1 "prova.c"` is another line with the # sign. This is a slight inexactitude with gcc documentation. It should only apply to a regular next line, without the # sign. But you can be sure that before returning at the first line of `prova.c` the compiler looked at all these places – n. m. could be an AI Feb 21 '17 at 16:28
  • 1
    listed in lines with the # sign. Not all of those are real files that exist in the file system, like `` or ``, but they function like normal files. So the compiler looked at line 1 of "prova.c", then looked at some internal thing called ``, then at some other internal place called `` etc, and then came back to line 1 of "prova.c". The next line, `int main(int argc, char* argv[]) {` , comes from line 1 of "prova.c". The line after that comes from line 2 of "prova.c". This continues until another line that begins with `#` is seen. – n. m. could be an AI Feb 21 '17 at 16:36
  • 1
    Possible duplicate of [What is the meaning of lines starting with a hash sign and number like '# 1 "a.c"' in the gcc preprocessor output?](http://stackoverflow.com/questions/5370539/what-is-the-meaning-of-lines-starting-with-a-hash-sign-and-number-like-1-a-c) – jww Feb 28 '17 at 02:45

1 Answers1

0

This is not a full answer, as you prefer, but it can give you some hints.

The -E argument given to gcc does (link):

Stop after the preprocessing stage; do not run the compiler proper. The output is in the form of preprocessed source code, which is sent to the standard output.

That explains the commands # 1 you get. Now, you can go to the documentation about the preprocessor output here. It says:

‘1’ This indicates the start of a new file.

I'm not sure about the commands inside the # 1 blocks.

ChronoTrigger
  • 8,459
  • 1
  • 36
  • 57