scanf()是C语言标准库中的输入函数,声明包含在stdio.h文件中,因此要使用该函数,必须加载#include
函数原型:int scanf(const char *format,...);
说明:scanf() 是从标准输入流stdio (标准输入设备,一般是键盘)中读内容的通用子程序,可以说明的格式读入多个字符,并保存在对应地址的变量中。
其调用形式为: scanf("<格式说明字符串>",<变量地址>);变量地址要求有效,并且与格式说明的次序一致。
使用示例:
//使用scanf函数输入一个字符变量。
char a;
scanf(“%c”,&a);
//同时输入多个值。
scanf(“%d,%d”,&num1,num2);
//在内部不能使用\n转义字符,否则没完没了。
scanf(“%d\n”,&a);
#include
main()
{
char sex = '0',diet = '0',sports = '0';
double faHeight = 0,moHeight = 0,x = 0;
printf("please input your parents's Height: ");
scanf("%f%f",&faHeight,&moHeight);}因为faHeight,moHeight都是double类型,但scanf里面是%f这样造成的输入错误是内存的分配问题,scanf("%lf%lf",&faHeight,&moHeight);注意变量的类型与输入时的类型必须一致,否则输入失败
头文件 #include
#include
int main()
{
int a,b,c;
printf("输入 a, b, c\n");
scanf("%d,%d,%d", &a, &b, &c);
printf("a = %d b = %d c = %d\n", a, b, c);
return 0;
}
scanf 使用的时候,要加上取地址符号 &,看下面代码
int a;
scanf("%d",&a);
记得看scanf("%d,%d,%d", &a, &b, &c);有没有加&符号