- Understanding C program Compilation Process
- Object-like Macros
- Macro Arguments
- Pre Processor Directives in C
- The C Preprocessor
- Standard Predefined Macros
- include guard
- Common Predefined Macros
- What are macros and how to use them
- What are the most common predefined macros
- How to include guard your header files
C macros are preprocessor directives that are used to define constants or create code snippets that are expanded during the preprocessing stage. Macros are defined using the #define
directive and can take arguments or not. Macros can be used to define constants, create inline functions, or to create code snippets that are used multiple times.
Here is an example of how to define a macro that calculates the square of a number:
#define SQUARE(x) ((x) * (x))
This macro can be used as follows:
int x = 5;
int y = SQUARE(x);
printf("%d\n", y); /* prints 25 */
The following are some of the most common predefined macros in C:
__LINE__
: This macro expands to the current line number of the source code file being compiled.__FILE__
: This macro expands to the name of the current source code file being compiled.__DATE__
: This macro expands to a string that represents the date when the source code file was last compiled.__TIME__
: This macro expands to a string that represents the time when the source code file was last compiled.__STDC__
: This macro is defined when the compiler complies with the ANSI C standard.__GNUC__
: This macro is defined when the GNU C/C++ compiler is used to compile the code.__WIN32__
: This macro is defined when the code is compiled for the Windows operating system.
To include guard a header file, you can use preprocessor directives to ensure that the header file is included only once in your code. Here's an example:
#ifndef HEADER_FILE_H
#define HEADER_FILE_H
/* code declarations and definitions go here */
#endif /* HEADER_FILE_H */
When the header file is included in a C or C++ file, the preprocessor first checks whether HEADER_FILE_H
has been defined using #ifndef
. If it has not been defined, the preprocessor defines it using #define
and includes the contents of the header file. If HEADER_FILE_H
has already been defined, the preprocessor skips the contents of the header file.
This ensures that the header file is included only once in your code, preventing issues such as multiple definitions of the same function or variable.
0. Object-like Macro
#include "0-object_like_macro.h"
#include "0-object_like_macro.h"
/**
* main - check the code
*
* Return: Always 0.
*/
int main(void)
{
int s;
s = 98 + SIZE;
printf("%d\n", s);
return (0);
}
Compiled with
gcc -Wall -pedantic -Werror -Wextra -std=gnu89 0-main.c -o a
Output:
$ ./a
1122
$
1. Pi
#include "1-pi.h"
#include "1-pi.h"
#include <stdio.h>
/**
* main - check the code
*
* Return: Always 0.
*/
int main(void)
{
float a;
float r;
r = 98;
a = PI * r * r;
printf("%.3f\n", a);
return (0);
}
Compiled with
gcc -Wall -pedantic -Werror -Wextra -std=gnu89 1-main.c -o b
Output:
$ ./b
30171.855
$
2. File name
Compiled with
gcc -Wall -pedantic -Werror -Wextra -std=gnu89 2-main.c -o c
Output:
$ ./c
2-main.c
$ cp 2-main.c 02-main.c
$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 02-main.c -o cc
$ ./cc
02-main.c
$
3. Function-like macro
#include "3-function_like_macro.h"
#include "3-function_like_macro.h"
#include <stdio.h>
/**
* main - check the code
*
* Return: Always 0.
*/
int main(void)
{
int a;
int b;
a = ABS(-98) * 10;
b = ABS(98) * 10;
printf("%d, %d\n", a, b);
return (0);
}
Compiled with
gcc -Wall -pedantic -Werror -Wextra -std=gnu89 3-main.c -o d
Output:
$ ./d
980, 980
$
4. SUM
#include "4-sum.h"
#include "4-sum.h"
#include <stdio.h>
/**
* main - check the code
*
* Return: Always 0.
*/
int main(void)
{
int s;
s = SUM(98, 1024);
printf("%d\n", s);
return (0);
}
Compiled with
gcc -Wall -pedantic -Werror -Wextra -std=gnu89 4-main.c -o e
Output:
$ ./e
1122
$