C Macros


C macro is a piece of code of the program, that has a specific name called as the macro name. If the macro name is used, then C preprocessor will be replaced with the body of the macro. C program allows us to define a macro for any valid identifier, even for C keyword.

There are two kinds of C macros, which are

  • object-like macros and
  • function-like macros.

Object-like macros

Object-Like macros are identifiers which will be replaced by a sequence of the piece of code in the C program and its name implied. Object-like macro is used to give a meaningful name to a constant.

In order to define a new macro in C program, we use the #define directive.


Syntax:

#define macro_name macro_body

Example:

#define pi 3.14

Function-like macros

It also allows us to define a macro that looks like a function call, which means that these macros are referred to as function-like macros.

This syntax is similar to an object-like macro, except we have to put the parentheses ( ) right after the macro name. Which is,


Syntax:

#define macro_name macro_body ( )

Example:

#include <stdio.h>

void main(){    
   printf("Date :%s\n", __DATE__ );    
   printf("Time :%s\n", __TIME__ );    
 }



OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.