Today we are going to discuss include statements in a C programs. In the last blog of this series we’ve discussed about a function, called printf. We didn’t write the function definition, as it was already present in a file called stdio.h. If you’ve missed that blog, check here.
As the name indicates, include keyword denotes that we are including an external file into the current file. The # symbol also has a special meaning. Before a program is compiled by the compiler, a program called pre-processor runs our file. The pre-processor only reads the lines that begin with a # symbol. So when the compiler finally runs, it already has all the code in the included files merged into the current file. We’ll discuss about pre-processor in another blog.
One of the most commonly included types of files in C programs is called a header file. It has an extension of ‘.h’. Any standard compiler comes with some pre-packaged header files, like ‘stdio.h’. These files have some very important utility functions, that are necessary to run a standard C program. These files are located in a special folder inside the compiler installation directory. But to include these files, we need not worry about writing their complete paths, as that special path is resolved by putting the name of the file inside angle brackets ‘<>‘. If you want to include a file in the current directory use double quotes ( “” ). For any other location you have to write the full path inside the double quotes.
In VS code, we can check the path of stdio.h by first holding the control key and clicking the name of the file inside the include statement to open the file in the editor. Then right click on the editor tab and choose ‘reveal in explorer’
Similar to header files, we can include our own C programs in other programs. Suppose we have created a function which can be used in many programs. Instead of writing it over and over in all the programs we can write it in one .c file and include it in whichever program its needed. The approach I’m showing here is not the standard or recommended way to do it, its for illustration only.
Let us take the same example from the previous blog. We have a function addus to add two numbers. Let us take it out from test.c and create another file addus.c and paste it there. The rest of the program remains as it was.
In test.c we add an include statement to include addus.c in the current file.
So when the pre-processor runs on the program, it grabs the code from addus.c and puts it into test.c. Now the compiler sees that the function addus() is already present in the program, so it runs without errors. The final output after running it:
It is not recommended to directly include ‘.c’ files in your programs. Rather create header files and include those. But this approach serves our purpose of learning what include statements are and how we can include our own files in our programs.
In this blog I have used GCC compiler for windows and VS code as my code editor. If you’re using an IDE, this code may not work. But I highly recommend you to use GCC compiler directly because the development environment I use here is uniform across windows, linux and macOS. Learn more about this setup here.
If you liked this article please comment and show your support and interest so that I’ll be motivated to continue this effort. Like our facebook page if you haven’t already. And if you have any questions please comment. I’ll try to reply all.