End sign of the string in C ++ \ 0

Source: Internet
Author: User

\ 0 is the end sign of the string in C ++. It is stored at the end of the string. Although it is not counted as the string length, it occupies a byte of memory space. View the \ 0 entry in Baidu encyclopedia. The ending mark of the string specified in c/c ++ is '\ 0 '. Some people may think that in C Language (C ++ will be different), '\ 0' is not a struct type, but an int type. Here, we should be consistent with the author of the Baidu entry and think that \ 0 and '\ 0' are equivalent. '\ 0' is not necessarily 00000000 of 8-bit because different processors have different digits. In fact, sizeof (int) returns different results because of different processor bits, while sizeof (char) returns 1 results. For 8-bit machines, A byte is composed of 8 digits, and a 16-bit host is composed of 16 digits. The computer we usually use is usually 32 bits, that is, a byte is composed of 32 bits, now it is a 64-bit server. The CPU generally reads data in bytes. However, we generally agree that 1 byte equals 8 bits, because this is the minimum number of digits.

'\ 0' is an escape character, which means to tell the program that this is not a number 0. '\ 0' and 0 can basically be used, for example, string [I]! = '\ 0' and string [I]! = 0 is the same. However, the '\ 0' type is char type, while the 0 type is int type. Therefore, in most computers, sizeof (0) = 4 and sizeof (' \ 0') = 1, this is not applicable in special cases. In addition, '\ 0' and '0' are different. They are all characters, but their ASCII code is different:' \ 0' the ASCII code value is 0, '0' can also be written as '\ 0x30'. the ASCII code value is 48.

There is no special string variable in C language, and a character array is usually used to store a string. The string always uses '\ 0' as the end of the string. Therefore, when a string is saved to an array, the terminator '\ 0' is also saved to the array and used as a flag for whether the string ends. With the '\ 0' sign, you do not need to use the length of the character array to determine the length of the string.

'\ 0' indicates the end mark of the string. For example, assign a string to an array: char str1 [] = {"Welcome! "}. In fact, the actual storage of the array str1 in the memory is: 'w''' e ''l''c' o ''m'' e ''! ''\ 0 '. The '\ 0' after this is automatically added by the C compilation system. Therefore, when you use a string to assign an initial value, you do not need to specify the length of the array, but the system will handle it by itself. Copy the string in character array str1 to character array str2. The string end flag '\ 0' is also copied together.

But ...... Some exceptions also occur. For example, suppose we have specified the length of the array but the length of the array is not enough.

For example, char str1 [8] = {"Welcome! "}. The length of str1 in the character group is 8, so the subsequent information is lost, that is, '\ 0.

In addition, if each character is enclosed in quotation marks when an array is assigned a value, '\ 0' is also lost '.

Example: char str1 [] = {'w', 'E', 'l', 'C', 'O', 'M', 'E ','! '};

If you want the array to end with '\ 0', you can either write it as char str1 [] = {"Welcome! "};

Either write (manually add '\ 0'): char str1 [] = {'w', 'E', 'l', 'C', 'O ', 'M', 'E ','! ',' \ 0 '};

Either write (intentionally reserve a blank space for the array): char str1 [9] = {'w', 'E', 'l', 'C', 'O ', 'M', 'E ','! ',' \ 0 '};

Finally, if you are interested in shoes, you can think about the difference between '\ 0' and NULL. You will find that we still haven't noticed many details in the C language.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.