我知道在C语言中,将数字常量声明为enum
而不是使用#define
定义它们是很惯用的,或者至少是一种良好的编程风格。
/* bad style */
#define MAXLINE 1024
/* good/better style */
enum {
MAX_LINE = 1024
};
有没有关于定义字符串常量的相应规则?
/* is this good style? */
#define HELLO "Hello World"
/* or is this better? */
const char *HELLO2 = "Howdy";
你更喜欢哪种方法?如果可能的话,请展示一些两种方法的缺点。
const char*
比const char []
在静态适用的情况下更好在哪里。但是再说一遍,如果你不需要大小,它也不会更糟糕;-) - Michael Krelin - hacker