今天晚上看到了详细scanf的简述,再在这里复述一下

进行编译后会出现下方

Scanf和printf类似,是使用相同的格式和参数比如:
printf("%d",people);
Scanf("%d",&ages);
Scanf("%s",name,40);
但是%c在scanf的读取格式是不同于其他的,比如:
scanf("%c",&char);
scnaf("%c" ,&char);
在正常程序读取中空格即代表中断,但是%c是个例外,前者是从非空格处开始读取,后者是从第一个输入的值开始读取(空格也允许)!
printf使用变量和常量还有表达式,而Scanf使用指向变量和指针
实例
比如下方的程序
#include <stdio.h>
int main(void)
{
int people;\\设定人数
float cake;\\设置cake价格
char name[40];\\cake的名字
printf("how many people are in the store? \n");
scanf_s("%d",&people);\\变量需要使用‘&’进行赋值
printf("How much for a cake? \n");
scanf_s("%f",&cake);
printf("please tell me the name of the shop \n");
scanf_s("%s",name,40);\\因为是常量无需使用‘&’进行赋值
printf("ok %s is cheap,l'll be right here",name);
return 0;
}
进行编译后:
how many people are in the store?
3
How much for a cake?
5.99
please tell me the name of the shop
will
ok well is cheap,l'll be right here
可以总结为:
如果用scanf()读取基本变量类型的值,在变量名前加上一个&;
如果用scanf()把字符串读入字符数组中,不要使用&。