What is the definition and usage of C language macros?
In this article, the editor introduces in detail "what is the definition and use of C language macros". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "what is the definition and use of C language macros" can help you solve your doubts.
I. Macro definition in C language
# define is one of the unit entities processed by the preprocessor
# macros defined by define can appear anywhere in the program
# all code defined by define can use this macro
# Macro constants defined by define can be used directly
# Macro constants defined by define are literal in nature
Are the following macro constants defined correctly?
Write code to test:
# define ERROR-1#define PATH1 "D:\ test\ test.c" # define PATH2 D:\ test\ test.c#define PATH3 D:\ test\ test.c int main () {int err = ERROR; char* p1 = PATH1; char* p2 = PATH2; char* p3 = PATH3;}
First, gcc-E Test.c-o Test.i is used for precompilation, and no error is reported in the precompilation. The results are as follows:
# 1 "Test.c" # 1 "# 1" # 1 "Test.c" int main () {int err =-1; char* p1 = "D:\ test\ test.c"; char* p2 = D:\ test\ test.c; char* p3 = D:\ testtest.c;}
Compile directly and find that char* p2 = PATH2; char* p3 = PATH3; reports an error
This shows that the macro definition is correct, but compilation can not be passed, it is just
# define PATH2 D:\ test\ test.c
# define PATH3 D:\ test\
It does not conform to the grammatical norms.
Second, macro definition expression
The use of # define expressions is similar to function calls
# define expressions can be more powerful than functions
# define expressions are more error prone than functions
One of the powerful things is that you can find the size of an array, which cannot be done by writing a function.
Let's take a look at the code for a macro expression:
# include # define _ SUM_ (a, b) (a) + (b) # define _ MIN_ (a, b) ((a)
< (b) ? (a) : (b))#define _DIM_(a) sizeof(a)/sizeof(*a) int main(){ int a = 1; int b = 2; int c[4] = {0}; int s1 = _SUM_(a, b); int s2 = _SUM_(a, b) * _SUM_(a, b); int m = _MIN_(a++, b); int d = _DIM_(c); printf("s1 = %d\n", s1); printf("s2 = %d\n", s2); printf("m = %d\n", m); printf("d = %d\n", d); return 0;} 下面为输出结果,但是 s2 我们预期的结果应该是 9,m 的值我们预期的结果应该是 1,这是怎么回事呢? 下面进行预编译看看代码到底是怎么运行的,输入 gcc -E Test.c -o Test.i int main(){ int a = 1; int b = 2; int c[4] = {0}; int s1 = (a) + (b); int s2 = (a) + (b) * (a) + (b); int m = ((a++) < (b) ? (a++) : (b)); int d = sizeof(c)/sizeof(*c); printf("s1 = %d\n", s1); printf("s2 = %d\n", s2); printf("m = %d\n", m); printf("d = %d\n", d); return 0;} 通过上面宏定义的替换,我们很容易知道为什么结果跟我们想的不一样。 三、宏表达式与函数的对比 宏表达式被预处理器处理,编译器不知道宏表达式的存在 宏表达式用"实参"完全替代形参,不进行任何运算 宏表达式没有任何的"调用"开销 宏表达式中不能出现递归定义 所以,下面递归定义就是错误的:
Fourth, interesting questions
Is there a scope limit for a constant or expression defined by a macro? (no)
Let's take a look at the code for macro scope analysis:
# include void def () {# define PI 3.1415926 # define AREA (r) r * r * PI} double area (double r) {return AREA (r);} int main () {double r = area (5); printf ("PI =% f\ n", PI); printf ("d = 5; a =% f\ n", r); return 0;}
The following is the output:
The concept of scope is for variables and functions in the C language, not for macros. Macro expressions are processed by preprocessors, and the compiler is unaware of the existence of macro expressions.
Fifth, powerful built-in macro meaning example _ FILE_ compiled file name file1.c_LINE_ current line number 25 date _ date at compile time Jan 31 2021 date _ time at compile time 17 Jan 01R 01mm STDC1 whether the compiler conforms to standard C specification 1
Let's take a look at a comprehensive example of macro usage:
# include # include # define MALLOC (type, x) (type*) malloc (sizeof (type) * x) # define FREE (p) (free (p), p=NULL) # define LOG (s) printf ("[% s] {% slug% d}% s\ n", _ _ DATE__, _ FILE__, _ LINE__, s) # define FOREACH (I, m) for (iTuno; I)