C++ character array input and output

There are two ways to input and output character arrays:

1) Input and output character by character.

2) Input or output the entire string at a time.For example, the following program segments:

 char str [20];
  cin>str;//input string with character array name
  cout<str;//output string with character array name

Enter a string at runtime,Such as:


In memory,The state of the array str is shown in Figure 5.9. A terminator '\ 0' is automatically added after the five characters.

During output, the characters are output one by one until the end character '\ 0' is encountered, and the output is stopped.The output is:


As mentioned before,The character array name str represents the address of the first element of the character array,The process of executing "cout<< str;" is to output characters one by one starting from the first element of the array pointed to by str,Until you encounter '\ 0'.

Please note the following about the input and output of the character array:

The output characters do not include the terminator '\ 0'.

When outputting a string,cout stream uses character array names,Instead of array element names.

If the length of the array is greater than the actual length of the string,It is only output to the end of encounter '\ 0'.

If a character array contains more than one '\ 0', the output ends when the first '\ 0' is encountered.

When typing a string from the keyboard into the computer with cin,The string entered from the keyboard should be shorter than the length of the defined character array,Otherwise problems will occur.

C++ provides the getline function in the cin stream for reading a line of characters (or the first few characters in a line of characters), which is safe and convenient to use.

C++ string and end-of-string flags

Use a character array to store the characters in a string.Such as:

 char str [12]={"i", "", "a", "m", "", "h", "a", "p", "p", "y"};

The one-dimensional character array str is used to store the characters in a string "i am happy".The actual length of the string (10) is not equal to the length of the array (12). In addition to storing the above 10 characters,The system automatically fills the empty character '\ 0' with the last two elements of the character array.

To determine the actual length of a string,C++ specifies a "end of string character", represented by the character '\ 0'. In the above array,The 11th character is '\ 0', which means that the valid characters of the string are the first 10 characters. That is,When the character '\ 0' is encountered, the string ends here.A string consisting of the characters preceding it.

For a string constant,The system will automatically add a '\ 0' as the terminator after all characters.For example, the string "i am happy" has 10 characters in total, but it occupies a total of 11 bytes in memory. The last byte '\ 0' is automatically added by the system.

In the program, it is often determined by detecting the position of '\ 0' to determine whether the string ends.Instead of determining the length of the string based on the length of the array.Of course, you should estimate the actual string length when defining a character array,Ensure that the length of the array is always greater than the actual length of the string.If multiple strings of different lengths are stored in a character array,You should make the array longer than the longest string.

Explanation:'\ 0' is just a sign for identification.

If you output a string with:

  how do you do?″;

The system outputs characters one by one when executing this statement,So how does it determine which character should be output and stop?

Let's add another method to the character array initialization:initialize the character array with string constants.E.g:

 char str []={"i am happy"};

You can also omit the curly braces,Write directly

 char str []="i am happy";

Instead of using a single character as the initial value,Instead, use a string (note that the two ends of the string are enclosed in double apostrophes, not single apostrophes) as the initial value.Obviously, this method is intuitive,Convenient and in line with people's habits.Note:The length of the array str is not 10, but 11 (because the end of the string constant is added by the system with a '\ 0'). Therefore, the above initialization is equivalent to the following initialization:

 char str []={"i", "", "a", "m", "", "h", "a", "p", "p", "y", "\ 0"};

Is not equivalent to:

 char str []={"i", "", "a", "m", "", "h", "a", "p", "p", "y"};

The former has a length of 11 and the latter has a length of 10. If there is

 char str [10]="china";

The first 5 elements of the array str are 'c', 'h', 'i', 'n', 'a', the sixth element is '\ 0', and the last 4 elements are null characters.See picture.

It should be noted that the character array does not require its last character to be '\ 0', and may not even contain '\ 0'. It is perfectly legal to write something like this:

 char str [5]={"c", "h", "i", "n", "a"};

Whether you need to add '\ 0' is completely determined according to your needs.But because the C++ compilation system automatically adds a '\ 0' to the string constant. Therefore, in order to make the process consistent,Easy to determine the actual length of the string,And corresponding processing in the program,A '\ 0' is also added artificially after the valid characters in the character array. Such as:

 char str [6]={"c", "h", "i", "n", "a", "\ 0"};
  • Previous How to resolve VisualSVN Server installation prompt error Repositories is not a valid short file name
  • Next Detailed explanation of SQL Server2014 hash index principle