Summary: In this tutorial, you will learn about C macros. We will show you two different kinds of C macros, including object-like macros and function-like macros.
#
What is a C macro #
A C macro is a code with a specific name called a macro name. Whenever the macro name is used, the C preprocessor will replace it with the body of the macro. C allows you to define a macro for any valid identifier, even the C keyword.
C provides you with two kinds of macros: object-like and function-like.
Let’s examine each type of macro and the context where you will use it.
Object-like macros #
An object-like macro is an identifier that will be replaced by a sequence of tokens ( or pieces of code) in the program. As its name implies, the object-like macro is similar to a data object in code in terms of its usage. You typically use an object-like macro to give a meaningful name to a constant.
To define a new macro in C, you use the #define
directive. The syntax of the object-like macro is as follows:
#define macro_name macro_body
Code language: C++ (cpp)
In the syntax above:
- You use the
#define
directive followed by the macro name (macro_name
) and macro body (macro_body
). - The macro body is a token sequence that represents the macro name. It is also known as a replacement list or expansion.
- No semicolon (
;
) is used at the end of the statement.
The following example creates a new object-like macro:
#define MAX_SIZE 1000
Code language: C++ (cpp)
The macro name is MAX_SIZE
and it is abbreviated for the constant 1000
. From now on, you can use this macro “like” as an identifier in code such as:
int list[MAX_SIZE];
Code language: C++ (cpp)
The C preprocessor will replace the MAX_SIZE
by 1000
as follows:
int list[1000];
Code language: C++ (cpp)
By convention, you put the macro name in uppercase to make your code easier to read and maintain.
You can define a macro onto multiple lines using backslash-newline. C preprocessor will treat it as one line when the macro is expanded, for example:
#define RGB_COLORS red, \
green,\
blue
enum RGB{ RGB_COLORS };
Code language: C++ (cpp)
Another important point is that the C preprocessor processes programs sequentially. Therefore, the macro is effective from the position where it is defined. Consider the following example:
int SIZE = 10;
#define SIZE 100
int x = SIZE
Code language: C++ (cpp)
The value of x
is 100
.
Function-like Marcos #
C also allows you to define a macro that looks like a function call. Therefore, this macro is referred to as a function-like macro.
The syntax of creating a function-like macro is similar to an object-like macro except you have to put the parentheses ()
right after the macro name.
The following example demonstrates how to use function-like macros:
/*
* File: main.c
* Author: learnc.net
* C Macro Demo
*/
#include <stdio.h>
#include <stdlib.h>
#define min(a, b) ((a) < (b) ? (a) : (b))
int main()
{
int x = 20;
int y = 30;
int result = min(x,y);
printf("Min of %d and %d is %d\n",x,y,result);
return 0;
}
Code language: C++ (cpp)
In the code, the preprocessor only replaces function-like macro when the macro name appears with the parentheses ()
right after it.
In this tutorial, you have learned how to create two kinds of macros in C: object-like and function-like. It is important to use C macro properly to increase your code readability.