Assigning a string is more than one byte for assigning a character to the end of the string. The above array C in memory of the actual storage situation is: C Program\0 ' "is the C compiler system automatically added. Because of the ' I ' flag, it is generally not necessary to specify the length of the array when assigning an initial value to a string, but the system handles it itself. The input and output of the character array becomes simple and convenient after the string method is adopted. In addition to the above method of assigning an initial value to a string, you can also use the printf function and the scanf function to output a string in a character array without using a looping statement to enter and output each character individually.
void Main ()
{
static char c[]= "Basic\ndbase";
printf ("%s\n", c);
}
Note in the printf function of this example, the format string used is "%s", which indicates that the output is a string. The array name in the output table column is available. cannot be written as: printf ("%s", c[]);
void Main ()
{
Char st[15];
printf ("Input string:\n");
scanf ("%s", ST);
printf ("%s\n", ST);
}
In this example, because the array length is defined as 15, the length of the string must be less than 15 to allow a byte to be used to hold the string end flag '. It should be explained that for an array of characters, if you do not initialize the assignment, you must specify the length of the array. It should also be noted that when you enter a string with the scanf function, the string cannot contain spaces, or a space is used as the end character of the string. For example, run example 4.8, when the input string contains spaces, the operation is: the input string:this is a book this from the output results can be seen after the space after the characters are not output. In order to avoid this situation, you can set up several character array segments to hold a string with spaces. The program can be rewritten as follows:
Lesson
void Main ()
{
Char st1[6],st2[6],st3[6],st4[6];
printf ("Input string:\n");
scanf ("%s%s%s%s", ST1,ST2,ST3,ST4);
printf ("%s%s%s%s\n", ST1,ST2,ST3,ST4);
}
This program is set up four arrays, the input of a line of characters in the space segment loaded into four arrays respectively. The strings in these four arrays are then output separately. As described earlier, the entries for scanf must appear in address mode, such as &a,&b. But in example 4.8, it appears as an array name, why? This is because in the C language, the array name represents the first address of the array. The entire array is a contiguous unit of memory that begins with the first address. If there is a character array char c[10], the memory can be expressed as shown in Figure 4.2. Set the first address of array C to 2000, that is, the c[0] cell address is 2000. Then the array name C represents the first address. Therefore, the address operator & cannot be added before C. such as writing scanf ("%s", &c); it is wrong. When the function printf ("%s", c) is executed, the first address is found by array name C, and each character in the array is output, until the string Terminator flag ' is ' is encountered.
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.