Reputation: 5534
I am new to C programming, and as I was coding some simple 'Hello World' style programs, I noticed that in all of them, I included #include<stdio.h>
at the top.
However, I'm not sure what this means exactly. I tried searching online and found that stdio.h
is a file containing commands for the preprocessor, but what is a preprocessor? I thought that when I write code, I compile it, and my code transforms into a form that a computer can read, and then I can run it. Can someone explain to me the usage of this command?"
Upvotes: 25
Views: 108974
Reputation: 65
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.
Upvotes: 1
Reputation: 5535
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.
Upvotes: 16
Reputation: 100
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.
Upvotes: 1
Reputation:
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.
Upvotes: 42