You are on page 1of 8

Macros

A macro is a piece of code in a program that is


replaced by the value of the macro.
 Macro is defined by #define directive.
Whenever a macro name is encountered by the
compiler, it replaces the name with the definition
of the macro.
Types of macros....
Object like macro...
Itis widely used to represent numeric
content..
Ex-#define A(macro name )=10(macro
value)
Function like macro...
It is just like a function call.
It will substitute the entire logic/code in
place of function name.
Ex- #define min((a,b) (a)<(b)? (a):(b))
Macros vs Function

A macro is a name given to a block of C


statements as a pre-processor directive. Being a
pre-processor, the block of code is
communicated to the compiler before entering
into the actual coding (main () function). A
macro is defined with the pre-processor
directive. Macros are pre-processed which
means that all the macros would be processed
before your program compiles. However,
functions are not pre-processed but compiled.
Examples

Macro Function
#include<stdio.h>
#include<stdio.h>
#define NUMBER 10
int number()
int main()
{
{
return 10;
printf("%d", NUMBER);
return 0; }
} int main()
{
printf("%d", number());
return 0;
}
Types of Predefined Macros in C

 _DATE_  – Used to represent the current date in MM


DD YYYY format.
 _STDC_  – Used to indicate when the compiler
compiles the code successfully with ANSI standard by
returning the value 1.
 _TIME_  – Represent the current time in HH:MM:SS.
 _LINE_  – Represent the current line number.
 _FILE_  – Represent the current file name.
Advantages:-
 It can be used more than once.
 Execution speed is high.
 It saves a lot time that is spent by the compiler for
invoking or function calling.
 It does not allocate any memory.
 Disadvantages ;-
 For big task or big processing we cannot use macro.
 Sometimes is does not provide the desired output.

You might also like