标签:style blog http color os io 使用 ar strong
函数名: scanf 功 能: 执行格式化输入 用 法: int scanf(char *format[,argument,...]); scanf("格式控制字符串",输入参数一,输入参数二)
格式控制字符串表示输入的格式:(int型用%d,float用%f,double型用%lf)
%d 十进制有符号整数 %u 十进制无符号整数 %f 浮点数 %s 字符串 %c 单个字符 %p 指针的值 %e 指数形式的浮点数 %x, %X 无符号以十六进制表示的整数 %0 无符号以八进制表示的整数 %g 自动选择合适的表示法
输入参数是指向变量的地址,所以要在变量前面加&
1.scanf函数的基本用法
scanf函数是一个阻塞式的函数,这个函数一个标准的输入函数,也就是说这个函数会等待标准输入设备(比如键盘)输入数据,并且会将输入的数据赋值给地址对应的变量
//
// main.c
// scanfMethod
//
// Created by Mac on 14-2-20.
// Copyright (c) 2014年 itcast. All rights reserved.
//
#include <stdio.h>
int main(int argc, const char * argv[])
{
// insert code here...
printf("请输入一个整数:");
int a;
//这里要使用&这个符号,这个符号表示引用变量a的地址,这个函数的意思就是当我输入一个整数的时候,会讲这个整数赋给变量a这个变量的地址
scanf("%d",&a);
printf("%d*%d=%d\n1",a,a,a*a);
return 0;
}
2.scanf函数接收多个值的用法
当scanf函数接收多个值的时候,我们可以通过符号(不包括%)将多个输入值分割开来,但是值得注意的是当分割符号式空格的时候,我们可以用空格或者tab键或者回车键来讲输入的值分开
1 //
2
3 // main.c
4
5 // scanfMethod
6
7 //
8
9 // Created by Mac on 14-2-20.
10
11 // Copyright (c) 2014年 itcast. All rights reserved.
12
13 //
14
15
16
17 #include <stdio.h>
18
19
20
21 int main(int argc, const char * argv[])
22
23 {
24
25
26
27 // insert code here...
28
29 printf("请输入三个整数:");
30
31 int a,b,c;
32
33 //注意如果分割符号式空格则我们在输入数据的时候可以通过空格或者tab键或者回车键来代替空格
34
35 scanf("%d*%d*%d",&a,&b,&c);
36
37 printf("%d*%d*%d=%d\n1",a,b,c,a*b*c);
38
39 return 0;
40
41 }
注意事项:
|
1
|
scanf("%c%c%c",&a,&b,&c); |
|
1
|
def |
|
1
|
scanf("%c%c%c",&a,&b,&c); |
|
1
2
3
4
5
6
7
8
9
|
#include<stdio.h>intmain(void){chara,b;printf("inputcharactera,b\n");scanf("%c%c",&a,&b);/*注意两个%c之间没有任何符号*/printf("%c%c\n",a,b);return0;} |
|
1
|
|
|
1
|
|
|
1
2
3
4
5
6
7
8
9
|
#include<stdio.h>intmain(void){chara,b;printf("inputcharactera,b\n");scanf("%c%c",&a,&b);/*注意两个%c之间有个空格*/printf("\n%c%c\n",a,b);return0;} |
|
1
|
scanf("%d,%d,%d",&a,&b,&c); |
|
1
|
5,6,7 |
|
1
|
scanf("a=%d,b=%d,c=%d",&a,&b,&c); |
|
1
|
a=5,b=6,c=7 |
|
1
2
3
4
5
6
7
8
9
|
#include<stdio.h>intmain(void){inta;printf("inputanumber");scanf("%d",&a);printf("%ld",a);return0;} |
|
1
2
3
4
5
6
7
8
|
#include<stdio.h>intmain(void){charstr[80];scanf("%s",str);printf("%s",str);return0;} |
|
1
|
Iloveyou! |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#include<stdio.h>#include<windows.h>intmain(void){charstr[80],str1[80],str2[80];scanf("%s",str);/*此处输入:Iloveyou!*/printf("%s\n",str);Sleep(5000);/*这里等待5秒,告诉你程序运行到什么地方*//*不是sleep(5)1,函数名是Sleep不是sleep。2,C/C++中,unsignedSleep(unsigned)应该是毫秒ms.*/scanf("%s",str1);/*这两句无需你再输入,是对stdin流再扫描*/scanf("%s",str2);/*这两句无需你再输入,是对stdin流再扫描*/printf("%s\n",str1);printf("%s\n",str2);return0;} |
|
1
|
Iloveyou! |
|
1
2
3
|
Iloveyou! |
|
1
2
3
4
5
6
7
8
|
#include<stdio.h>intmain(void){charstr[50];scanf("%49[^\n]",str);/*scanf("%s",string);不能接收空格符*/printf("%s\n",str);return0;} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#include<stdio.h>intmain(void){inta;charc;while(c!=‘N‘){scanf("%d",&a);scanf("%c",&c);printf("a=%dc=%c\n",a,c);/*printf("c=%d\n",c);*/}return0;} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#include<stdio.h>intmain(void){inta;charc;while(c!=‘N‘){scanf("%d",&a);fflush(stdin);scanf("%c",&c);fflush(stdin);printf("a=%dc=%c\n",a,c);}return0;} |
|
1
2
3
4
5
6
7
8
9
10
|
#include<stdio.h>intmain(void){inti;charj;for(i=0;i<10;++i)scanf("%c",&j);/*这里%前没有空格*/printf("%c",j);/*在输入十个字符之后*/return0;} |
|
1
2
3
4
5
6
7
8
9
10
|
#include<stdio.h>intmain(void){inti;charj;for(i=0;i<10;++i)scanf("%c",&j);/*注意这里%前有个空格*/printf("%c",j);/*在输入十个字符之后,验证打印出来的字符是否是自己输入的最后一个字符(即输入的第十个字符)*/return0;} |
|
1
2
3
4
5
6
7
8
9
10
|
#include<stdio.h>intmain(void){inta=0,b=0,c=0,ret=0;ret=scanf("%d%d%d",&a,&b,&c);printf("第一次读入数量:%d\n",ret);ret=scanf("%d%d%d",&a,&b,&c);printf("第二次读入数量:%d\n",ret);return0;} |
|
1
2
3
4
5
6
7
8
9
10
|
#include<stdio.h>intmain(void){inta=0,b=0,c=0,ret=0;ret=scanf("%d%d%d",&a,&b,&c);printf("第一次读入数量:%d\n",ret);ret=scanf("%c%d%d",&a,&b,&c);printf("第二次读入数量:%d\n",ret);return0;} |
|
1
2
3
4
5
6
7
8
9
10
11
12
|
#include<stdio.h>intmain(void){inta=0,b=0,c=0,ret=0;ret=scanf("%d%d%d",&a,&b,&c);fflush(stdin);printf("第一次读入数量:%d\n",ret);ret=scanf("%d%d%d",&a,&b,&c);fflush(stdin);printf("第二次读入数量:%d\n",ret);return0;} |
|
1
2
3
4
5
6
7
8
9
|
#include<stdio.h>intmain(void){inta,b,c;scanf("%d,%d",&a,&b);c=a+b;/*计算a+b*/printf("%d+%d=%d",a,b,c);return0;} |
|
1
2
3
4
5
6
7
8
9
10
|
#include<stdio.h>intmain(void){inta,b,c;while(scanf("%d%d",&a,&b)!=2)fflush(stdin);c=a+b;printf("%d+%d=%d",a,b,c);return0;} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
/*此函数可以和scanf函数一起使用,但使用%c输入时要注意,即此函数只能用于缓冲区非空的情况*/#include<stdio.h>voidflush(){charc;while((c=getchar())!=‘\n‘&&c!=EOF);}intmain(void){inta,b,c;/*计算a+b*/while(scanf("%d%d",&a,&b)!=2)flush();c=a+b;printf("%d+%d=%d",a,b,c);return0;} |
1 #include<stdio.h>
2 intmain(void)
3 {
4 inti,c;
5 while(1)
6 {
7 printf("Pleaseinputaninteger:");
8 scanf("%d",&i);
9 if(feof(stdin)||ferror(stdin))
10 {
11 //如果用户输入文件结束标志(或文件已被读完),或者发生读写错误,则退出循环
12 //dosomething
13 break;
14 }
15 //没有发生错误,清空输入流。通过while循环把输入流中的余留数据“吃”掉
16 while((c=getchar())!=‘\n‘&&c!=EOF);
17 //可直接将这句代码当成fflush(stdin)的替代,直接运行可清除输入缓存流
18 //使用scanf("%*[^\n]");也可以清空输入流,不过会残留\n字符。
19 printf("%d\n",i);
20 }
21 return0;
22 }
标签:style blog http color os io 使用 ar strong
原文地址:http://www.cnblogs.com/lxcode/p/3955985.html