What is macros ?

Macro is fragment of code which is given name and replaced by its value at preprocessing stage.

There are two type of macros :

  1. Object like macros
  2. Function like macros

1. Object like macro :

Object like macro is simple identifier which is replaced by value. These are used to give numeric value to symbol. To define PI value as 22/7 we can use object like macro.

You can define macro by ‘#define’ followed by name and its value.

Example:

#define PI 22/7

 

To define a multiline macro using backslash-newline.

To use :

int x[]={VALUES};

Meaning ===> int x[ ]= {1,2,3};

2. Function like macro :

We can define a macro which look like a function.

Example :

#define SQUARE(X) X*X


Code :

Examples of Object and function like macros

 

%d bloggers like this: