24

I am new to c programming and I was coding some simple programs "Hello world" style.

In all of these programs I put #include<stdio.h> in the top but I am not sure what this means exactly. I googled it and I found that stdio.h is a file that has commands for the preprocessor, but what is a preprocessor? I thought when I write code, I compile it and my code transforms to a form that a "computer" can read and then I can run it. Can somebody explain to me what the usage of this command is?

yaylitzis
  • 5,354
  • 17
  • 62
  • 107

4 Answers4

39

It looks for the stdio.h file and effectively copy-pastes it in the place of this #include statements. This file contains so-called function prototypes of functions such as printf(), scanf(), ... so that compiler knows what are their parameters and return values.

  • ok, so when I use printf the code that implement the printf is located in stdio.h? could I implement my version of printf (I know this is silly to do it, but lets say..)? – yaylitzis Sep 30 '13 at 07:11
  • 3
    @Nat95: No, the code for the printf function is not in stdio.h, just the function definition. The linker later resolves this when linking to the standard library. You could absolutely write your own printf function. – Lucas Sep 30 '13 at 07:13
  • +1 this is by far the best and simplest explanation of what the preprocessor effectively does with #include. Btw you can see this if you instruct the compiler/build system to keep preprocessed files. – stijn Sep 30 '13 at 07:17
  • 4
    To see the contents of the header files that would have been pasted above your actual code, compile your source code with `-E` option and you will be able to see the function prototypes that a particular header file contains. @Lucas: one typo mistake it's function prototype or declaration not function definition. – Prabhat Kumar Singh Jan 08 '15 at 11:50
  • I would suggest mentioning that the filename means "Standard Input Output Header" – Mark Entingh Apr 19 '16 at 17:24
15

The simplest explanation perhaps should be that your program calls or uses many functions whose code is not part of your program itself. For e.g. if you write "printf" in your code to print something, the compiler does not know what to do with that call.

stdio.h is the place where information for that printf resides.

Update:

Rather the prototype of printf function (name, return type and parameters) reside in stdio.h. That is all required in the compilation phase. The actual code of printf is included in the linking phase, which comes after compilation.

The include statement basically inserts all function prototypes BEFORE the actual compilation. Hence the name preprocessor.

Update 2:

Since the question focused on include statement (and the OP also asked about writing definition of functions himself, another important aspect is if it is written like (note the angular brackets)

#include <stdio.h>

The preprocessor assumes, it is a standard library header and looks in the system folders first where the compiler has been installed.

If instead a programmer defines a function by himself and place the .h file in the current working directory, he would use (note the double quotes)

#include "stdio.h"

Following illustrates it and the behavior is portable across all platforms.

fkl
  • 5,412
  • 4
  • 28
  • 68
  • ok, so when I use printf the code that implement the printf is located in stdio.h? could I implement my version of printf (I know this is silly to do it, but lets say..)? – yaylitzis Sep 30 '13 at 07:11
  • Yes you can do that. Please understand that my above answer is oversimplified. But it is the right concept to start from. As we go deep, you might see much details. I actually wrote a newer version of printf in my first programming course of BSc more than a decade ago. That involved directly accessed the video memory and writing on specific addresses via pointers. – fkl Sep 30 '13 at 07:13
  • To clarify, there are .h files and there are .c files. h files only contain function prototypes i.e. tell what a function name, return type and parameters are. The actual definition is located in linking phase, which comes after compilation. – fkl Sep 30 '13 at 07:14
1

It tells the compiler to use functions, structures, macros and etc from file sdtio.h, which represents a part of glibc(or whatever is the standart C library you got). Compiler also adds record to the output executable "to-link list", that it should be linked to standart C library.

wirm
  • 100
  • 5
1

Preprocessor directives in a source code are the statements which are processed before the compilation of a program, after this step the source code is converted into the expanded source code as it now contains the references to the functions which are already defined in the Standard C Library(or any other) like printf, scanf, putw, getchar etc. The stdio.h is a file with ".h" extension that contains the prototypes (not definition) of standard input-output functions used in c.

coco97
  • 65
  • 7