一个字符串需要多少字节?一个炭?
我正在复习第一学期的 C++ 课程,我想我遗漏了一些东西。一个字符串占用多少字节?一个炭?
我们给出的例子是,一些是字符文字,一些是字符串:
'n', "n", '\n', "\n", "\\n", ""
I我对其中换行符的使用感到特别困惑。
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?
The examples we were given are, some being character literals and some being strings:
'n', "n", '\n', "\n", "\\n", ""
I'm particularly confused by the usage of newlines in there.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
NUL
的 C 风格字符串终结者。
\n
(换行符)只是一个字符,\\
(反斜杠)也是如此。\\n
只是一个反斜杠,后跟n
。NUL
terminator.
\n
(line break) is only a single char and so is\\
(backslash).\\n
is just a backslash followed byn
.'n'
:不是字符串,是一个文字char,一个字节,字母n的字符代码。"n"
:字符串,两个字节,一个表示 n,一个表示每个字符串末尾的空字符。"\n"
:两个字节 \n 代表“换行”,它占用一个字节,加上一个空字符字节。'\n'
:与第一个相同,文字字符,不是字符串,一个字节。"\\n"
:三个字节.. 一个用于 \,一个用于换行符,一个用于空字符""
:一个字节,只是空字符。'n'
: is not a string, is a literal char, one byte, the character code for the letter 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 one for the null character""
: one byte, just the null character.char
占用一个字节。'
的文字是 char 文字;使用"
的文字是字符串文字。\
是转义字符和\n
是换行符,将它们放在一起,您应该能够弄清楚。
char
, by definition, takes up one byte.'
are char literals; literals using"
are string literals.\
is the escape character and\n
is a newline character.Put these together and you should be able to figure it out.
以下内容将在内存中占用x个连续字符:
编辑:格式化固定
编辑2:我写了一些非常愚蠢的东西,感谢Mooing Duck指出这一点。
The following will take x consecutive chars in memory:
edit: formatting fixed
edit2: I've written something very stupid, thanks Mooing Duck for pointing that out.
'n'
->一个字符
。char
始终为 1 个字节。这不是一个字符串。“n”
->一个字符串文字,包含一个n
和一个以 NULL 结尾的char
。所以 2 个字节。'\n'
->一个char
,一个char
始终是1个字节。这不是一个字符串。“\n”
->一个字符串文字,包含一个\n
和一个以 NULL 结尾的char
。所以 2 个字节。“\\n”
->一个字符串文字,包含一个\
、一个 '\n' 和一个以 NULL 结尾的char
。所以 3 个字节。""
->一个字符串文字,包含一个终止 NULLchar
。所以1个字节。'n'
-> Onechar
. Achar
is always 1 byte. This is not a string."n"
-> A string literal, containing onen
and one terminating NULLchar
. So 2 bytes.'\n'
-> Onechar
, Achar
is always 1 byte. This is not a string."\n"
-> A string literal, containing one\n
and one terminating NULLchar
. So 2 bytes."\\n"
-> A string literal, containing one\
, one '\n', and one terminating NULLchar
. So 3 bytes.""
-> A string literal, containing one terminating NULLchar
. So 1 byte.字符串占用的字节数等于字符串中的字符数加 1(终止符),再乘以每个字符的字节数。每个字符的字节数可能会有所不同。对于常规
char
类型来说,它是 1 个字节。您的所有示例都是一个字符长,除了倒数第二个字符(两个)和最后一个字符(零)之外。 (有些是
char
类型,并且仅定义单个字符。)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.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.)您似乎指的是字符串常量。并将它们与字符常量区分开来。
在所有架构上,
char
都是一个字节。字符常量使用单引号分隔符'
。字符串是连续的字符序列,尾随 NUL 字符来标识字符串的结尾。字符串使用双引号字符 '"'。
此外,您还介绍了 C 字符串常量表达式语法,它使用黑斜杠来指示特殊字符。
\n
是字符串常量中的一个字符。因此,对于示例 <代码>'n', "n", '\n', "\n":
'n'
是一个字符"n"
是一个只有一个字符的字符串,但它需要存储两个字符(一个用于字母n
,一个用于 NUL'\n'
是一个字符,换行符(基于 ASCII 的系统上按 ctrl-J)"\n"
是一个字符加一个 NUL。我让其他人根据这些来思考。
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'
.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 '"'.
Also, you introduce the C string constant expression syntax which uses blackslashes to indicate special characters.
\n
is one character in a string constant.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 lettern
and one for the NUL'\n'
is one character, the newline (ctrl-J on ASCII based systems)"\n"
is one character plus a NUL.I leave the others to puzzle out based on those.
取决于是否使用 UTF8,字符为 1 个字节,如果使用 UTF16,字符为 2 个字节,字节是 00000001 还是 10000000 都无关紧要,一旦声明用于初始化,就会注册并保留一个完整字节,并且如果字符发生更改,则此寄存器会更新新的价值。
字符串字节数等于“”之间的字符数。
示例:11111111 是一个填充字节,
UTF8 char T = 01010100(1 个字节)
UTF16 char T = 01010100 00000000(2 个字节)
UTF8 字符串“编码”= 011000110110111101100100011010010110111001100111(6 个字节)
UTF16 字符串“编码”= 011000110000000001101111000000000110010000000000011010010000000001101110000000000110011100000000(12字节)
UTF8\n= 0101110001101110 (2 字节)
UTF16 \n = 01011100000000000110111000000000 (4 字节)
注意:您键入的每个空格和每个字符在编译器中都会占用 1-2 个字节,但空间如此之多,除非您为计算机或游戏控制台键入代码从 90 年代初开始,您的内存大小为 4mb 或更少不应该担心字符串或字符的字节。
对内存有问题的事情是调用需要使用浮点数、小数或双精度进行大量计算的事情,以及在循环或更新方法中使用数学随机。最好在运行时运行一次或在固定时间更新时运行一次,并在时间跨度上取平均值。
Depends if using UTF8 a char is 1byte if UTF16 a char is 2bytes doesn't matter if the byte is 00000001 or 10000000 a full byte is registered and reserved for the character once declared for initialization and if the char changes this register is updated with the new value.
a strings bytes is equal to the number of char between "".
example: 11111111 is a filled byte,
UTF8 char T = 01010100 (1 byte)
UTF16 char T = 01010100 00000000 (2 bytes)
UTF8 string "coding" = 011000110110111101100100011010010110111001100111 (6 bytes)
UTF16 string "coding" = 011000110000000001101111000000000110010000000000011010010000000001101110000000000110011100000000 (12 bytes)
UTF8 \n = 0101110001101110 (2 bytes)
UTF16 \n = 01011100000000000110111000000000 (4 bytes)
Note: Every space and every character you type takes up 1-2 bytes in the compiler but there is so much space that unless you are typing code for a computer or game console from the early 90s with 4mb or less you shouldn't worry about bytes in regards to strings or char.
Things that are problematic to memory are calling things that require heavy computation with floats, decimals, or doubles and using math random in a loop or update methods. That would better be ran once at runtime or on a fixed time update and averaged over the time span.