Defining and calling macros in C Language

Defining and calling macros in C Language
Defining and calling macros in C Language

A macro is a segment of code which is replaced by the value of macro. Macro is defined by #define directive. There are two types of macros:

  1. Object-like Macros
  2. Function-like Macros

Object-like Macros

The object-like macro is an identifier that is replaced by value. It is widely used to represent numeric constants.

For example:    #define PI 3.14

Here, MIN is the macro name.

to see the full example of object-like and function-like macros.

C Predefined Macros

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

No.MacroDescription
1_DATE_represents current date in “MMM DD YYYY” format.
2_TIME_represents current time in “HH:MM:SS” format.
3_FILE_represents current file name.
4_LINE_represents current line number.
5_STDC_It is defined as 1 when compiler complies with the ANSI standard.

#include  
 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
Date :Dec 6 2015
Time :12:28:46
Line :6
STDC :1
c language tutorial learn c language study c language