1. Definition of character array:
An array used to store the character quantity is called a character array.
The arrays of formal values are the same. For example:
Char C [10];
The generic and integer types can also be defined as int C [10], but each array element occupies 2 bytes of memory.
The character array can also be a two-dimensional or multi-dimensional array. For example:
Char C [5] [10];
It is a two-dimensional character array.
2. character array Initialization
The first method is to assign values to each element:
Character Arrays can also be initialized and assigned values during definition. For example:
Char C [10] = {'C', '', 'P', 'R', 'O', 'G', 'R', 'A ', 'M '};
The values of each element after the assignment are:
The value of C [0] Is 'C'
The value of C [1] is''
The value of C [2] is 'P'
The value of C [3] is 'R'
The value of C [4] Is '0'
The value of C [5] is 'G'
The value of C [6] is 'R'
The value of C [7] is 'A'
The value of C [8] Is 'M'
C [9] is not assigned a value, which is automatically assigned a value of 0 by the system. You can also assign an initial value to all elements.SaveLength description. For example:
Char C [] = {'C', '', 'P', 'R', 'O', 'G', 'R', 'A ', 'M '};
The length of the C array is automatically set to 9.
In addition, in the second method, the C language allows the array to be initialized and assigned a value using a string. For example:
Char C [] = {'C', '', 'P', 'R', 'O', 'G', 'R', 'A ', 'M '};
It can be written as follows:
Char C [] = {"C program "};
Or remove {} and write it:
Char C [] = "C program ";
3. References to character Arrays
Character arrays are referenced by subscripts, just like normal arrays.
4. Input and Output of character Arrays
After the string mode is used, the input and output of character arrays become simple and convenient. In addition to the above method of assigning initial values with strings, the printf and scanf functions can also be used to output the strings in an array of characters at a time, without having to use loop statements to input and output each character one by one.
Example 1:
# Include <stdio. h>
Intmain (void ){
Char C [] = "Basic \ ndbase ";
Printf ("% s \ n", C );
Return 0;
}
Note that in the printf function in this example, the format string is "% s", indicating that the output is a string. The array name is provided in the output table column. The value cannot be printf ("% s", C []).
Example 2:
# Include <stdio. h>
Int main (void ){
Char st [15];
Printf ("input string :");
Scanf ("% s", St );
Printf ("your string is: % s \ n", St );
Return 0;
}
In this example, the length of the array is defined as 15, so the length of the input string must be less than 15, to leave a byte for storing the string ending sign '\ 0 '. It should be noted that for an array of characters, if no initialization value is assigned, the length of the array must be specified. Note that,When the scanf function is used to input a string, the string cannot contain spaces. Otherwise, spaces are used as the end character of the string.
For example, when the input string contains spaces, the running condition is:
Input string: this is a book
Output:
Your string: This
From the output result, we can see that all characters After spaces are not output. To avoid this situation, you can set several character arrays to store spaces in segments. The program can be rewritten as follows:
# Include <stdio. h>
Int main (void ){
Char ST1 [6], st2 [6], st3 [6], st4 [6];
Printf ("input string :");
Scanf ("% S % s", ST1, st2, st3, st4 );
Printf ("your string: % S % s \ n", ST1, st2, st3, st4 );
Return 0;
}
In this program, four arrays are set, and the input space segments of one line of characters are loaded into four arrays respectively. Then output the strings in the four arrays respectively.
5. Relationship between strings and character Arrays
There is no special string variable in C language, and a character array is usually used to store a string. When we first introduced a String constant, it indicates that the string always uses '\ 0' as the end character 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.
The C language allows the array to be initialized and assigned a value using a string. For example:
Char C [] = {'C', '', 'P', 'R', 'O', 'G', 'R', 'A ', 'M '};
It can be written as follows:
Char C [] = {"C program "};
Or remove {} and write it:
Char C [] = "C program ";
The value assignment in string mode occupies one byte more than the value assignment in characters one by one. It is used to store the string ending sign '\ 0 '. The actual storage of array C in the memory is as follows:
'\ 0' is automatically added by the C compilation system.Because the '\ 0' flag is used, you do not need to specify the length of the array when you use the string to assign the initial value.