What are the considerations for character arrays representing strings?

青灯夜游
Release: 2020-07-29 15:34:58
Original
3345 people have browsed it

Notes: 1. If a character array of unspecified length is used to store characters, and the terminator is not specified, then this is not a string. 2. If there is a number 0 or '\0' in the middle of the character array, then when using the string output function, it will end before 0, and anything after 0 will not be output.

What are the considerations for character arrays representing strings?

There is no c string data type in the C language. Instead, a character array is used to simulate a string. The string ends with '\0', that is, 0. symbol.

It should be noted that:

1. If a character array of unspecified length is used to store characters and the end character is not specified, then this is not String.

For example, char buff1[ ]={'a','b','c'}; after printing through printf("%s",buff1), there will be a string of garbled characters after abc. If after abc If the character is followed by the number 0, or '\0', then this is a string, which can be printed normally, in the form: char buff[] = {'a','b','c',0} or char buff[ ] = { 'a','b','c','\0'}.

2. If a character array of specified length is used to initialize the string, when the number of characters is less than the specified length, the format is: char buff[100] = { 'a','b','c' }, then this is a string

Because the character array in this case will default the remaining unspecified characters to 0, which is equivalent to having a terminator, and this character array can be output as a string Function

However, if you just declare an array of a specified size without initializing it, and then assign characters to it without specifying the end character 0, then it will be garbled when printed using the string number function, and it is not a String.

So, we often set it to 0 when initializing the character array, in the form: char buff2[100]={0}.

3. If there is a number 0 or '\0' in the middle of the character array, then when using the string output function, it will end before 0, and anything after 0 will not be output.

Recommended: "c Language Tutorial"

The above is the detailed content of What are the considerations for character arrays representing strings?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!