Before learning the C macro, you should know about C preprocessor directives in C language. In this post, you can clearly understand the following
- what preprocess commands are available in C?
- How do you declare the preprocessor command.?
- what is a macro?
- What is the macro syntax?
- how to declare the macro?
- how to assign the macro value?
- how can use the macro function in c.
Here your answer, if you have any doubts please post in doubts about C Preprocessor and Macros.
In C language, totally 11 pre-processor commands are there,
Preprocessor Command | Description |
---|---|
#define | Substitutes a preprocessor macro. |
#include | Inserts a particular header from another file. |
#undef | Undefines a preprocessor macro. |
#ifdef | Returns true if this macro is defined. |
#ifndef | Returns true if this macro is not defined. |
#if | Tests if a compile time condition is true. |
#else | The alternative for #if. |
#elif | else and #if in one statement. |
#endif | Ends preprocessor conditional. |
#error | Prints error message on stderr. |
#pragma | Issues special commands to the compiler, using a standardized method. |
The below table is the syntax, rule and compiling stage of preprocessor commands
Syntax | : For header file #include <stdio.h> For macro or |
Rule | All the preprocessor command should start with (hash) and followed by an input file or macro. Preprocessor declaration should be a non-blank character, If you do not mention the for include files or macro then the compiler will give an error. |
Preprocessor command in Compiling stage | #include : While compiling stage, it will remove the #Include and replace the header file PATH in the source code.#define : While compiling stage, also remove #define (Macro) with replacing macro name with code |
Contents
- 1 What is MACRO in C?
- 2 C Preprocessor and Macros Examples
- 2.1 Understand of Macro while compilation time
- 2.2 How to print the Macro value in C
- 2.3 Macro basic example-1
- 2.4 Two time declare the same macro but different value assignment
- 2.5 How you define Macro for how many seconds in Year?
- 2.6 How you define Marco using find small number?
- 2.7 Find out MIN & MAX using MACRO in C?
- 2.8 Write a macro to find the smallest number among 3 given numbers?
- 2.9 Swap Two Numbers using MACRO
- 2.10 MACRO Example of complete code
- 2.11 Explore more
What is MACRO in C?
Macros are the names of text/ literal values/ string (constant values) or code fragments, which will expand when the pre-processor processes the macro. The pre-processor processes the macros at compile-time; hence that macros replace with the corresponding code fragments.
A macro can execute faster than a function: As the code of macro gets expanded at the line of call, therefore macro gets executed faster with no overhead of context switch.
Syntax
#define <MACRO_NAME> <MACRO_VALUE>
- Here
#define
denote user declaring a macro in c - <MACRO_NAME> user can provide any char, String
- <MACRO_VALUE> user can provide any value to assign the value
Rules
- You should give the space in between #define and Macro Name.
- You should give the space in between Macro Name and Macro Value.
- No need to add the
;
semicolon in end of the declaration as like variable value assignment.
Declaration
You should declare the macro with proper syntax like #define MACRO_NAME
Assign Macro Value
you can assign any values, Stings and athematic actions in macro
- Assign macro as string value:
#define WEBSITE "www.ArunEworld.com"
HereWEBSITE
is the macro andwww.arunEworld.com
is the macro string value. - Assign macro as char value:
#define LETTER 'A'
HereLETTER
is the macro and'A'
is the macro char value. - Assign macro as digits value:
#define DIGIT 2
, HereDIGIT
is the macro and2
is the macro digit value.
C Preprocessor and Macros Examples
Here You can find many more examples related to C Preprocessor and Macros
Understand of Macro while compilation time
#define MAX_NUM 15
Referring to the sample above, what is MAX_NUM?
(A) MAX_NUM is a precompiler constant.
(B) MAX_NUM is a preprocessor macro.
(C) MAX_NUM is an integer variable.
(D) MAX_NUM is a linker constant.
Answer : MAX_NUM is a preprocessor macro.
How to print the Macro value in C
While assign the macro value, user should know the value type in order to print the macro using printf()
function
- If Macro is like
#define WEBSITE "ArunEworld.com"
then you can print the macro usingprintf("%s", WEBSITE)
. If you try to print as aprintf("%d", WEBSITE)
then compiler give the warning [warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat=]] but it will print the corresponding putput value4195812
This 4195812 is belong to address of the"www.ArunEworld.com"
string storing address in memory. - If Macro is like
#define VALUE 30
then you can print the macro usingprintf("%d", VALUE)
. If you try to print as aprintf("%c", VALUE)
then corresponding ASCII value will be print.
Macro basic example-1
In this below code you can clearly understood what happening when we use the macro
Example-1: Actual Code:
#include <stdio.h>
#define OPEN_CURLY_BRACE {
#define CLOSE_CURLY_BRACE }
int main()
OPEN_CURLY_BRACE
printf("ArunEworld");
return 0;
CLOSE_CURLY_BRACE
Example-1:While Compiling the above code(Actual Code)
#include <stdio.h>
#define OPEN_CURLY_BRACE {
#define CLOSE_CURLY_BRACE }
int main()
{
printf("ArunEworld");
return 0;
}
Example-2:Actual Code
#include <stdio.h>
#define INT_TYPE int
INT_TYPE main()
{
printf("ArunEworld");
return 0;
}
Example-2: While compiling above code(Actual Code)
#include <stdio.h>
int main()
{
printf("ArunEworld");
return 0;
}
Two time declare the same macro but different value assignment
If your declared the same macro two times but different value then warning [warning: “MACRO_NAME” redefined] should be generated by compiler and print the last declared macro value in console
Code
#include <stdio.h>
#define MACRO_NAME "ArunEworld"
#define MACRO_NAME "ArunEworld Macro re-defined example"
int main()
{
printf("%s", MACRO_NAME);
return 0;
}
Output:
main.c:12:0: warning: "MACRO_NAME" redefined
main.c:11:0: note: this is the location of the previous definition
ArunEworld Macro re-defined example
How you define Macro for how many seconds in Year?
#define SECONDS_PER_YEAR (60 * 60 * 24 * 365)UL
How you define Marco using find small number?
- Answer :
#define MIN(x,y) ((x<y)?x:y)
Example
#include <stdio.h>
#define MIN(x,y) ((x<y)?x:y)
int main()
{
int a,b,min;
printf("Enter first number: ");
scanf("%d",&a);
printf("Enter second number: ");
scanf("%d",&b);
min=MIN(a,b);
printf("Minimum number is: %d\n",min);
return 0;
}
/*
Enter first number: 100
Enter second number: 200
Minimum number is: 100
*/
Find out MIN & MAX using MACRO in C?
- Answer :
#define MIN(X,Y) (X<Y ? X:Y) or #define MAX(X,Y) (X>Y ? X:Y)
Example code:
#include <stdio.h>
#define MIN(x,y) ((x<y)?x:y)
int main()
{
int a,b,min;
printf("Enter first number: ");
scanf("%d",&a);
printf("Enter second number: ");
scanf("%d",&b);
min=MIN(a,b);
printf("Minimum number is: %d\n",min);
return 0;
}
Output:
/*
Enter first number: 100
Enter second number: 200
Minimum number is: 100
*/
Write a macro to find the smallest number among 3 given numbers?
Answer :
Example Code :
#include<stdio.h>
#define MIN(X,Y)(X<Y ? X:Y)
#define SMALL(X,Y,Z)(MIN(X,Y)<Z ? MIN(X,Y):Z)
void main()
{
int FIRST,SECOND,THIRD;
clrscr();
printf("ENTER NUMBERS TO COMPARE\n");
printf("\nFIRST NUMBER : ");
scanf("%d", &FIRST);
printf("\nSECOND NUMBER : ");
scanf("%d", &SECOND);
printf("\nTHIRD NUMBER : ");
scanf("%d", &THIRD);
printf("\nThe SMALLER NUMBER IS : %d", SMALL(FIRST,SECOND,THIRD));
getch();
}
Output
ENTER NUMBERS TO COMPARE
FIRST NUMBER : 11
SECOND NUMBER : 22
THIRD NUMBER : 33
The SMALLER NUMBER IS : 11
Swap Two Numbers using MACRO
#define SWAP(x, y) (x ^= y ^= x ^= y)
MACRO Example of complete code
#include<stdio.h> #define S printf("ArunEworld"); #define AEW(S) int main()\ {\ S\ return 0;\ } AEW(S) /* Output: ArunEworld */