C Language Fundamentals: Character Arrays and Strings

        In this section we will learn about character arrays and strings. We know that when defining an array, we need to specify the corresponding variable type for it. That is, after defining the type of an array, all variables in the array are of the same type. Now let's define a character array, as follows:

char hello[5] = {'H','e','l','l','o'};

        In this way, we define an array of character type. There is no problem with this definition of variables of array type, but there are two shortcomings:

1. The definition process is cumbersome. When initializing the array variable, you need to write out each character one by one, and add a pair of single quotation marks to both ends of each character.

2. When displaying a character array, we usually want to be able to display it at one time instead of displaying these characters one by one by looping through the array.

        For example, for the array defined above, if we want to use printf to display it, we need to loop:

char hello[5] = {'H','e','l','l','o'};
for (int i = 0; i < 5; i++)
{
	printf("%c", hello[i]);
}

Hello

        In fact, the C language supports the way of directly defining strings to define character arrays, for example:

char hello[6] = "Hello";
printf("%s", hello);

Hello

        Note that when we define this array, we specify that the size of the array is 6, which means that we need to apply for 6 bytes of storage space, but we only write 5 characters in the double quotes to the right of the equal sign. This is not a typo. C language stipulates that when using double quotes to define a string, '\0' is used as the end marker of the string, which means that the string has a total of 6 characters, namely 'H', 'e ', 'l', 'l', 'o', '\0'. About \0 readers only need to know that this is a special character, it indicates the end of the string, and its value is 0. In this way, we can print this string directly through the printf function. It should be noted that when using printf to display a string, we need to ensure that the last end character in the string variable is \0. If the end character is not \0, printf will not know when it displays the string. where does it end, e.g.

char hello[5] = "Hello";
printf("%s", hello);

Hello#@($0948#."

        也就是说,我们虽然定义了一个字符串,但只定义了5个字符的长度,而使用双引号为其初始化时,这个字符串的实际大小应该是6因为要追加一个\0,但我们在定义数组大小时只指定其大小是5所以这个数组中的中只有5个元素分别是'H'、'e'、'l'、'l'、'o'。而在'o'之后并没有'\0',所以在printf显示这个字符串时,显示 Hello之后,并不知道何时结束,所以就会继续显示,直到遇到一个'\0'为止,于是我们程序就出现了非预期的结果。这样的程序并不是我们提倡的,我们在定义字符串时一定要为其多分配一个字符的长度,以便存放字符串的结束符'\0'。当然,使用双引号定义的字符串可以像使用普通数组一样使用其数组元素例如ABC运行结果可以看到直接显示字符串和循环显示字符数组中的元素的效果是一样的。

char str[6] = "Hello";
printf("%s\n", str);

for (int i = 0; i < sizeof(str); i++)
{
    printf("%c", str[i]);

}
printf("\n");

Hello
Hello

        在这里我们使用了另一个重要函数sizeof(),它的作用是可以计算出某一个变量或类型的大小,就是占用的内存字节数,例如:

char ch;
short sh;
int i;
printf("%d %d %d %d %d\n", 
        sizeof(ch), 
        sizeof(sh), 
        sizeof(i), 
        sizeof(long), 
        sizeof(long long));

1 2 4 8 8

        请注意,long类型的变量在不同的操作系统下占用字节数可能不同,请参数参见《变量大小》。另外在使用sizeof来计算数组大小时,计算的是数组一共占用内存的字节数,而不是数组的元素元素的个数,例如:

int array[4];
printf("%d\n", sizeof(array));

        上面例子的结果是16而不是4。


欢迎关注公众号:编程外星人

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325727413&siteId=291194637