如何在C中声明字符串常量?

时间:2021-12-27 21:26:50

I know it's quite idiomatic, or good style at least, in C to declare numeric constants as enums instead of #defineing them.

我知道在C语言中,将数字常量声明为枚举而不是#定义它们是相当习惯的,或者至少是很好的风格。

/* bad style */
#define MAXLINE 1024

/* good/better style */
enum {
    MAX_LINE = 1024
};

Is there an equivalent rule for the definition of string constants?

字符串常量的定义是否有等价的规则?

/* is this good style? */
#define HELLO "Hello World"

/* or is this better? */
const char *HELLO2 = "Howdy";

What do you prefer? If possible show some drawbacks of either method.

你更喜欢什么?如果可能的话,这两种方法都有一些缺点。

4 个解决方案

#1


78  

There's one more (at least) road to Rome:

还有一条(至少)通往罗马的道路:

static const char HELLO3[] = "Howdy";

(static — optional — is to prevent it from conflicting with other files). I'd prefer this one over const char*, because then you'll be able to use sizeof(HELLO3) and therefore you don't have to postpone till runtime what you can do at compile time.

(静态-可选-是为了防止它与其他文件冲突)。我更喜欢这个而不是const char*,因为这样您就可以使用sizeof(HELLO3),因此您不必将在编译时可以做的事情推迟到运行时。

The define has an advantage of compile-time concatenation, though (think HELLO ", World!") and you can sizeof(HELLO) as well.

定义具有编译时连接的优点(请思考“HELLO, World!”),您还可以使用sizeof(HELLO)。

But then you can also prefer const char* and use it across multiple files, which would save you a morsel of memory.

但是,您也可以选择const char*,并在多个文件中使用它,这将为您节省一点内存。

In short — it depends.

简而言之,这要看情况。

#2


12  

One advantage (albeit very slight) of defining string constants is that you can concatenate them:

定义字符串常量的一个好处(尽管非常小)是可以将它们连接起来:

#define HELLO "hello"
#define WORLD "world"

puts( HELLO WORLD );

Not sure that's really an advantage, but it is a technique that cannot be used with const char *'s.

我不确定这是否是真正的优势,但这是一种不能用于const char * s的技术。

#3


9  

If you want a "const string" like your question says, I would really go for the version you stated in your question:

如果你想要一个像你的问题说的“const字符串”,我真的会选择你在你的问题中说的那个版本:

/* first version */
const char *HELLO2 = "Howdy";

Particularly, I would avoid:

特别是,我将避免:

/* second version */
const char HELLO2[] = "Howdy";

Reason: The problem with second version is that compiler will make a copy of the entire string "Howdy", PLUS that string is modifiable (so not really const).

原因:第二个版本的问题是编译器会复制整个字符串“Howdy”,加上这个字符串是可修改的(所以不是真正的const)。

On the other hand, first version is a const string accessible by const pointer HELLO2, and there is no way anybody can modify it.

另一方面,第一个版本是const指针HELLO2可访问的const字符串,任何人都无法修改它。

#4


3  

The main disadvantage of the #define method is that the string is duplicated each time it is used, so you can end up with lots of copies of it in the executable, making it bigger.

#define方法的主要缺点是每次使用字符串时都被复制,所以您可以在可执行文件中得到它的大量副本,使其更大。

#1


78  

There's one more (at least) road to Rome:

还有一条(至少)通往罗马的道路:

static const char HELLO3[] = "Howdy";

(static — optional — is to prevent it from conflicting with other files). I'd prefer this one over const char*, because then you'll be able to use sizeof(HELLO3) and therefore you don't have to postpone till runtime what you can do at compile time.

(静态-可选-是为了防止它与其他文件冲突)。我更喜欢这个而不是const char*,因为这样您就可以使用sizeof(HELLO3),因此您不必将在编译时可以做的事情推迟到运行时。

The define has an advantage of compile-time concatenation, though (think HELLO ", World!") and you can sizeof(HELLO) as well.

定义具有编译时连接的优点(请思考“HELLO, World!”),您还可以使用sizeof(HELLO)。

But then you can also prefer const char* and use it across multiple files, which would save you a morsel of memory.

但是,您也可以选择const char*,并在多个文件中使用它,这将为您节省一点内存。

In short — it depends.

简而言之,这要看情况。

#2


12  

One advantage (albeit very slight) of defining string constants is that you can concatenate them:

定义字符串常量的一个好处(尽管非常小)是可以将它们连接起来:

#define HELLO "hello"
#define WORLD "world"

puts( HELLO WORLD );

Not sure that's really an advantage, but it is a technique that cannot be used with const char *'s.

我不确定这是否是真正的优势,但这是一种不能用于const char * s的技术。

#3


9  

If you want a "const string" like your question says, I would really go for the version you stated in your question:

如果你想要一个像你的问题说的“const字符串”,我真的会选择你在你的问题中说的那个版本:

/* first version */
const char *HELLO2 = "Howdy";

Particularly, I would avoid:

特别是,我将避免:

/* second version */
const char HELLO2[] = "Howdy";

Reason: The problem with second version is that compiler will make a copy of the entire string "Howdy", PLUS that string is modifiable (so not really const).

原因:第二个版本的问题是编译器会复制整个字符串“Howdy”,加上这个字符串是可修改的(所以不是真正的const)。

On the other hand, first version is a const string accessible by const pointer HELLO2, and there is no way anybody can modify it.

另一方面,第一个版本是const指针HELLO2可访问的const字符串,任何人都无法修改它。

#4


3  

The main disadvantage of the #define method is that the string is duplicated each time it is used, so you can end up with lots of copies of it in the executable, making it bigger.

#define方法的主要缺点是每次使用字符串时都被复制,所以您可以在可执行文件中得到它的大量副本,使其更大。