Coding Cultivation Series---User Interface Processing Essentials

1. Make sure that the variable storing the input value is large enough

  • In char name[10], you can enter a maximum of 10 characters. When the input value is more than 10 characters, a problem will occur.
  • We should take a long-term view. For example, if we need to store 10 bytes, we can expand the length of the array to 30, 50 or even 100.
  • The same is true for other data types, a larger data type should be selected in advance for variable declaration, such as:
long int a;
long double b;

2. The conversion specifier and the number of parameters should be consistent

printf("输出%d和%d\n",num1,num2);

Insert picture description here

  • It must be consistent. I won’t say anything else, and the consequences will be serious.

3. Use the fgets() and sscanf() functions instead of the scanf function

  • When scanf() encounters a blank character or special character in the process of reading a string, it will stop reading. eg: 123.46, only 123 will be read at this time
  • If you want to read all the content you input, you have to use the fgets() function and the sscanf() function in turn
  • The fgets() function can read the entire line of content including special characters and blank characters, and save it to the buffer function of the standard input device, while the sscanf() function can read these content and store it in a variable
  • sscanf() means string scanf()
#include<stdio.h>
#include<stdlib.h>

int main()
{
    
    
	char console[80];//控制台一行字符串最大长度
	int num;//输入字符串
	
	fgets(console_line, sizeof(console_line), stdlin);//读入字符串
	sscanf(console_line,"%d",&number);//读入fgets()函数的字符串,特殊字符也可读

	...
	printf("%d\n", num);
	system("pause");
}
  • I hope everyone will work hard to cultivate their habit of using the fgets() and sscanf() functions

4. Use the fflush function to clear the standard input/output device buffer

#include<stdio.h>
#include<stdlib.h>

int main()
{
    
    
	int num1=1;
	int num2=0;
	int sum;
	
	printf("结果应该是:");
	sum=num1/num2;//故意出错,让num2为0
	
	printf("%d\n", sum);
	system("pause");
}
  • Declare the program, "The result should be:" This sentence will not be printed, according to the buffer principle
  • The fflush(stdout) function can force the buffer content to be forcibly transferred to the input and output devices
#include<stdio.h>
#include<stdlib.h>

int main()
{
    
    
	int num1 = 1;
	int num2 = 0;
	int sum;
	
	printf("结果应该是:");
	fflush(stdout);//将上一条语句强制输出
	
	sum = num1 / num2;//故意出错,让num2为0
	
	printf("%d\n", sum);
	system("pause");
}
  • Use the fflush(stdin) function to force the keyboard buffer to be cleared
#include<stdio.h>
#include<stdlib.h>

int main()
{
    
    
	int num1 = 1;
	int num2, sum;
	
	fflush(stdin)//强制清空作为标准输出设备的键盘缓冲
	
	scanf("%d", num2);
	
	sum = num1 / num2;//故意出错,让num2为0
	
	printf("%d\n", sum);
	system("pause");
}

5. Summary

Here mainly introduces the situation that should be paid attention to when inputting and outputting. There are many small details. You will encounter these problems in actual work, and you will understand at that time.
Manly demeanor, code cultivation, hahaha, how can we connect

Guess you like

Origin blog.csdn.net/weixin_43722052/article/details/110944747