Welcome to
Logically Proven Blog.
This post teaches
you file inclusion in your ‘C’ application and the difference between
#include “file” and #include <file>
File inclusion is
an important preprocessor directive of C programming. This directive causes one
file to be included in another.
Syntax:
#include "filename"
It causes the
entire contents of filename to be inserted into the source code at that point
in the program.
We can use the file
inclusion for the following benefits –
- If we have a large program, divide the code into set of related functions to understand and organize your code in a better way. This increases readability of your code.
- One more benefit of using file inclusion is re-usability. For example we need some macro definitions that requires in all programs that are commonly used. In this case store these macro definitions in a file and just include the file into your application.
The files that are
included to have a ‘.h’ extension. The extension stands for “header file”. This
header file contains C function declarations and macro definitions and to be
shared between several source files. There
are two types – user header files (programmer writes) and compiler header files
(that comes with the compiler).
For example,
stdio.h header file, this comes along with your compiler.
The related library
functions are grouped into different categories and then stored in different
header files. For example all mathematical related functions are stored in the
header file math.h.
The #include
directive works by directing the C preprocessor to scan the specified file as
input before continuing with the rest of the current file. Look at the following
example to know how the preprocessor processes the #include directives.
For example, if you have a header file header.h as follows,
char *test(void);
and a main program called program.c that uses the header file, like this -
int x; #include "header.h" int main(void) { puts(test()); return 0; }
the compiler will see the same token stream as it would if program.c read
int x; char *test(void); int main(void) { puts(test()); return 0; }
Difference between
#include “file” and #include <file>
There are two ways
to write #include statement in your program.
#include "filename" #include <filename>
#include “filename”
This command would
look for the file “filename” in the current directory as well as the specified
list of directories as mentioned in the include search path (compilers can be
set up by selecting directories from the options menu, e.g., Turbo C/C++
compiler) that might have been set up.
#include <filename>
This command would
look for the file “filename” in the specified list of directories only.
Please write your comments if you find anything is incorrect or do you want to share more information about the topic discussed above.
Logically Proven,
Learn, Teach, Share
0 comments:
Post a Comment