0% found this document useful (0 votes)
5 views3 pages

Macro

Uploaded by

wansulot
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)
5 views3 pages

Macro

Uploaded by

wansulot
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/ 3

Preprocessor and macros

The C preprocessor is a macro preprocessor (allows you to define macros) that


transforms your program before it is compiled. These transformations can be the
inclusion of header file, macro expansions etc.

C Source Code Preprocessor Compiler

The #include preprocessor is used to include header files to C programs. For


example,

#include <stdio.h>

Here, stdio.h is a header file. The #include preprocessor directive replaces the
above line with the contents of stdio.h header file. That’s the reason why you need to
use #include <stdio.h> before you can use functions like scanf() and printf().
You can also create your own header file containing function declaration and
include it in your program using this preprocessor directive.
#include ”my_header.h”

A macro is a fragment of code which has been given a name. Whenever the name is
used, it is replaced by the contents of the macro. There are two kinds of macros. They
differ mostly in what they look like when they are used:
 object-like macros resemble data objects when used;
 function-like macros resemble function calls.

Here, when we use PI in our program, it is replaced with acos(-1.0).

#include <stdio.h> #include <stdio.h>


#include <math.h> #include <math.h>
#define PI acos(-1.0)
int main(void)
int main(void) {
{ double r = 1;
double r = 1; double area = acos(-1.0) * r * r;
double area = PI * r * r; printf("%lf\n", area);
printf("%lf\n", area); return 0;
return 0; }
}

You can also define macros that work in a similar way like a function call. This is
known as function-like macros. For example,
#include <stdio.h>
#include <math.h>
#define CircleArea(r) acos(-1.0)*(r)*(r)
int main(void)
{
double radius = 1;
double area = CircleArea(radius);
printf("%lf\n", area);
return 0;
}

Every time the program encounters CircleArea(argument), it is replaced by


3.1415*(argument)*( argument)
Suppose, we passed 1 as an argument then CircleArea(1) expands to 3.1415*1*1.
#include <stdio.h>
#define min(x,y) (x < y) ? x : y
#define max(x,y) (x > y) ? x : y

int main(void)
{
int a = 10, b = 5;
printf("%d %d\n", min(a,b), max(a, b));
return 0;
}

Predefined Macros
Here are some predefined macros in C programming.

__DATE__ A string containing the current date


__TIME__ A string containing the current time
__FILE__ A string containing the current fie

#include <stdio.h> #include <stdio.h>

int main(void) int main(void)


{ {
puts(__DATE__); printf("Date: %s\n",__DATE__);
puts(__TIME__); printf("Time: %s\n", __TIME__);
puts(__FILE__); printf("File: %s\n", __FILE__);
return 0; return 0;
} }

typedef
The C programming language provides a keyword called typedef, which you can
use to give a type a new name. Following is an example to define a term ll for 64-byte
numbers:
typedef long long ll;

After this type definition, the identifier ll can be used as an abbreviation for the
type long long, for example
ll a, b, c;

You can use typedef to give a name to your user defined data types as well. For
example, you can use typedef with structure to define a new data type and then use that
data type to define structure variables directly as follows

#include <stdio.h>
typedef long long ll;

typedef struct fraction


{
ll numerator, denominator;
} fraction;

int main(void)
{
fraction a;
a.numerator = 3; a.denominator = 5;
printf("%lld/%lld\n", a.numerator, a.denominator);
return 0;
}

Remember that typedef interpretation is performed by the compiler whereas #define statements
are processed by the pre-processor.

You might also like