Usually you can see these “include” directive at the very top of your source code. It’s called a preprocessor directive.
All preprocessor statements in C start with a hash(#) symbol. include directive is similar to imports statement in Java.
#include < stdio.h > int main() { printf("Microsoft\n Apple\n Oracle\n Google\n Yahoo\n"); return 0; }
Output:
Microsoft
Apple
Oracle
Google
Yahoo
include Preprocessor Directive In C Program
[youtube https://www.youtube.com/watch?v=4BBml3T8WK4]
This “include” directive causes one file to be included in another. For example, when we include stdio.h (header file) in our above program, all the content(code) gets imported or included in our program source code. This way we can make use of any function present inside stdio.h library file in our c program.
Car Program
We are writing Car simulation program in C – we write all the related code in different files by group them together and then include it in our main Car program. For example, we can separate wheels, color and display related things and then include all those things in our main Car program. This way our main Car program source code looks less clumsy and more over we need not worry about the implementation details present in those files.
Ex: When we include stdio.h file, we do not worry about the implementation details of printf() method. We simply use it for displaying content to the console. All the implementation details are present in stdio.h header file.
There are 2 ways of including header files
#include < stdio.h > #include "Mylib.h"
When we use < and > to wrap around the file name, the linker looks for the files in standard library storage folder. When we wrap the file name in double quotes it looks for the file in current directory.
Usually user written, custom header files are wrapped in double quotes.
We’ll be writing our own library files in this video series. But for now it’ll get too complicated if we go any further.
Note: There are many preprocessor statements other than include directive. We’ll discuss them as and when we use it in our C programs.