浅谈常用输入函数

大部分输入函数都是从缓冲区中读取数据,当缓冲区中有数据时,直接读取,当缓冲区中没有数据时,你需要通过键盘将数据输入缓冲区中,再由各输入函数从缓冲区读取数据到变量中,然而有一些输入函数不管缓冲区中有没有数据,不会读取缓冲区中的数据。是一定 通过键盘读取的,如getch()函数。
//scanf函数基本用法:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <conio.h>   //getch()函数头文件。
void print1()
{
	char ch1,ch2;
	scanf("%c",&ch1);
	scanf("%c",&ch2);      //32是空格,10是换行符。
	//ch2 = getchar(); 
	printf("ch1 = %d\t,ch2 = %d\n",ch1,ch2);  //ch1 = 97,ch2 = 10,我们能发现scanf遇到空格,换行符,制表符结束输入,不会丢弃结束字符。
}
int main()
{
	print1();
	printf("hello...\n");
	system("pause");
	return 0;
}






#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <conio.h>   //getch()函数头文件。
void print2()
{
	char str1[1024];
	char str2[1024];
	char ch3;
	char ch2;
	char ch;                 //测试发现当输入的不是单独的一个字符时,当再次读取时,scanf会丢弃结束符,从第一个不是结束符的字符开始读取,遇到结束符结束读取。
	scanf("%s",str1);		//当输入一个字符时,用scanf要小心,当再次读取一个字符时,不会跳过结束符。
	scanf("%c",&ch);
	scanf("%c",&ch3);
	scanf("%s",str2);
	scanf("%c",&ch2);
	printf("%d\n",ch);
	printf("%d\n",ch3);
	printf("str1=%s\nstr=%s\n",str1,str2);
	printf("%d\n",ch2);
}
int main()
{
	print2();
	printf("hello...\n");
	system("pause");
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <conio.h>   //getch()函数头文件。
void print3()
{
	int a;
	int b;
	scanf("%d",&a);
	scanf("%d",&b);  //测试发现当读取的不是单独的一个字符时,当再次读取时,scanf会丢弃结束符,从第一个不是结束符的字符开始读取,遇到结束符结束读取。
	printf("a= %d\nb= %d",a,b);
}
int main()
{
	print3();
	printf("hello...\n");
	system("pause");
	return 0;
}




#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <conio.h>   //getch()函数头文件。
void print5()
{
	int a;
	char str[1024];
	scanf("%d",&a);
	scanf("%s",str);//测试发现当读取的不是单独的一个字符时,当再次读取时,scanf会丢弃结束符,从第一个不是结束符的字符开始读取,遇到结束符结束读取
	printf("a=%d\nstr=%s\n",a,str);
}
int main()
{
	
	print5();
	
	system("pause");
	return 0;
}





#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <conio.h>   //getch()函数头文件。
void print6()
{
	char str[1024];
	char ch;
	scanf("%[^\n]",str);  //用这种方法可以读取空格,遇到换行符才结束读取,但是不会丢弃换行符。
	scanf("%c",&ch);
	printf("str=%s,ch=%d",str,ch);
}
int main()
{
	print6();
	printf("hello...\n");
	system("pause");
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <conio.h>   //getch()函数头文件。
void print7()
{
	char ch1;
	char ch2;
	scanf("%c%*c",&ch1);
	scanf("%*c");    //从缓冲区中读取一个字符,并丢弃该字符,常用来及时吸收缓冲的回车字符。
	scanf("%c",&ch2);
	printf("ch1 =%c,ch2 = %c\n",ch1,ch2);
}
int main()
{
	print7();
	printf("hello...\n");
	system("pause");
	return 0;
}




//gets()和getchar()函数基本用法:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>//getch()函数头文件。
void print4()
{
char ch;
char ch2;
char str[1024];
gets(str);//测试发现gets函数可以读取空格,遇到换行符结束并且会将换行符丢弃。
ch= getchar();//读取一个字符。缓冲区中一个接一个的读取,不管读取任何字符都不丢弃。
ch2=getchar();
printf("str=%s\nch=%d\n",str,ch);
printf("ch2 =%d\n",ch2);
}
int main()
{
print4();
printf("hello...\n");
system("pause");
return0;
}






//getch()函数用法:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>//getch()函数头文件。
void print8()
{
char ch1;
char ch2;
char ch3;
ch1=getchar();
ch2=getchar();
ch3=_getch();//系统提示要用标准函数。
printf("ch1=%c,ch2=%c,ch3=%c\n",ch1,ch2,ch3);//通过测试发现getch函数不是从缓冲区读取数据,而是从键盘输入读取,虽然缓冲区刚开始有数据,但是它不读取,等待键盘输入读取,并且键盘输入的字符不会显示在屏幕上。所以好多人喜欢用getch函数实现暂停功能。
}
int main()
{
print8();
printf("hello...\n");
system("pause");
return0;
}














评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值