C++ 一个字符串占用多少字节?一个字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9384448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How many bytes does a string take? A char?
提问by Moshe
I'm doing a review of my first semester C++ class, and I think I missing something. How many bytes does a string take up? A char?
我正在复习我第一学期的 C++ 课,我想我错过了一些东西。一个字符串占用多少字节?一个字符?
The examples we were given are, some being character literals and some being strings:
我们给出的例子是,一些是字符文字,一些是字符串:
'n', "n", '\n', "\n", "\\n", ""
'n', "n", '\n', "\n", "\\n", ""
I'm particularly confused by the usage of newlines in there.
我对那里的换行符的使用感到特别困惑。
回答by fredoverflow
#include <iostream>
int main()
{
std::cout << sizeof 'n' << std::endl; // 1
std::cout << sizeof "n" << std::endl; // 2
std::cout << sizeof '\n' << std::endl; // 1
std::cout << sizeof 'n' << std::endl; // 1
std::cout << sizeof "\n" << std::endl; // 2
std::cout << sizeof "\n" << std::endl; // 3
std::cout << sizeof "n" << std::endl; // 2
}
Single quotes indicate characters, double quotes indicate C-style strings with an invisible NUL terminator.
单引号表示字符,双引号表示带有不可见 NUL 终止符的 C 样式字符串。
\n
(line break) is only a single char, and so is \\
(backslash). \\n
is just a backslash followed by n.
\n
(换行符)只是一个字符,\\
(反斜杠)也是如此。\\n
只是一个反斜杠后跟 n。
回答by gztomas
'n'
: is not a string, is a literal char, one byte, the ascii code for character n."n"
: string, two bytes, one for n and one for the null character every string has at the end."\n"
: two bytes as \n stand for "new line" which takes one byte, plus one byte for the null char.'\n'
: same as the first, literal char, not a string, one byte."\\n"
: three bytes.. one for \, one for newline and the null character""
: one byte, just the null character.
'n'
: 不是字符串,是文字字符,一个字节,字符 n 的 ascii 代码。"n"
: 字符串,两个字节,一个用于 n,一个用于每个字符串末尾的空字符。"\n"
: 两个字节作为 \n 代表“换行”,它需要一个字节,加上一个字节的空字符。'\n'
: 与第一个文字字符相同,不是字符串,一个字节。"\\n"
: 三个字节..一个用于\,一个用于换行符和空字符""
: 一个字节,只是空字符。
回答by ildjarn
- A
char
, by definition, takes up one byte. - Literals using
'
are char literals; literals using"
are string literals. - A string literal is implicitly null-terminated, so it will take up one more byte than the observable number of characters in the literal.
\
is the escape character and\n
is a newline character.
- A
char
,根据定义,占用一个字节。 - 使用
'
的文字是字符文字;使用"
的文字是字符串文字。 - 字符串文字是隐式以空字符结尾的,因此它将比文字中可观察到的字符数多占用一个字节。
\
是转义字符,\n
是换行符。
Put these together and you should be able to figure it out.
把这些放在一起,你应该能够弄清楚。
回答by Wojciech Cierpucha
The following will take x consecutive chars in memory:
以下将在内存中占用 x 个连续字符:
'n' - 1 char (type char)
"n" - 2 chars (above plus zero character) (type const char[2])
'\n' - 1 char
"\n" - 2 chars
"\n" - 3 chars ('\', 'n', and zero)
"" - 1 char
edit: formatting fixed
编辑:格式固定
edit2: I've written something very stupid, thanks Mooing Duck for pointing that out.
编辑 2:我写了一些非常愚蠢的东西,感谢 Mooing Duck 指出这一点。
回答by wallyk
You appear to be referring to string constants. And distinguishing them from character constants.
您似乎指的是字符串常量。并将它们与字符常量区分开来。
A char
is one byte on all architectures. A character constant uses the single quote delimiter '
.
Achar
在所有架构上都是一个字节。字符常量使用单引号分隔符'
。
A string is a contiguous sequence of characters with a trailing NUL character to identify the end of string. A string uses double quote characters '"'.
字符串是一个连续的字符序列,带有尾随的 NUL 字符来标识字符串的结尾。字符串使用双引号字符 '"'。
Also, you introduce the C string constant expression syntax which uses blackslashes to indicate special characters. \n
is one character in a string constant.
此外,您还介绍了使用黑斜线表示特殊字符的 C 字符串常量表达式语法。 \n
是字符串常量中的一个字符。
So for the examples 'n', "n", '\n', "\n"
:'n'
is one character"n"
is a string with one character, but it takes two characters of storage (one for the letter n
and one for the NUL'\n'
is one character, the newline (ctrl-J on ASCII based systems)"\n"
is one character plus a NUL.
因此对于示例'n', "n", '\n', "\n"
:'n'
一个字符是一个字符"n"
的字符串,但它需要两个存储字符(一个用于字母n
,一个用于 NUL'\n'
是一个字符,换行符(基于 ASCII 的系统上的 ctrl-J)"\n"
是一个字符加上一个 NUL。
I leave the others to puzzle out based on those.
我让其他人根据这些来解谜。
回答by Mooing Duck
'n'
-> One char
. A char
is always 1 byte. This is not a string."n"
-> A string literal, containing one n
and one terminating NULL char
. So 2 bytes.'\n'
-> One char
, A char
is always 1 byte. This is not a string."\n"
-> A string literal, containing one \n
and one terminating NULL char
. So 2 bytes."\\n"
-> A string literal, containing one \
, one '\n', and one terminating NULL char
. So 3 bytes.""
-> A string literal, containing one terminating NULL char
. So 1 byte.
'n'
-> 一char
。Achar
总是 1 个字节。这不是字符串。"n"
-> 一个字符串文字,包含一个n
和一个终止 NULL char
。所以2个字节。'\n'
-> 一char
,Achar
总是 1 个字节。这不是字符串。"\n"
-> 一个字符串文字,包含一个\n
和一个终止 NULL char
。所以2个字节。"\\n"
-> 一个字符串文字,包含一个\
、一个 '\n' 和一个终止 NULL char
。所以3个字节。""
-> 一个字符串文字,包含一个终止 NULL char
。所以1个字节。
回答by triclosan
'n' - 0x6e
"n" - 0x6e00
'\n' - 0x0a
"\n" - 0x0a00
"\n" - 0x5c6e00
"" - 0x00
回答by Jonathan Wood
The number of bytes a string takes up is equal to the number of characters in the string plus 1 (the terminator), times the number of bytes per character. The number of bytes per character can vary. It is 1 byte for a regular char
type.
字符串占用的字节数等于字符串中的字符数加 1(终止符)乘以每个字符的字节数。每个字符的字节数可能会有所不同。对于常规char
类型,它是 1 个字节。
All your examples are one character long except for the second to last, which is two, and the last, which is zero. (Some are of type char
and only define a single character.)
除了倒数第二个是 2 和最后一个是 0 之外,所有示例都是一个字符长。(有些是类型char
并且只定义一个字符。)