Have you ever wondered :
.#include meaning?
.what is “#include” keyword?
.How does #include work?
we will discuss it.
-
DESCRIPTION
#: is a pound sign which helps to pre-process the program before the compilation
include: is a simple directive that tells pre-processor to include the library’s data(i.e. function declarations)
-
HOW TO USE?
in fact,
1 2 |
#include "file-name" #include <header> |
The content of “file-name” or “header” will be copy and paste at position using include keyword. We will add 2 examples to clarify.
example 1:
1 2 3 4 5 6 7 8 9 |
// @file main.cpp #include <stdio.h> int main(int argc, char *argv[]) { printf("Hello world.\n"); return 0; } |
example 2:
1 2 3 4 5 6 7 8 9 |
// @file main.cpp #include <stdio.h> int main(int argc, char *argv[]) { printf("Hello world.\n"); return 0; #include "header.txt" |
1 2 3 |
// @file header.txt } |
-
Once-Only Headers
If a header file happens to be included twice, the compiler will process its contents twice and it will result in an error. The standard way to prevent this is to enclose the entire real contents of the file in a conditional, like this-
1 2 3 4 5 6 |
#ifndef HEADER_FILE #define HEADER_FILE the entire header file file #endif |