大部分输入函数都是从缓冲区中读取数据,当缓冲区中有数据时,直接读取,当缓冲区中没有数据时,你需要通过键盘将数据输入缓冲区中,再由各输入函数从缓冲区读取数据到变量中,然而有一些输入函数不管缓冲区中有没有数据,不会读取缓冲区中的数据。是一定
通过键盘读取的,如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;
}