0% found this document useful (0 votes)
15 views2 pages

Macro in C

Uploaded by

bojodis702
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

Macro in C

Uploaded by

bojodis702
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

cse DOT prabir AT gmail DOT com

The C Preprocessor
Program-1 The statement #define Program-2 Program-3
#include <stdio.h> MAX 5 is called 'macro #include <stdio.h> #include <stdio.h>
#define MAX 5 definition' or 'macro'. #define PI 3.1415 int main()
int main() During preprocessing, int main() {
{ the preprocessor replaces { printf("File :%s\n", __FILE__ );
every occurrence of MAX printf("Date :%s\n", __DATE__ );
int i=10; float r = 5.5, area; printf("Time :%s\n", __TIME__ );
for (i=0; i<MAX; i++) in the program with 5. area = PI * r * r; printf("Line :%d\n", __LINE__ );
printf(" %d ", i); printf("\n Area of circle: %f ", area); return 0;
return 0; Macro substitution return 0; }
} should be done before } Output: ?
Output: 0 1 2 3 4 compilation. Output: Area of circle : 95.030373
***Entire macro expression should be enclosed within parentheses.
Program-4 Program-5 Program-6
#include <stdio.h> #include <stdio.h> #include <stdio.h>
#define AND && #define AND && #define SQR(X) ((X)*(X))
#define OR || #define TESTM (a>2 AND b<20) int main()
int main() int main() {
{ { int j;
int a=6, b=11, c=2; int a=6, b=11; j = SQR(5);
if ( (a>5) AND ( b==10 OR c<5)) if (TESTM) printf(" value of j %d ", j);
printf(" CSE "); printf(" CSE "); getch();
else else return 0;
printf(" IT "); printf(" IT "); }Output: value of j 25
return 0; return 0;
} }
Output: CSE Output: CSE
Macros vs. Functions:
o In a macro call the preprocessor replaces the macro template with its macro expansion. In a function call the
control is passed to a function along with certain arguments, some calculations are performed in the function
and a useful value is returned back from the function.
o Usually macros make the program run faster but increase the program size, whereas functions make the
program smaller and compact.
Different file inclusion :
#include "test.c" #include <test.c>
This command would look for the file test.c in the This command would look for the file test.c in the
current directory as well as the specified list of specified list of directories only.
directories as mentioned in the include search path
that have been set up.
Conditional Compilation:
If a macro name has been defined (#define), the block of code will be processed as usual; otherwise not.
Program-7 Program-8
#include <stdio.h> #include <stdio.h>
int main() #define MAX 10
{ int main()
#ifdef MAX {
printf("\n A "); #ifdef MAX
printf("\n B "); printf("\n A ");
#else printf("\n B ");
printf("\n C "); #else
printf("\n D "); printf("\n C ");
#endif printf("\n D ");
return 0; #endif
} return 0;
Output: C D }
Output: A B
Program-7 printf("\n C ");
#include <stdio.h> printf("\n D ");
int main() #endif
{ return 0;
#ifndef MAX // means if not defined }
printf("\n A "); Output: A B
printf("\n B ");
#else
cse DOT prabir AT gmail DOT com
Program-8 #undef Directive
#include <stdio.h> On some occasions it may be desirable to cause a
#define MAX 5 defined name to become undefined. This can be
int main()
{
accomplished by means of the #undef directive. In
#if MAX>5 order to undefine a macro that has been earlier
printf("\n A "); #defined, the directive #undef macro template can
printf("\n B "); be used. Thus the statement,
#else
printf("\n C ");
printf("\n D "); #undef MAX
#endif
return 0;
}
Output: C D

You might also like