×

C pre-processor

The C processor is a macro processor which is used to compile the source code of the program (step by step) . It is not a part of the compiler. All pre-processor commands begin with the hash symbol (#). The C pre-processor provides various facilities that are:
  • Inclusion of header
  • Macro expansion
  • Conditional compilation
  • Line control
 
Preprocessor Syntax/Description
Macro Macro is used to define the constant value and data types. Syntax: #define
Header file inclusion The source code of the file “file_name” is included in the main program at the specified place. Syntax: #include <file_name>
Conditional compilation It is used to set of commands in source program. Syntax: #ifdef, #endif, #if, #else, #ifndef
Other directives #undef is used to defined a macro variable and #Pragma is used to call a function before and after main function. Syntax: #undef, #pragma
  C language, there are various stages of the execution process. The given following diagram will help you:   There are following important pre-processor directories in C language.
Directive Description
#define Substitutes a pre-processor macro.
#include It is used to inserts a particular header from another file.
#undef It is used to undefine a preprocessor macro.
#ifdef It is used to return true if this macro is defined.
#ifdef It is used to return if this macro is defined.
#if It is used to check compile time condition.
#else The alternative for #if.
#elif #if and #else in one statement.
#endif It is used to end pre-processor conditional.
#error It is used to print an error message on stderr.
#pragma Issues special command to the compiler, using a standardized method.
  Example: 1
#include<stdio.h>
#include”header.h”;
Example 2
#ifndef MESSAGE
 #define MESSAGE “You Wish”
#endif
  C Macros A macro is a name which stands for a fragment of code. Before using macro we must define it with ‘#define’ directive. It is the name of a macro. There are two types of macros.
  1. Object-like macros
  2. Function-like macros 
 
  1. Object-Like Macros:
The object-like macro is an identifier that is replaced by value. It is widely used to represent numeric constants. Example:
Pi =3.147
 
  1. Function like Macros:
Function like macros is used as a function call. Let us consider an example.
#define Max(a,b)((a)>(b)?(a):(b))
Here MAX is the macro name. There are some predefined macros :
Predefined Macros Description
_DATE_ It is defined as the current date with “MMM DD YYY” format.
_TIME_ It is defined as current time with “HH:MM:SS” format.
_FILE_ It contains the current filename as a string literal.
_STDC_ It is defined as 1 when compiler compiles with the ANSI standard.
__STDC_VERSION__ It is defined as standard’s version
_GNUC_ It is defined if and only if this is  GNU C. etc
Example of Macros
#include <stdio.h>
main(){
printf("File :%s\n",__FILE__);
printf("Date :%s\n",__DATE__);
printf("Time :%s\n",__TIME__);
printf("Line :%d\n",__LINE__);
printf("STDC:%d\n",__STDC__);
}
Output
File :main.c
Date :Apr 24 2019
Time :11:50:39
Line :6
STDC:1
  C #define and #include. C #defined: It defines constant value. It can be any of the basic data types. Syntax:
#define token value
Example: 1
#include <stdio.h>
#define PI 3.14
main() {
 printf("%f",PI);
}
Output
3.140000
Example:2
#include <stdio.h>
#define number 3.14
#define letter 'A'
#define letter_sequence "C_Tutorial"
#define backslash_char '\?'
void main(){
 printf("value of number : %f \n", number );
 printf("value of letter : %c \n", letter );
 printf("value of letter_sequence : %s \n", letter_sequence);
 printf("value of backslash_char : %c \n", backslash_char);
}
Output
value of number : 3.140000

value of letter : A

value of letter_sequence : C_Tutorial

value of backslash_char  : ?
  #include: It is used to paste the code of given file into the current file. We can use system-defined and used-defined header files. There are two types of #include directive.
  1. #include< filename>
  2. #include ”filename”
Example
#include<stdio.h>
int main(){
 printf("Hello World ");
 return 0;
}
Output
Hello World
  C #undef: It is used to check undefined the constant or macro. It is defined by #define. Syntax:
#undef token
Example 1:
#include <stdio.h>
#define PI 3.14
#undef PI
main() {
 printf("%f",PI);
}
Output
Compile time Error: PI undeclared
Example 2:
#include <stdio.h>
#define number 15 // variable define
int squ=number*number;
#undef number
int main() {
 printf("%d",squ);
 return 0;
}
Output
225
  #ifdef, #else and #endif The ‘#ifdef’ directive is used to check whether the macro is defined or not. If it is defined then, ‘if’ clause statements are included in the source code. Otherwise “else” clause statements are included in the source file for compilation and execution. Example : #ifdef
#include<stdio.h>
#define AGE 18
void main(){
#ifdef AGE
{
printf("You are Eligible for voting\n");
}
#else
{
printf("Not eligible\n");
}
#endif
}
Output
You are Eligible for voting

  C #Pragma: The C #Pragma is used to call a function before and after the main function. It also provides additional information to the compiler. There are several types of pragmas in GCC compiler.
  • AArch64 Pragmas
  • ARM Pragmas
  • M32C Pragmas
  • MeP Pragmas
  • RS/6000 and PowerPC Praglmas
  • S/390 Pragmas:
  • Darwin Pragmas:
  • Solaris Pragmas: etc.
Syntax:
#pragma token
Example:
#include <stdio.h>
void function1( );
void function2( );
#pragma startup fun1
#pragma exit fun2
int main(){
 printf ( "\n Now we are in main function" ) ;
 return 0;
}
void fun1(){
 printf("\nFun1 is called before main function call");
}
void fun2(){
 printf ( "\nFun2 is called just before end of main function" ) ;
}
Output
Now we are in main function
 

Related Topics

Merge and Merge sort with example in C

Assume we have two ordered Integer arrays, a[] and b[]. If we wish to combine them into another ordered array, such as c[], we can use the following straightforward technique. Compare...

3 minutes read.

Static function in C

The functions in the C programming language are by default global. This means the programmer can easily access the function which is outside from the file where it was initially...

3 minutes read.

Recursion in C

Recursion in C: In the C programming language, the concept known as recursion exists that is a technique in which a function calls itself either directly or indirectly. It allows...

4 minutes read.

Git Hooks

Git Hooks Overview Just like other version control systems, Git has its way to deliver customized scripts whenever some important activity occurs. The hooks act just like the triggers or catalysts...

4 minutes read.

For Loop in C Programming Examples

The Syntax of the For Loop for (initialization statement; termination condition; modifying (increment/ decrement) statement) {       /* main body of the For loop */     } In for loop, the initialization command...

6 minutes read.

Functions in C

What is function? Functions are defined as the set of announcements which take inputs, perform operations and provide results. The operation of a function only performs when the function gets called....

4 minutes read.

getc() function in C

Getc is one of the file handling technique in C. The Getc() is a C library function gets the next character or new characters  from the specific stream and supports...

4 minutes read.

How to convert a string to hexadecimal in C

Converting a character array or any string to its respective hexadecimal form is simple. The only thing we have to do is to follow the below steps. Take each character from...

3 minutes read.

Write() function in c

As the name suggests the write () function is used to write the file descriptor. In other words it is used to write any file name without specifying file name,...

3 minutes read.

Function in C

Function is a group of statements that are used to perform any task. In other words, we can say that a function is a self- contained a block of programs...

3 minutes read.

C pre-processor

The C processor is a macro processor which is used to compile the source code of the program (step by step) . It is not a part of the compiler....

4 minutes read.

Loop Statement in C

Loop statement is used to execute one or more statement repeatedly multiple times. There are three types of loops in C language. Why use loop? We can use loop because it executes a...

2 minutes read.

Else If Ladder in C

What is Else If Ladder: When there are multiple options and a user has to decide from the options, we use Else If Ladder. Basically, it is an extension of if...

3 minutes read.

Conio.h in C

Header files are the source files with the extension of .h. In c language header files are referred to as the helping files and they contain definitions of various functions...

4 minutes read.

Tower of Hanoi in C

What is Tower of Hanoi? The Tower of Hanoi is a gaming problem that was created in 1883 by a French mathematician named Édouard Lucas. The Tower of Hanoi temple in...

4 minutes read.

Typecast vs. typedef in C

Typecast vs. typedef in C Typecast In the C programming language, converting the data type from one form to another is known as type casting or the type conversion. It is a...

5 minutes read.

Binomial Coefficient Program in C

What is Binomial coefficient? In the given set of n possibilities, the binomial coefficient(n,k) indicates the order of choosing 'K' results from those possibilities. Binomial coeeficient of posistive n and k...

3 minutes read.

Difference between while and do-while loop in C

Introduction Both 'While' and 'Do-While' loops in C mostly have a similar concept, and the code of both loops runs for mainly similar purposes. Both loops process the program/code in the...

3 minutes read.

Fibonacci Series in C Using For Loop

In this C article, we will let you know about the procedure of displaying the Fibonacci series of the first “n” positive integers. The syntax of for loop used in the...

4 minutes read.

C Program for Mean and Median of an Unsorted Array

In this tutorial, we will look at how to determine the mean and median of a given unsorted array. To determine the Mean: To get the average, mean is determined. The formula...

2 minutes read.