Input and output of character arrays in C language

There are two ways to input and output character arrays:
(1) Input and output character by character. Use the format character "%c" to input or output a character. For example

int main()
{
    
    
	char c[6];   //定义一个字符串
	for (int i = 0; i < 5; i++)
	{
    
    
		scanf("%c", &c[i]);  //输入字符串
	}
	for (int i = 0; i < 5; i++)
	{
    
    
		printf("%c", c[i]); //输出字符串
	}
	return 0;
}

(2) Input or output the entire string at once. Using the "%s" format character means input and output of strings. For example

int main()
{
    
    
    //char str[]={"China"};
	char str[6];
	scanf("%s", str);
	printf("%s", str);
	return 0;
}

The storage condition of the array in the memory is:
Please add a picture description
when output encounters '\0', the output will stop.
Explanation:
(1) The output character does not contain the terminator '\0'.
(2) When the "%s" format character is used to output a string, the output item in the printf function is the character array name, not the array element name.
(3) If the length of the array is greater than the actual length of the string, it will only be output until it encounters '\0'. For example

char c[10]={
    
    "China"}; //字符串长度为5,连'\0'一共占6个字节
printf("%s",c);

Only output the valid characters "China" of the string instead of outputting 10 characters. That's the benefit of terminating flags with strings.
(4) If a character array contains more than one '\0', it will end when the first '\0' is encountered. For example

int main()
{
    
    
	char str[10] = {
    
    "China\0boy"};
	printf("%s", str);
	return 0;
}

Please add a picture description
(5) You can use the scanf function to input a string. scanf("%s",c);
The input item c in the scanf function is the name of the defined character array, and the input string should be shorter than the length of the defined character array. For example,
it is defined. char c[6];
Entering China from the keyboard,
the system will automatically add a '\0' terminator after China. If you use a scanf function to input multiple strings, you should separate them with spaces. For example

char str1[5],str2[5],str3[5];
scanf("%s%s%s",str1,str2,str3);

Input data: How are you?
Enter as three strings due to space character separation. After the input, the status of the str1, str2, str3 arrays is as follows:
Please add a picture description
the value of the unassigned element in the array is automatically set to '\0'. If changed to

char str[13];
scanf("%s",str);

If you enter How are you?
Since the system regards the space character as the separator between the input strings, only the character "How" before the space is sent to str. Treat "How" as a string, so add '\0' after it. The array state of str is
Please add a picture description
Note: If the input item in the scanf function is a character array name, do not add the address symbol &, because in C language, the array name represents the address of the first element of the array (or the starting point of the array) origin address) .
The following figure is an example: what this figure represents is a character array, if the array occupies 6 bytes. The array name c represents the address 2000.
You can use this printf("%o",c); //八进制形式输出output statement to get the address of the first element of the array, that is, the starting address of the array.
Please add a picture description
(6) The method of outputting the string mentioned above: printf("%s",c);
it is actually executed as follows: find the address of the first element of the array according to the character array name c, and then output the characters one by one until '\0' is encountered .

Guess you like

Origin blog.csdn.net/NuYoaH502329/article/details/127797561