Monday, 19 August 2019

Preprocessor Directives in C -#include, #define and #undef


preprocessor directives in c

The Preprocessor in C is system software, known as CPP, is a macro processor. The preprocessor is the programs used before the compilation of source code. Before understanding the working of the preprocessor, first, learn about these steps involved between writing a program and executing a program in C.

Preprocessor directives in C


The source code is written by the programmer, and the source code file is stored in the file program.c. File program.c is preprocessed by the C preprocessor, and generate a source code file named program. The compiler compiles this file, and an object file with .obj extension is generated. In the final step, linker links the object code file with the code of library functions, and produce the executable file program.exe.

If C programs contain preprocessor directives which instruct the compiler to preprocess the source code before compiling, preprocessor directives in C always start with '#' hash symbol. You can use these directives anywhere in the program.

How does a C Preprocessor work?

A preprocessor in C is separate from the compiler. It is not the part of the compiler. It instructs the compiler for pre-processing. It is system software. Preprocessing is done on high-level language.

The preprocessor doesn't know about the scope rules of C. #define (preprocessor directive in C) are implemented as they appear and remain effective until the end of the file. Three steps performed by the C preprocessor on the high-level language code.

preprocessor directives in C


A) Removing Comments

Preprocessor in C eliminates all the comments written by the programmer to understand the program. So, it is useless for a machine. That's why preprocessor removes all the comments from the file.

B) File Inclusion

It includes all the required header/library files. In the C programming language, #include tells the preprocessor to include the content of the library file.

For instance,
#include <stdio.h> or #include "stdio.h"

#include (Preprocessor directive in C) instructs the preprocessor to include the specified library file <stdio.h>. If the library filename is written within angle brackets (< >), the file is searched for in the standard compiler include paths. If the library file is written within double quotes ( " "), the search path is expanded to include the current source directory.

C) Macro Expansion

"Macros" are termed as small function. If we have to use a function or variable name repeatedly, we should give priority to macros. The preprocessor defines these macros. For instance,
#define PI 3.14

A program demonstrates the working of the preprocessor in C

A) Editor File
#include <stdio.h>
int main()
{
int add, a=1, b=2; //Initialize variable 'a' and 'b'
add=a+b; //Addition of two numbers
printf ("Result of addition is %d\n", add); //Prints the output
return 0;
}

B) Preprocessor in C eliminates all the comments from the program.
int main()
{
int add, a=1, b=2;
add=a+b;
printf ("Result of addition is %d\n", add);
return 0;
}

C) The compiler transforms the above high-level code into low-level object code (Assembly Source Code).

D) This assembly source code is converted into the object source code by the assembler.

E) The linker combines the library code with the executable code.

F) The loader reads the program from memory and runs the program.

List of Preprocessor Directives in C

You can learn below mention preprocessor directives:

A) File Inclusion

This type of preprocessor directive instructs the compiler to include a file into the source program. The programmer uses two types of files into the C program.

i) Header Files or Standard Files

These type of files are pre-defined. If you want to include these header files (like stdio.h, string.h, stdlib.h, math.h, etc.) into the program, then you should write like
#include <stdio.h>

Every header file has some functions like <stdio.h> header file has printf, scanf, etc. functions in C. These angle brackets (<>) tells the compiler to search the file in the standard directory.

ii) User-defined files

To reduce the complexity of a large program. A large program is divided into smaller files and used by the programmer whenever needed. These files are known as user-defined files. If you want to access these files in the program, then you should write like #include "filename."

#include preprocessor directives in C is used to include the content of the header or user-defined file into the program.

B) Macros

Macros are part of the program and have some unique name. Whenever we used a macro name in a program, then the compiler replaces the name with the code. Macros are of two types, object-like macros, and function-like macros.

i) Object-like macros

This type of object-like macro is a variable which is replaced by the value. This value can be numeric constant.

To define macros, #define is used, which is a preprocessor directive in C.

Syntax
#define Macro_name Macro_value

Learn how #define works:
#include <stdio.h>
//Definition of Macros does not need semi-colon(;)
#define string "Hello" //String Constant
#define yes 1      // integer constant
#define No 0
#define addition (20+30)

int main()
{
printf ("Print value of a string %s\n\n",string);
printf ("What is the value of yes?\n Answer is %d\n\n", yes);
printf ("No means %d\n", No);
printf ("Addition of two number is %d\n",addition);
return 0;
}

Output
Print value of a string Hello
What is the value of yes?
Answer is 1
No means 0
Addition of two number is 50

ii) Function-like macros

You can define macros with parameters, it works like functions. Let's understand macros-like function with the help of the C program.

#include <stdio.h>
#define area(length,breadth) (length*breadth) //Macro definition
int main()
{
int length1= 12, breadth1=10, Area_of_rectangle; //Initialization of Variable
Area_of_rectangle = area(length1, breadth1); //Calling Function area
printf("Area of Rectangle is %d\n", Area_of_rectangle); //Print output
return 0;
}

Output
Area of Rectangle is 120

Whenever the compiler finds the function area (length1, breadth1), then the compiler will execute the code #define area(length, breadth) (length*breadth) and you will get the output.

C) Conditional Compilation Preprocessor Directives in C

These directives compile a specific part of the program or can skip some specific part of the program based on some conditions. Conditional compilation preprocessor directives in C are:

i) #ifdef

The #ifdef preprocessor directive in C checks #define directive is defined in a program or not.

Syntax
#ifdef Name_of_Macro

C Program demonstrates the working of #ifdef directive.
#include <stdio.h>
#define string "Hello"
int main()
{
#ifdef string //checks string is defined or not
printf ("String is defined");
#endif // string
return 0;
}

Output
String is defined

ii) #ifndef Preprocessor Directives in C

The #ifndef preprocessor directive is the opposite of #ifdef directive. It checks macro is not defined by #define. If the macro is not defined, then it executes the code.

Syntax
#ifndef Name_of_Macro

Let's understand the concept of #ifndef with the help of the program.
#include <stdio.h>
#define string "Hello"
int main()
{
#ifdef string //Checks string is defined by #define
printf ("String is defined\n");
#endif // string
#ifndef string2 //Checks string2 is not defined by #define
printf ("String2 is not defined\n");
#define string2 "Bye"
#endif // string2
printf ("Print string is %s\n String2 is %s\n", string, string2);
return 0;
}

Output
String is defined
String2 is not defined
Print string is Hello
String2 is Bye

Note: #ifdef and #ifndef ended with the #endif. #endif defines the scope of the conditional directive.

iii) #if

This preprocessor directive checks the condition if the condition is true; it executes the block of code.

Syntax
#if Condition//Block of Code#endif

iv) #else

This directive checks the condition if the condition is false; it evaluates a block of code. You can use #else with #if, #elif, #ifdef and #ifndef preprocessor directives in C. Concept of #if and #else directive is similar to the if-else statement in C.

Syntax
#if Condition//if block of code#else//else code#endif

Program demonstrates the working of #if and #else preprocessor directives in C.
#include <stdio.h>
#define Value 30
int main()
{
#if Value == 30
printf ("Value is %d\n",Value);
#else
printf ("value is not defined");
#endif
return 0;
}

Output
Value is 30

v) #elif

This directive compiles multiple conditions.

Syntax
#if Condition//Block of Code#elif Condition//Block of Code#else//Block of Code#endif

D) Other Preprocessor Directives in C

Various preprocessor directives are here,

i) #undef

If you want to undefine defined macros (#define Name_of_Macro Macro_value) then used #undef preprocessor directive.

Syntax
#undef Name_of_Macro Macro_value

Where Name_of_Macro is the Macro to remove by using the #undef directive.

#include <stdio.h>
#define value 20 //Define Macro
int main()
{
printf ("Value is %d\n", value);
//Remove a defined macro
#undef value
printf ("value is %d\n", value);
return 0;
}

Output
Preprocessor directives in c

After using the #undef directive, we need to redefine Macro. See below mention program,

#include <stdio.h>
#define value 20 //Define Macro
int main()
{
printf ("Value is %d\n", value);
#undef value //This directive remove defined macro
#define value 30
printf ("value is %d\n", value);
return 0;
}

Output
Preprocessor directives in c

ii) #pragma directive

This preprocessor directive provides additional information to the compiler. Operation of #pragma directive is based on the compiler. This directive is used to turn on or off some features.

Syntax
#pragma Name_of_token

List of some #pragma preprocessor directives in C are here:
1) #pragma startup
Use #pragma startup directive before the main() function. This directive is used to specify a function in C.
2) #pragma exit
Use this directive before the ending of the program.
3) #pragma warn
The #pragma warn directive suppress or hide warnings.
4) #pragma GCC dependency
This directive is used to checks the dates of the latest file and other files.
5) #pragma poison
This identifier can block the identifier from the program. Below mention C Program demonstrates the working of #pragma preprocessor directive.

#include <stdio.h>
#include <conio.h>
int print();
#pragma startup print
#pragma exit print
int main()
{
printf ("\n Hello World");
return 0;
}
int print()
{
printf ("\n Welcome");
return 0;
}

Output
Welcome
Hello World
Welcome

iii) #error Preprocessor Directives in C

If the #error directive is found in the program, then the compiler aborts the compilation process and produce the fatal error.

#include<stdio.h>
#ifndef __MATH_H
#error Error produces //error preprocessor directive
#else
void main(){
float a;
a=sqrt(7);
printf("%f",a);
}
#endif


No comments:

Post a Comment