0

I started learning C programming. I went through this example

#include <stdio.h>
main() {
    printf("Hello, World");
}

It is said that main is start for the program. So, if main starts first, When and how does the first line gets executed?

Cesare
  • 9,139
  • 16
  • 78
  • 130
srinivas
  • 101
  • 2
  • 9
  • 3
    If I'm not mistaken, it's a preprocessor statement and allows the compiler to know where to look to get definitions and implementations for the functions used in the program (such as `printf`). Therefore the _compiler_ reads it. It isn't what you would call "executed" – Arc676 Oct 29 '15 at 13:32
  • 2
    Your program first _preprocessed_. Special engine, called _preprocessor_ reads your file and changes this line with real content of "stdio.h". – user996142 Oct 29 '15 at 14:16

4 Answers4

5

Preprocessor directives such as #include are evaluated and acted upon when your code is compiled, not when it runs. To the extent that they get "executed" at all, that happens outside the scope of any run of the program.

Generally speaking, including a header files such as stdio.h anyway only makes macros, function declarations, type declarations, and sometimes global variable declarations available to your program. There's no direct runtime effect.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
2

Here illustrate generating of program/library (its the same for C):

Code build

The pre-processor simply processes text; it knows virtually nothing about C syntax. It scans the input program text, looking for the directives in which the first character on a line is a '#' or "escaped-newline" sequences. When it meets these directives, it takes certain actions, including other files to be processed (#include), defining symbols (#define), etc (#ifdef). It acts entirely on the program text and will happily pre-process text which may be complete gibberish for the C compiler.

A #include directive reads another file into the program at the point at which it is placed. It effectively merges two input files into a single output file for the compiler. So at this point pre-processor should find all included files (usually headers).

Links:

SergA
  • 1,097
  • 13
  • 21
0

It doesn't, its an #include statement for the preprocessor, leading to it copy pasting the contents of the header file stdio.h to your program, allowing you to use its typedefs and structs and functions or macros. If you use functions, structs, typedefs or macros that are defined in a library, you need to #include that library, so your compiler knows where to get the definitions of the functions you used.

So generally speaking #include does not get executed when you run the program. Its relevant only when you compile your program.

Magisch
  • 7,312
  • 9
  • 36
  • 52
  • No, it doesn't affect the linker at all. – unwind Oct 29 '15 at 13:43
  • @unwind when did I say that? My Answer does not refer to the linker, but the preprocessor. The formulation "linking it to" refers to the fact that it basicly copy-pastes the contents of the lib into your program file. – Magisch Oct 29 '15 at 13:44
  • Sure, then I misunrdestood your use of the word "linking the library". A header file is *not* a library, and "link to a library" generally means something completely different, so I'll remain in opposition to this answer. :) – unwind Oct 29 '15 at 13:52
  • @unwind I corrected some of the language in the answer to make it abundantly clear what I mean. – Magisch Oct 29 '15 at 13:53
  • Better. I'm not convinced that using an `#include` makes the compiler (actually the linker, in my world) aware of where to find definitions. In my experience that's not how it works (hence the classic answer "add `-lm`"). – unwind Oct 29 '15 at 13:55
  • @unwind. At least in my dev environment it works exactly like that. – Magisch Oct 29 '15 at 13:57
  • In MSVC you might not need to add libraries. Some libraries have [`#pragma` directives to guide the linker](http://stackoverflow.com/questions/12821391/c-visual-studio-linking-using-pragma-comment). On other platforms, though, the math functions will reside in separately linked libraries, and need to be manually specified. Of course, the stdio-related objects are linked in by default (and you'd need to opt-out with `-nostdlib`) – sehe Jan 29 '16 at 23:38
0

As others have said it doesn't get executed by your program at all. What happens is before the code is passed to the compiler the exact contents of the file stdio.h, which exists on your computer somewhere the compiler can find it. This allows you to use the code in the file. When you include your own header files later it's important to remember that this is a basic copy paste operation and nothing weird happens.

These instructions that start with a # symbol are called preprocessor directives because they happen early on, before the compiler sees the code.

Fsmv
  • 1,106
  • 2
  • 12
  • 28