Macro
Macro
#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.
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;
}
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.
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;
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.