Friday, July 29, 2016

C Basics - Preprocessors in C

Preprocessing is a separate step in the compilation process. A C Preprocessor is just a text substitution tool and it instructs the compiler to do the required pre-processing before the actual compilation.
All preprocessor commands begin with a hash symbol (#).

Preprocessor directives:
#define : Substitutes a preprocessor macro.
#include: Inserts a particular header from another file.
#undef : Undefines a preprocessor macro.
#ifdef: Returns true if this macro is defined.
#ifndef : Returns true if this macro is not defined.
#if : Tests if a compile time condition is true.
#else : The alternative for #if.
#elif : #else and #if in one statement.
#endif : Ends preprocessor conditional.
#error : Prints error message on stderr.
#pragma : Issues special commands to the compiler, using a standardized method.

Examples:

#define MAX_STRING_LENGTH 80
This directive tells the preprocessor to replace instances of MAX_STRING_LENGTH with 80.

#include <stdio.h>
#include "system.h"
These directives tell the preprocessor to get stdio.h from system libraries system.h from the local directory and add the content to the current source file.

#undef  ARRAY_SIZE
#define ARRAY_SIZE 20
This directive tells the preprocessor to undefine existing ARRAY_SIZE and define it as 20.

#ifndef MESSAGE
   #define MESSAGE "Hello World!"
#endif
This directive tells the preprocessor to define MESSAGE only if MESSAGE isn't already defined.

#ifdef DEBUG
   /* Your debugging statements here */
#endif
This directive tells the preprocessor to process the statements enclosed if DEBUG is defined.

Predefined Macros:
__DATE__ : The current date as a character literal in "MMM DD YYYY" format.
__TIME__ : The current time as a character literal in "HH:MM:SS" format.
__FILE__ : This contains the current filename as a string literal.
__LINE__ : This contains the current line number as a decimal constant.
__STDC__ : Defined as 1 when the compiler complies with the ANSI standard.

Example:
#include <stdio.h>

int main() {

   printf("File :%s\n", __FILE__ );
   printf("Date :%s\n", __DATE__ );
   printf("Time :%s\n", __TIME__ );
   printf("Line :%d\n", __LINE__ );
   printf("ANSI :%d\n", __STDC__ );
   return 0;
}
The output of the above program would be,
File :test.c
Date :Apr 21 2016
Time :09:57:59
Line :594
ANSI :1

Preprocessor Operators:
\ Operator: The macro continuation operator (\) is used to continue a macro that is too long for a single line.
#define  message_for(a, b)  \
   printf(#a " and " #b ": We are with you!\n")

# Operator: The stringize or number-sign operator ( '#' ), when used within a macro definition, converts a macro parameter into a string constant.
#include <stdio.h>

#define  message_for(a, b)  \
   printf(#a " and " #b ": We love you!\n")

int main(void) {
   message_for(Camlie, Doora);
   return 0;
}
The output of the above program would be,
Carole and Debra: We love you!

## Operator: The token-pasting operator (##) within a macro definition combines two arguments.
#include <stdio.h>

#define tokenpaster(n) printf ("token" #n " = %d", token##n)

int main(void) {
   int token56 = 89;
   tokenpaster(56);
   return 0;
}
The output of the above program would be,
token56 = 89

defined() Operator: The defined operator is used in constant expressions to determine if an identifier is defined using #define.
#include <stdio.h>

#if !defined (MESSAGE)
   #define MESSAGE "We are with you!"
#endif

int main(void) {
   printf("Here is the message: %s\n", MESSAGE);  
   return 0;
}
The output of the above program would be,
Here is the message: We are with you!

Parameterized Macros:
One of the powerful functions of the preprocessor is the ability to simulate functions using parameterized macros.
For example, the following functionality can be implemented using macro.

int square(int x) {
   return x * x;
}

We can rewrite above the code using a macro as follows,

#define square(x) ((x) * (x))

For example,

#include <stdio.h>

#define MAX(x,y) ((x) > (y) ? (x) : (y))

int main(void) {
   int i, j;
   printf("Program to find MAX of two numbers\n");
   printf("Enter first number:");
   scanf("%d",&i);
   printf("Enter second number:");
   scanf("%d",&j);
   printf("Max of %d and %d is %d\n", i,j,MAX(i, j));  
   return 0;
}




Related topics:
Standard Input-Output in C   |   File Handling in C   |   Header Files in C   |   Type Casting in C   |   Error Handling in C

List of topics: C Programming

No comments:

Post a Comment