在C语言中,下列语句中使用哪个更好?
static const int var = 5;
or
#define var 5
or
enum { var = 5 };
在C语言中,下列语句中使用哪个更好?
static const int var = 5;
or
#define var 5
or
enum { var = 5 };
当前回答
这取决于你需要这个值做什么。你(和目前所有人一样)忽略了第三种选择:
Static const int var = 5; #定义var 5 枚举{var = 5};
忽略名字的选择问题,那么:
If you need to pass a pointer around, you must use (1). Since (2) is apparently an option, you don't need to pass pointers around. Both (1) and (3) have a symbol in the debugger's symbol table - that makes debugging easier. It is more likely that (2) will not have a symbol, leaving you wondering what it is. (1) cannot be used as a dimension for arrays at global scope; both (2) and (3) can. (1) cannot be used as a dimension for static arrays at function scope; both (2) and (3) can. Under C99, all of these can be used for local arrays. Technically, using (1) would imply the use of a VLA (variable-length array), though the dimension referenced by 'var' would of course be fixed at size 5. (1) cannot be used in places like switch statements; both (2) and (3) can. (1) cannot be used to initialize static variables; both (2) and (3) can. (2) can change code that you didn't want changed because it is used by the preprocessor; both (1) and (3) will not have unexpected side-effects like that. You can detect whether (2) has been set in the preprocessor; neither (1) nor (3) allows that.
因此,在大多数情况下,优先选择“enum”而不是其他选项。否则,第一个和最后一个要点很可能是控制因素——如果你需要同时满足这两个点,你必须更努力地思考。
如果你问的是c++,那么你每次都应该使用option (1) - static const。
其他回答
一般来说:
static const
因为它尊重作用域并且是类型安全的。
我能看到的唯一警告是:如果您希望变量可能在命令行上定义。还有另一种选择:
#ifdef VAR // Very bad name, not long enough, too general, etc..
static int const var = VAR;
#else
static int const var = 5; // default value
#endif
尽可能使用类型安全的替代方法,而不是宏/省略号。
如果你真的需要使用宏(例如,你想要__FILE__或__LINE__),那么你最好非常小心地命名你的宏:在它的命名约定中,Boost推荐所有大写字母,以项目的名称(这里是BOOST_)开头,而仔细阅读库,你会注意到这(通常)后面是特定区域(库)的名称,然后是一个有意义的名称。
它通常会产生很长的名字:)
顺便说一句,#define的另一个选择是“enum”,它提供了适当的作用域,但表现得像一个“真正的”常量。例如:
enum {number_ten = 10;}
在许多情况下,定义枚举类型并创建这些类型的变量是有用的;如果这样做了,调试器可能能够根据变量的枚举名称显示变量。
然而,这样做有一个重要的警告:在c++中,枚举类型与整数的兼容性有限。例如,默认情况下,不能对它们进行算术运算。我发现这是枚举的一个奇怪的默认行为;虽然有一个“严格的enum”类型会很好,但考虑到c++通常与C兼容,我认为“enum”类型的默认行为应该与整数互换。
我写了一个快速测试程序来证明其中的一个区别:
#include <stdio.h>
enum {ENUM_DEFINED=16};
enum {ENUM_DEFINED=32};
#define DEFINED_DEFINED 16
#define DEFINED_DEFINED 32
int main(int argc, char *argv[]) {
printf("%d, %d\n", DEFINED_DEFINED, ENUM_DEFINED);
return(0);
}
这个编译带有以下错误和警告:
main.c:6:7: error: redefinition of enumerator 'ENUM_DEFINED'
enum {ENUM_DEFINED=32};
^
main.c:5:7: note: previous definition is here
enum {ENUM_DEFINED=16};
^
main.c:9:9: warning: 'DEFINED_DEFINED' macro redefined [-Wmacro-redefined]
#define DEFINED_DEFINED 32
^
main.c:8:9: note: previous definition is here
#define DEFINED_DEFINED 16
^
注意,当define给出警告时,enum给出一个错误。
在c#中定义更为流行。你可以使用这些值来声明数组大小,例如:
#define MAXLEN 5
void foo(void) {
int bar[MAXLEN];
}
据我所知,ANSI C不允许你在这种情况下使用静态常量。在c++中,在这些情况下应该避免使用宏。你可以写
const int maxlen = 5;
void foo() {
int bar[maxlen];
}
甚至不用static,因为内部链接已经由const隐含[仅在c++中]。
如果你有mystruct.var这样的东西,#define var 5会给你带来麻烦。
例如,
struct mystruct {
int var;
};
#define var 5
int main() {
struct mystruct foo;
foo.var = 1;
return 0;
}
预处理器将取代它,代码将无法编译。因此,传统的编码风格建议所有常量#定义都使用大写字母以避免冲突。