scanf是stdio.h(standard input and output,基本输入输出)库中的函数,所以需要使用#include <stdio.h>将头文件包含进来,如过不包含,将提示scanf未定义。如下所示,我们将通过"//"将#include <stdio.h>注释掉,则提示错误如下(scanf was not declared...),因scanf在stdio.h中。):
1,2,3
--------------------------------
Process exited after 8.165 seconds with return value 0
请按任意键继续. . .
(2)&a,&b,&c(取地址符号):表示的是取a,b,c的地址,键盘输入数据写入对应地址。
cpp复制代码
#include <stdio.h>
int main(void)
{
int a,b,c,sum;
scanf("%d,%d,%d",&a,&b,&c);
printf("%p,%p,%p",&a,&b,&c);
return 0;
}
cpp复制代码
1,2,3
000000000062FE1C,000000000062FE18,000000000062FE14
--------------------------------
Process exited after 9.363 seconds with return value 0
请按任意键继续. . .
1,2,3
000000000062FE18,000000000062FE10,000000000062FE08
--------------------------------
Process exited after 3.591 seconds with return value 0
请按任意键继续. . .
16进制....FE18与...FE10相差8字节,这与实际一致。
(3)漏写"&"符号
cpp复制代码
#include <stdio.h>
int main()
{
int n;
scanf("%d",n);
printf("%d",n);
}
cpp复制代码
3
--------------------------------
Process exited after 8.583 seconds with return value 3221225477
请按任意键继续. . .
未能执行后续输出操作。
#include <stdio.h>
int main()
{
int n;
scanf("%d",&n);
printf("%d",n);
}
加上"&"符号后,运行正常
3
3
Process exited after 1.887 seconds with return value 0
请按任意键继续. . .
3 输出通过变量名(不需要&符号)
cpp复制代码
#include <stdio.h>
int main(void)
{
int a,b,c,sum;
scanf("%d,%d,%d",&a,&b,&c);
printf("%d,%d,%d",a,b,c);
return 0;
}
cpp复制代码
1,2,3
1,2,3
--------------------------------
Process exited after 6.643 seconds with return value 0
请按任意键继续. . .