问题描述
没有定义的宏函数的用途/适用性是什么?
What is the use/applicability of macro function without definition:
#ifndef __SYSCALL
#define __SYSCALL(a, b)
#endif
人们可以在Linux系统的头文件/usr/include/asm/msr.h
One can find this macro in Linux system in header file /usr/include/asm/msr.h
我也注意到以下种类的宏.
I also notice macro of following kind.
#define _M(x) x
我认为可以定义使代码统一的这种宏的唯一原因.就像在 #define SOMETHING(1<< 0)中一样.还有其他隐藏(更好)使用这种宏的方法吗?
And only reason to defined this kind of macro that I can think to make code uniform. like in #define SOMETHING (1 << 0). Is there any other hidden(better) use of this kind of macros?
用示例回答将非常有帮助.还有人可以给我提供文本/链接来阅读有关此内容的信息.
An answer with example will be very helpful. Alsocan someone provide me a text/link to read about this.
推荐答案
这种形式的宏的最常见情况之一:
One of the most common case of a macro of this form:
#define _M(x) x
旨在为仅支持C语言的原始K& R方言的编译器提供向后兼容性,而C& R语言方言早于现在流行的ANSI C方言.在该语言的原始K& R语言中,声明函数时未指定函数参数. 1989年,ANSI对语言进行了标准化,并进行了许多改进,包括声明了参数类型的函数原型.
is to provide backwards compatibility for compilers that only supported the original K&R dialect of C, that predated the now-ubiquitous ANSI C dialect. In the original K&R dialect of the language, function arguments were not specified when declaring the function. In 1989, ANSI standardized the language and incorporated a number of improvements, including function prototypes that declared the number of type of arguments.
int f(int x, double y); /* ANSI C. K&R compilers would not accept this */
int f(); /* Function declared in the original K&R dialect */
尽管现在很少或不支持C的原始K& R方言的编译器,但是当需要同时支持两种编译器时,编写了许多软件,而宏提供了一种同时支持这两种编译器的简便方法.仍然有很多头文件提供这种向后兼容性.
While compilers that support the original K&R dialect of C are rare (or extinct) these days, a lot of software was written when both kinds of compilers needed to be supported, and macros provided an easy way to support both. There are still a lot of headers laying about that provide this backwards compatibility.
为了向K& R编译器提供向后兼容性,许多头文件具有以下内容:
To provide backwards compatibility for K&R compilers, many header files have the following:
#if ANSI_PROTOTYPES
# define _P(x) x
#else
# define _P(x) ()
#endif
...
int f _P((int x, double y));
如果已经正确设置了ANSI_PROTOTYPES
定义(由用户或通过某些先前的#ifdef
逻辑),那么您将获得所需的行为:
If the ANSI_PROTOTYPES
definition has been correctly set (either by the user or by some prior #ifdef
logic), then you get the desired behavior:
- 如果定义了ANSI_PROTOTYPES,则定义将扩展为
int f(int x, double y)
. - 如果未定义ANSI_PROTOTYPES,则定义将扩展为
int f()
- If ANSI_PROTOTYPES is defined, the definition expands to
int f(int x, double y)
. - If ANSI_PROTOTYPES is not defined, the definition expands to
int f()
这篇关于在C中没有定义的宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!