C language Pre-Processor Directives

1. #define directive
2. Undefined a Macro
3. Token pasting and stringizing Operators
4. #include directive
5. Conditional Compilation
6. #ifndef Directive
7. #error Directive
8. #pragma Directive
9. Assertions in C
10. Predefined macros in C

1. #define directive

The #define is a pre-processor directive is used to define a macro. A macro can be thought as a placeholder for another identifier. During pre-processing the compiler will replace the “macro” with it’s substitute.

Syntax:

#define “MACRO_NAME”	<value>
Example:
#define MAX	200

Macros should not be ended by semi-colon.

Usually we define macros in capital letters. So that it will be understood by other programmer.

Use Case: You have a currency exchange program, the currency value will be updated daily, instead of updating the value in all the statements, you can define a macro “IN_CURRENCY” and assign the value. When the currency value changes, you just need to change the value. It will be affected everywhere. Of course you need to compile the program again to see the changes.

2. Undefined a Macro

#undef pre-processor directive is used to undefined a macro. Once you undefined a macro, the macro name cannot be used.

Syntax:

#undef	macro_name	

Example:

#undef MAX

Note: No semicolon either.

Example: Programming example for #def and #undef

#include<stdio.h>

#define MAX 100

int main()
{
	printf("The value of MAX = %d\n", MAX );
#undef MAX
	// if you try to use MAX here, it will thorw an error

}
Output:
The value of MAX = 100

3. Token pasting and stringizing Operators

Stringizing operator (#): This operator is used to convert token into string.

Example of Stringizing Operator:

#include<stdio.h>

#define display(name) printf(#name)

int main()
{
	display("prodevelopertutorial.com");

	return 0;
}

Output:

"prodevelopertutorial.com"

Token pasting Operator (##): This operator is used to concatenate 2 tokens. It takes 2 tokens as input and the output will be the concatenation of those 2 tokens

Example for token pasting operator:

#include<stdio.h>
#include<string.h>

#define attach(str_1, str_2) str_1 ## str_2

int main()
{
	char str_1[] = "prodeveloper";
	char str_2[] = "tutorial";
	char website_name [100] = {'\0'};

	// below line is equal to printf("The value of str_1 is %s\n", str_1 );
	printf("The value of str_1 is %s\n", attach(str, _1) );

	// below line is equal to printf("The value of str_2 is %s\n", str_2 );
	printf("The value of str_2 is %s\n", attach(str, _2) );

	// below line is equal to strcat(website_name, str_1);
	strcat(website_name, attach(str, _1));

	// below line is equal to strcat(website_name, str_2);
	strcat(website_name, attach(str, _2));

	printf("The website name is %s\n", website_name );


	return 0;
}
Output:
The value of str_1 is prodeveloper
The value of str_2 is tutorial
The website name is prodevelopertutorial

4. #include directive

#include directive is used to include header files into C program.

#include <”file_name”> will look for the header file in standard directories. For gcc in linux include files are placed in “/usr/include” directory.

#include “file_name” will look for the header file in current directory and standard directory. You can also include absolute or relative path of the header files. It will accept it.

5. Conditional Compilation

“#ifdef” “#endif” “#else” are called as conditional compilation. This helps to include the part of program based on condition while compiling the program. The compiler will check if the “identifier” is defined, if defined then it will execute the #ifdef block, else it will execute #else block. You should use “#endif” to end the conditional compilation block.

Use Case: You are writing a program to both “intel” and “amd” processors. For “intel” processor you need to execute set of statements for “amd” processor you need to execute other set of statements. Then you can use conditional compilation.

You might argue that a simple if..else will do the work. But here the program size will increase. If you use conditional compilation that code will be not included in the object file, thus saving the object file size.

The input for the conditional compilation can be written in program header or can be given as input to the program while compiling.

Syntax:

#defind <identifier>

#ifdef <identifier>
	//statement 1
	//statement 2
#else
	//statement 1
	//statement 2
#endif

Example for Conditional Compilation:

#include<stdio.h>

#define LINE

int main()
{
#ifdef LINE
	printf("LINE is defined\n");
#else
	printf("LINE is NOT defined\n");
#endif

}

Output:

LINE is defined

6. #ifndef Directive

In the above “#ifdef” block will be executed if there is a identifier is defined. “#ifundef” block will be executed if that particular identifier is not defined.

Syntax:

#ifundef <identifier>
		//statement 1
		//statement 2
	#endif

Example for #ifundef

#include<stdio.h>


int main()
{
#ifndef LINE
	printf("LINE is NOT defined\n");
#else
	printf("LINE is  defined\n");
#endif

}
Output:
LINE is NOT defined

7. #error Directive

“#error” directive is used to display programmer an error message during the pre processing stage of the program.

Syntax:

#error <error_message>

Example for #error directive

#include<stdio.h>


int main()
{
#ifdef LINE
	printf("LINE is defined\n");
#else
	#error "MACRO LINE IS NOT DEFINED"
#endif

}
So while compiling a program, the preprocessor will give an error as shown below:
/Users/root/macros.c:9:3: error: "MACRO LINE IS NOT DEFINED"
        #error "MACRO LINE IS NOT DEFINED"
         ^
1 error generated.

8. #pragma Directive

#pragma directives are compiler dependent and usually programmers try to avoid the usage of these directive.

Some of the pragma directives are below:

#pragma startup : Run a function before execution of main
#pragma exit	: Run a program after executing main
#pragma warn <options>:  It displays the warning according to the given options.
Options are:
aus	assigned values but never used
def	possible use of variable before definition
rch	Unreachable Code

Example of pragma directive

#include<stdio.h>

#pragma startup fun_1
#pragma end fun_2

void fun_1()
{
	printf("This function will be called before main function\n");
}


void fun_2()
{
	printf("This function will be called after main function\n");
}


int main()
{
	int num_1 = 0;

	printf("In main function\n");

}

9. Assertions in C

“assert()” macro is used to stop the program execution if the condition is false. It is defined in “assert.h” header file. It will internally execute “abort()” to terminate the program.

Example for “assert()”:

#include<stdio.h>
#include<assert.h>
int main()
{
	int num_1 = 5;

//here the number is 5. Hence the assert condition is false
	assert(num_1 == 4);
}

Output:

Assertion failed: (num_1 == 4), function main, file /Users/root/assert_example.c, line 7.
Abort trap: 6

10. Predefined macros in C

__DATE__		Displays system date in string format
__TIME__		Displays system time in string format
__LINE__		Displays line number in integer format
__FILE__		Displays file name in string format
__func__		Displays function name in string format

Example:

#include<stdio.h>

int main(int argc, char const *argv[])
{
	printf("Date is %s \n",__DATE__ );
	printf("Time is %s \n",__TIME__ );
	printf("Line is %d \n",__LINE__ );
	printf("File is %s \n",__FILE__ );
	printf("Function is %s \n",__func__ );

	return 0;
}

Output:

Date is Aug 12 2018
Time is 13:23:45
Line is 7
File is: /Users/root/programs/macros.c
Function is main
Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *