[C Programming Tutorial Experiment] Basic Exercises (4)

Code 1: getchar()

//getchar()
//按照如下输入格式(其中<CR>代表回车,注意 回车也是一个字符)则输出结果是? 
//12<CR>
//34<CR> 
#include <stdio.h>
int main()
{
    
    
	char a,b,c,d;
	//printf("请输入:\n");
	scanf("%c%c",&a,&b);
	c=getchar();
	d=getchar();
	
	printf("输出如下:\n");
	printf("%c%c%c%c\n",a,b,c,d);
}

Output:
insert image description here
Why does the above result occur? Where did the entered 4 go?

注意:The function getchar() has no parameters, and the return value of the function is the character read from the terminal keyboard, and only one character can be read at a time. The characters entered by the user are stored in the keyboard buffer, including the carriage return '\n'. However, for the scanf function, '\n' will trigger scanf to read the contents of the input buffer, but will stop reading when '\n' or a space ' ' is encountered, while getchar will directly read '\n' and a space.

You can add the following test statement:

#include <stdio.h>
int main()
{
    
    
	char a,b,c,d;
	//printf("请输入:\n");
	scanf("%c%c",&a,&b);
	c=getchar();
	d=getchar();
	
	printf("输出如下:\n");
	printf("%c%c%c%c\n\n",a,b,c,d);
	
	printf("-------------------------\n\n");
	printf("测试:\n\n");
	printf("a=%c\n\n",a);
	printf("b=%c\n\n",b);
	printf("c=%c\n\n",c);
	printf("d=%c\n\n",d);	
}

The result is:

That is to say, after the user enters 12 and the carriage return, the ''c=gerchar();' statement has read the carriage return character, so naturally, the 3 entered by the user is ''d=getchar();' ' read by this statement.

So the result of this question is as seen,12 回车符(看不见,即换行了)3(在下一行)
insert image description here

Code 2: Output character 'A'

//以下不能输出字符A的语句是?
//已知字符A的ASCII码值为65,字符a的ASCII码值为97 
#include <stdio.h>
int main()
{
    
    
	printf("%c\n\n",'a'-32);//97-32=65 输出整型数字65对应的字符A 
	printf("%d\n\n",'A'); //显然输出为字符A对应的整型数字65 
	printf("%c\n\n",65);//输出整型数字65对应的字符A 
	printf("%c\n\n",'B'-1);//66-1=65 输出整型数字65对应的字符A 
 } 

output:

The reason is obvious:
insert image description here

Code 3: Integer 48 corresponds to the character '0' ----- Memory common integer and character size conversion

//若运行程序时从键盘输入 48<回车>,则输出结果是? 
#include <stdio.h>
int main()
{
    
    
	char c1,c2;	//48~57对应字符'0'到'9' 
	scanf("%d",&c1); //需要知道48对应的字符为'0' 
	c2=c1+9; //那么,48+9=57,故c2='9' 
	printf("%c%c\n\n",c1,c2); //故输出 09
} 

output:

Notes have been explained. Also, 字符'A' ~ 'Z' 对应 整型 65 ~ 90. 字符'a' ~ 'z' 对应 整型 97 ~ 122, each lowercase letter is 32 larger than its uppercase counterpart. 字符'0' ~ '9' 对应 整型 48 ~ 57. This is the basic knowledge that beginners should memorize.
insert image description here

Code 4: getchar() and putchar()

//输入 YES<回车> 则结果是?
#include <stdio.h>
int main()
{
    
    
	char a,b,c;
	a=getchar();//从键盘输入一个字符,送给字符变量a 
	b=getchar();//从键盘输入一个字符,送给字符变量b 
	c=getchar();//从键盘输入一个字符,送给字符变量c 
	putchar(a);//输出变量a的值 
	putchar(b);//输出变量b的值
	putchar(c);//输出变量c的值
	putchar('\n');//换行 
	return 0;
 } 

Output:
insert image description here
简析: After continuously inputting "YES" and pressing the Enter key, the characters are sent to the computer, and then 3 characters of "YES" are output.

说明: When entering information with the keyboard, instead of pressing a key on the keyboard once, the corresponding character will be entered into the computer immediately. The input characters are temporarily stored in the keyboard buffer, and only when the Enter key is pressed, they are input into the computer together, and then assigned to the corresponding variables in sequence.

If: when running this program, enter a character and press Enter immediately, what will be the result? In this example, if you press the Enter key immediately after entering the character Y, and then press the Enter key immediately after entering the character E, Y and E will be output respectively. As shown below:
insert image description here
Here, instead of a character Y, the first line input is two characters: Y和换行符, where the character Y is assigned to the variable a, and the newline character is assigned to the variable b. Line 2 then enters two characters, E and a newline, where the character E is assigned to the variable c, 换行符没有赋给任何变量. When using the putchar() function to output the values ​​of the variables a, b, and c, the character Y is output, then a newline is output, and then the character E is output, and then putchar('\n') is executed to wrap the line.

Also note that executing the getchar() function can not only obtain a displayable character from the input device, but also obtain characters that cannot be displayed on the screen, such as control characters.

Code 5: Domain width

//执行以下程序,输出结果是? 
#include <stdio.h>
int main()
{
    
    
	int x=12;
	double y=3.1415926;
	printf("%d%10.6f\n",x,y);
		
}  

The output is:
insert image description here
In the '' %10.6f'' format control character, 10 represents 域宽, including the decimal point ".", 6 represents the number of digits after the decimal point, since the seventh digit is 6, so 第6位四舍五入为3.

Code 6: still getchar()

//还是getchar()
//已知输入格式为   4空格5回车
//                 67回车
//则输出结果是? 
#include <stdio.h>
int main()
{
    
    
	char a,b,c,d,e;
	scanf("%c%c",&a,&b);//注意这里输入的是字符型数据,空格也会被作为字符
	c=getchar();
	d=getchar();
	e=getchar();
	
	printf("%c%c%c%c%c\n",a,b,c,d,e);
} 

Output:
insert image description here
简析: Pay attention when inputting character data with the scanf() function. If the input character is declared in the %c format, the space character and escape character are both input as valid characters, so the space between 4 and 5 will be assigned. give b. Second, be aware of the difference between numbers and numeric characters. Finally, the getchar() function can accept the enter key, which is also entered as a character.

So, a is '4', b is space, c is '5', d is carriage return (line feed effect), and e is '6'. (7 is not assigned to any variable) is the effect of the output.

Guess you like

Origin blog.csdn.net/qq_44731019/article/details/123588603