How many bytes does a string occupy? A char?

I am doing a review of my first semester C ++ class, and I think I'm missing something. How many bytes does a string occupy? A char?

Here are some examples, some of which are character literals, and some are strings:

'n', "n", '\n', "\n", "\\n", ""

I am particularly confused by the use of newlines.

+6
source share
8 answers
 #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.

\n (line break) is just one char, and also \\ (backslash). \\n is just a backslash followed by n.

+24
source
  • A char , by definition, takes up one byte.
  • Literals using ' are char literals; literals using " are string literals.
  • The string literal implicitly ends with zero, so it will take another byte than the observed number of characters in the literal.
  • \ is the escape character, and \n is the newline character.

Put them together and you can figure it out.

+5
source

X consecutive characters will be stored in memory:

 '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 wrote something very stupid, thanks Mooing Duck for pointing this out.

+3
source
  • 'n' : not a string, is a char literal, one byte, ascii code for the character n.
  • "n" : a string, two bytes, one for n and one for a null character, each line has an end.
  • "\n" : two bytes like \ n mean a "new line" that takes one byte, plus one byte for a null char.
  • '\n' : same as the first, char literal, not a string, one byte.
  • "\\n" : three bytes .. one for \, one for a new line and a null character
  • "" : one byte, only a null character.
+2
source

The number of bytes the string occupies is equal to the number of characters in the string plus 1 (terminator) times the number of bytes per character. The number of bytes per character may vary. This is 1 byte for the regular char type.

All your examples are one character long, except for the second, which lasts two, and the last, which is zero. (Some of them are of type char and define only one character.)

+1
source

'n' → One char . A char always 1 byte. This is not a string. "n" → A string literal containing one n and one trailing NULL char . So, 2 bytes.
'\n' → One char , A char always 1 byte. This is not a string. "\n" → A string literal containing one \n and one trailing NULL char . So, 2 bytes.
"\\n" → A string literal containing one \ , one '\ n' and one trailing NULL char . So, 3 bytes.
"" → A string literal containing one trailing NULL char . So, 1 byte.

+1
source
 'n' - 0x6e "n" - 0x6e00 '\n' - 0x0a "\n" - 0x0a00 "\\n" - 0x5c6e00 "" - 0x00 
0
source

You seem to be referring to string constants. And distinguishing them from symbolic constants.

A char is one byte on all architectures. The character constant uses the single quote delimiter ' .

A string is a continuous sequence of characters with a trailing NUL character to identify the end of a string. The string uses double quotation marks "".

In addition, you introduce the syntax for expressing the constant of a string C, which uses a black mark 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 memory (one for the letter n and one for NUL
'\n' - one character, new line (ctrl-J for ASCII based systems)
"\n" is one character plus NUL.

I leave the rest to puzzles based on these.

0
source

Source: https://habr.com/ru/post/909012/


All Articles