Thursday, July 12, 2018

C Macros

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, PI is the macro name which will be replaced by the value 3.14.

Function-like Macros

The function-like macro looks like function call. For example:





#define MIN(a,b) ((a)<(b)?(a):(b))



Here, MIN is the macro name.

Visit #define 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.

C 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 :main.c

Date :Jul 12 2018

Time :15:10:03

Line :6

STDC :1

Macros :

  • Macro is a process where an identifier in a program is replaced by a predefined string or value.
  • #define is a preprocessor statement and is used to define macros.

Syntax:

Form 1 : (simple macro)





#define identifier predefined_string or value



Example :





#define pf printf

#define sf scanf



C – MACRO EXAMPLE PROGRAM :






#include<stdio.h>
#include<conio.h>
#define pf printf
#define sf scanf
#define vm void main()
#define cls clrscr
#define gt getch
#define val 100
void main()
{
int n;
cls();
printf("Enter a number : ");
scanf("%d",&n);
printf("Given Number = "%d",n);
p("\nval = %d",val);
getch();
}



Form 2 : (complex macro or macro with arguments)

Syntax:





#define identifier(arg-1,arg-2,……….arg-n) definition



Example :





#define square(x) x*x



C – COMPLEX MACRO EXAMPLE PROGRAM :





#include<stdio.h>
#include<conio.h>
#define square(x) x*x
void main()
{
int n,s;
clrscr();
printf("Enter a number : ");
scanf("%d",&n);
s=square(n);
printf("Square of given number = %d",s);
getch();
}



Difference between function and macro :

Function is a self contained block of statements. Macro is a preprocessor statement.
Function replaces its return value Macro replaces its definition.
We can use only specified data types in functions. In Macros Data types are generic.
Execution speed of function is less. Execution speed of Macro is more compared to function.

C – MACRO EXAMPLE PROGRAM :





#include<stdio.h>
#include<conio.h>
//#define square(x) x*x
//#define square(x) (x*x)
void main()
{
int n;
n=100/square(5);
printf("n = %d",n);
getch();
}






Instagram



No comments:

Post a Comment