跳到主要内容

scanf

格式

scanf("格式化字符串",存储地址1,存储地址2......)

int a,b;
scanf("%d %d",&a,&b);
如何获取变量的内存地址

& 当加在变量前时是取地址符,如 &a 就表示变量a的内存地址

格式化输入

scanf的强大之处就在于用于一些特殊格式输入时,以下用一些例子说明

  • 时间格式 时:分:秒
int a,b,c;
scanf("%d:%d:%d",&a,&b,&c);
  • 日期 年-月-日
int a,b,c;
scanf("%d-%d-%d",&a,&b,&c);
  • 函数形式 max(123,234)
int a,b;
scanf("max(%d,%d)",&a,&b);

要学会举一反三,格式化字符串并不是固定的,想怎么写就怎么写

其他类型

如何获取小数,字符等类型的数据

int a;
double b;
char c;
char d[100];
scanf("%d %f %c %s",&a,&b,&c,d); //数组名 就能代表数组地址