Macros

A macro is a segment of a code which is replaced by the value of macro. In C preprocessor is a micro preprocessor and that transforms your program before it is compiled. These transformations can be inclusion of header file, macro expansions etc.
Macro is defined by #define directive. There are two types of macros :

  • Object-like macros
  • Function-like macros
  • Object-like macros:The object-like macros is a identifier that is replaced by value.It is used to represent numeric constants.
    For Example :
    #define PI 3.14
  • Function-like macros: The function-like macros looks like function call.
    For Example
    #define MIN(a,b) ((a)<(b)?(a):(b))

Predefined Macros

C defines many predefined macros that can be used in C program.

No. MacroDescription
1 _DATE_represent current date in "MMM DD YYYY" format.
2_TIME_represent current time in "HH:MM:SS" format.
3_FILE_represent current file name.
4_LINE_represent current line number.
5_STDC_It is define 1 when complie with the ANSI standard.

Predefined Macros Example

File : simple.c

#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("STDC :%d\n", __STDC__ );      
   return 0;  
 }

Output

File : simple.c

File :simple.c
Date :Dec 5 2020
Time :12:28:46
Line :6
STDC :1

Submit your review
1
2
3
4
5
Submit
     
Cancel

Create your own review

The Technical Funda
Average rating:  
 0 reviews