C preprocessor directives

C preprocessor is an micro process. before compilation source code they are translate our source code to compiler. most of case there are use to include header files.

Note that its start with #(hash) sign. and it will execute before compilation of source code.

1) micro expansion

#define directive are known as micro expansion

Examples

Replace statements:

/*
  Macros example replace statements
*/
#include <stdio.h>
#define WELCOME printf("Welcome to c programming");
int main(){
  WELCOME;
}

Output:

Welcome to c programming

Replace operator

/*
  Macros example replace operators
*/
#include <stdio.h>
#define AND &&
#define OR  ||
int main(){
  int a=10,b=15;
  if(a> 5 AND b <20){
    printf("a is greater than 5 and b is less than 20");
  }

  if(a OR b){
    printf("\n a: %d b : %d",a,b);
  }
}

Output

a is greater than 5 and b is less than 20
 a: 10 b : 15

Macros with argument

/*
  Macros with arguments
*/
#include <stdio.h>
#define MAX(a,b) (a>b)?a:b //Macros arguments
int main(){
  int big=MAX(21,41); //get big value
  printf("%d\n",big);

  big=MAX(100,50); 
  printf("%d\n",big);
}

Output

41
100

macros with multi line use (\) backwards slash.

/*
  Macros with multi-line
*/
#include <stdio.h>
#define DISPLAY(a) for(int b=1;b<=a;b++) \
    printf("\n %d",b);
int main(){
  DISPLAY(5);
}

Output

 1
 2
 3
 4
 5


2) File inclusion

file inclusion is a process to include header file and user defined header files. file inclusion are two type.

1) Predefined and
2) User defined

#include < >//predefined
#include " "//user defined
Example 
#include<stdio.h> //predefined
#include "operation.h" //user defined

important points: 1) If we are use( <"filename">) then it will locate and search file given in predefined header file location.
2) If we are use( "filename") then it will first search that particular file in specified location or current location then include given particular file.

So why use file inclusion. the reason is best way to utilize resource and functionality when our need. for example c program are not need to run any header file but we are use < stdio.h >. because they are providing printf() and scanf() function to display and input user data. similarly if we are need string operation like strlen(),strcat() then we are use of < string.h > header file. if include any header file in our c program then we are capable to utilize all inside member functions.


3) Conditional compilation

Given few lists of conditional compilation are follows.

a) #ifdef
b) #else
c) #endif
d) #elseif
e) #ifndef

#ifdef check given macros are defined or not. if defined execute inner statements of this block. for example

/*
  Example of #ifdef and #endif
*/
#include <stdio.h>
#define FLAG 3                    
int main(){
  
  #ifdef FLAG
    printf("FLAG is : %d",FLAG);
  #endif  
  return 0; 
}
FLAG is : 3
/*
  Example of #idefine, #ifdef and #endif
*/
#include <stdio.h>
#define NUM 21 //define micro NUM
int main(){
  /*Conditional macros NUM*/
  #ifdef NUM
    printf("\n There NUM is define : %d", NUM);
    #endif //end if     

  return 0; 
}

Output: There NUM is define : 21

#else

/*
  Example of #else
*/
#include <stdio.h>

int main(){
  /*Conditional macros NUM*/
  #ifdef NUM
    printf("\n There NUM is define : %d", NUM);
  #else
      printf("\n NUM not defined"); 
    #endif //end if     

  return 0; 
}

Output: NUM not defined

#ifndif

/*
  Example of #ifndef
*/
#include <stdio.h>

int main(){
  /*Conditional macros NUM*/
  #ifndef NUM
     #define NUM 50
     printf("\n NUM : %d", NUM);
    #endif 

  return 0; 
}

Output: NUM : 50


4) Predefined Macros

1) __DATE__ : This will return current date of your system. Format of date is "MM : DD : YYYY".

2) __FILE__ : This is returning the name of source file. which are create .exe file in compilation time.

3) __TIME__ : Returning the current time of your system. Time Format is "HH:MM:SS".

4) __LINE__ : returning Line number. current location of macros.

5) __STDC__ : Compiler compile ASCII standards then it will returning 1.

/*
  Example of Predefined Macros.
*/
#include <stdio.h>

int main(){
  
  printf("Date : %s\n",__DATE__);
  printf("FILE : %s\n",__FILE__ );
  printf("TIME : %s\n",__TIME__);
  printf("LINE : %d\n",__LINE__);
  printf("STDC : %d\n",__STDC__);
  
  return 0; 
}

Output:

Date : Nov  8 2018
FILE : usercode.c
TIME : 09:07:53
LINE : 11
STDC : 1

Some other macros

#error : help pof #error macros we are provide user defined errors. most of case our project need some predefined file or some constant variable. if those file and variable not are founds. In this situation there are possible to crash our program. so that is a way to utilize #error macros. For example

/*
    Example of #error macros
*/

#ifndef __STDIO_H
#error <stdio.h> header file missing. first include it
#else
int main(){
  printf("Welcome to programming");

}
#endif

Output

#error stdio.h header file missing. first include it

#warning : Warning are used to inform user to utilize our program. this warning is displayed when compile a program.

/*
    Example of #warning
*/
#include <stdio.h>

#ifndef __STDLIB_H

#warning When create dynamic memory there are need to include stdlib header file. First include it
#endif

int main(){
  printf("Welcome to programming");

}

When complie a program then it will produce warning

When compile this program then it will produce warning

When it will run Output:

Welcome to programming

© 2019, regularcodes.com, All rights reserved