C - Header Files & Preprocessor Directives - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, we are going to learn about C Header Files & Preprocessor Directives.
Humans and computers communicate using input/output devices.
Programming languages facilitate such communication through I/O operations like reading input from the keyboard, displaying output on screens, writing to files, printers, etc.
C language provides many functions to perform, read, and write operations for the I/O devices. These functions are made available in files (usually written with .h extension) referred to, as header files.
Standard I/O library functions are available in a file named stdio.h (standard input output header file).
C language provides a collection of such header files which form the C standard library. These files are usually available in operating systems like Linux by default.
Programmers can also create their own header files which are usually referred to as user-defined header files.
As mentioned earlier, there is a file called stdio.h among the header files present in C standard library, which contains the most commonly used functions to print data to console and to read (also called scan) data.
In order to use the functions available in the header file stdio.h, the following line has to be used in a program:
#include <stdio.h>
It makes the contents of the header file available to the compiler and the linker during the execution of programs.
Now let’s understand some preprocessor directives,
The header files are included in a program using the #include directive.
The header files can be included using #include in two ways as follows:



#include <header_file_name.h>: This variant is used to include system header files made available in C standard library. The compiler searches for the named file in the standard list of system directories.
#include “header_file_name.h”: This variant is commonly used to include user-defined header files. The compiler searches for the named files only in the local or project-specific paths.





#include <header_file_name.h>: This variant is used to include system header files made available in C standard library. The compiler searches for the named file in the standard list of system directories.
#include “header_file_name.h”: This variant is commonly used to include user-defined header files. The compiler searches for the named files only in the local or project-specific paths.



#include <header_file_name.h>: This variant is used to include system header files made available in C standard library. The compiler searches for the named file in the standard list of system directories.
#include “header_file_name.h”: This variant is commonly used to include user-defined header files. The compiler searches for the named files only in the local or project-specific paths.

#include <header_file_name.h>: This variant is used to include system header files made available in C standard library. The compiler searches for the named file in the standard list of system directories.
#include “header_file_name.h”: This variant is commonly used to include user-defined header files. The compiler searches for the named files only in the local or project-specific paths.
Given below is the most commonly included header file which contains the standard input/output functions like, printf(), scanf(), etc.
#include <stdio.h>

Here, the symbol # is called the preprocessor directive, include is called the command, and stdio.h is the header file.
Note: Some compilers automatically include stdio.h. While using compilers that do not automatically include stdio.h, programmers have to explicitly write the include statement in their programs to avoid compilation errors.
In programming terminology, a macro is a pattern or a rule which specifies how a certain sequence of text should be replaced.
C allows us to define two types of macros using the preprocessor directive #define as shown below:

#define PI 3.14
#define MIN(a, b) ((a) < (b)? (a) : (b))

#define PI 3.14
#define MIN(a, b) ((a) < (b)? (a) : (b))
The first type of substitution has been discussed while learning about symbolic constant.
The second type can be used to define a rule or a function that works on given arguments.
Given below is the general syntax for declaring macros using #define:
#define macro_name replacement_text
#define macro_name(arg_1, arg_2... arg_n) function_expression
Consider the following example that swaps the values of two variables using #define macro.
#include
#define SWAP(a, b, c){c t; t=a; a=b; b=t;}
int main()
{
 int x=10, y=20;
 SWAP(x, y, int);
 printf("%d %d\n", x, y);
 return 0;
}

Output:
20 10
Here, SWAP(a, b, tempo) is the name of the macro with three arguments that are replaced with the macro body: (tempo = a, a = b, b = temp0).
C provides a special concatenation operator ## which can be used in a #define directive as shown below:
#define CONCAT(X, Y) X##Y
The ## operator concatenates the values in X and Y forming a single sequence.
For example, CONCAT (20, 21) will result in 2021.
In C, a programmer can instruct the preprocessor whether or not to include a certain section of the code. This is accomplished using conditional directives.
These are similar to if statements in syntax and usage. However, unlike if statements that are executed during runtime, the conditional directives are executed before compilation by the preprocessor.
These directives are used whenever there is a need to compile a portion of the program conditionally. It is also known as conditional compilation.
The most common conditional directive #ifdef (spelled as if-defined) verifies if a given identifier is defined or not.
The syntax for its usage is :
#ifdef identifier or #if defined (identifier)
The source code following #ifdef will be compiled only if the given identifier is defined either in the code or if it is provided as a compilation option as -Didentifier_name.
The opposite of #ifdef is #ifndef, which is written as
#ifndef identifier or #if !defined (identifier)
Consider the following example:
#if defined (MAX)
    #define MIN 30
#else
    #define MAX 100
    #define MIN 200
#endif
Here, MIN 30 will be defined only if the constant MAX is defined earlier than MIN 30. Else, both MAX and MIN will be defined with values 100 and 200 respectively.
Similar to the if-else-if construct, we have a #if-#elif which is used in creating a chain of #if-else statements that can be used to conditionally compile a portion of code.
The general syntax for #if, #elif, #else, #endif is given below:
#if expression1
    statement_block1;
#elif expression2
    statement_block2;
#elif expression3
    statement_block3;
#else
    statement_block4;
#endif
The #undef directive “undefines” a symbolic constant or a macro identifier that has been defined earlier; i.e., it negates the effects of a #define directive that may have appeared earlier in the program.
The #undef directive is used to remove values defined by #define directives so that they can be redefined with new values.
The format of #undef is
#undef symbolic_name
Given below is an example for #undef:
#include <stdio.h>
#define TEMP 20
int main() {
	printf("%d\n", TEMP);
	#ifdef TEMP
		#undef TEMP
		#define TEMP 99
	#else
		#define TEMP 999
	#endif

	printf("%d\n", TEMP);
	return 0;
}
Output:
20
99
Here, the code first prints 20 and then prints99.
Wiki – C standard Library
Wiki – Header Files
Wiki – include directive
Wiki – Special macros and directives 
Happy Learning 

Understanding the structure of a C Program
How add files to S3 Bucket using Shell Script
AngularJs Directive Example Tutorials
How to Copy Local Files to AWS EC2 instance Manually ?
Rules to define Python Identifiers
C – Derived and User Defined Data Types
Java 8 walk How to Read all files in a folder
Pandas read_excel – Read Excel files in Pandas
Variables & Keywords in C Language
Java How to create Jar File ?
How to add dynamic files to JTree
C – Integer Data Types – int, short int, long int and char
Step by Step Tutorials AngularJs Example
php readfile Example Tutorials
Step by Step – How to push the project into GIT Repository

Understanding the structure of a C Program
How add files to S3 Bucket using Shell Script
AngularJs Directive Example Tutorials
How to Copy Local Files to AWS EC2 instance Manually ?
Rules to define Python Identifiers
C – Derived and User Defined Data Types
Java 8 walk How to Read all files in a folder
Pandas read_excel – Read Excel files in Pandas
Variables & Keywords in C Language
Java How to create Jar File ?
How to add dynamic files to JTree
C – Integer Data Types – int, short int, long int and char
Step by Step Tutorials AngularJs Example
php readfile Example Tutorials
Step by Step – How to push the project into GIT Repository
 


Δ
 C – Introduction
 C – Features
 C – Variables & Keywords
 C – Program Structure
 C – Comment Lines  & Tokens
 C – Number System
 C – Local and Global Variables
 C – Scope & Lifetime of Variables
 C – Data Types
 C – Integer Data Types
 C – Floating Data Types
 C – Derived, Defined Data Types
 C – Type Conversions
 C – Arithmetic Operators
 C – Bitwise Operators
 C – Logical Operators
 C –  Comma and sizeof Operators
 C – Operator Precedence and Associativity
 C – Relational Operators
 C Flow Control – if, if-else, nested if-else, if-else-if
 C – Switch Case
 C Iterative – for, while, dowhile loops
 C Unconditional – break, continue, goto statements
 C – Expressions and Statements
 C – Header Files & Preprocessor Directives
 C – One Dimensional Arrays
 C – Multi Dimensional Arrays
 C – Pointers Basics
 C – Pointers with Arrays
 C – Functions
 C – How to Pass Arrays to Functions
 C – Categories of Functions
 C – User defined Functions
 C – Formal and Actual Arguments
 C – Recursion functions
 C – Structures Part -1
 C – Structures Part -2
 C – Unions
 C – File Handling
 C – File Operations
 C – Dynamic Memory Allocation
 C Program – Fibonacci Series
 C Program – Prime or Not
 C Program – Factorial of Number
 C Program – Even or Odd
 C Program – Sum of digits till Single Digit
 C Program – Sum of digits
 C Program – Reverse of a number
 C Program – Armstrong Numbers
 C Program – Print prime Numbers
 C Program – GCD of two Numbers
 C Program – Number Palindrome or Not
 C Program – Find Largest and Smallest number in an Array
 C Program – Add elements of an Array
 C Program – Addition of Matrices
 C Program – Multiplication of Matrices
 C Program – Reverse of an Array
 C Program – Bubble Sort
 C Program – Add and Sub without using + –