博客
关于我
第15节 数据的输入
阅读量:312 次
发布时间:2019-03-03

本文共 1410 字,大约阅读时间需要 4 分钟。

一.用scanf函数输入

1.scanf函数的功能:从键盘获得数据scanf(格式描述,变量地址); #include 
2.格式控制符%d,%i:用来输入整型数据,长整型ld,短整型hd;%o:用来输入八进制整数;%x:用来输入十六进制整数;%u:用来输入无符号十进制数;%c:用来输入单个字符;%f:用来输入浮点数,可用小数或指数形式输入;
int a,b;scanf("%d %d",&a,&b);printf("%d\n",a+b);

二.输入数据的分割

int a,b;scanf("%d %d",&a,&b);   2空格3 /2回车3 /2Tab3scanf("%d%d",&a,&b);    2空格3 /2回车3 /2Tab3scanf("%d,%d",&a,&b);    2逗号3scanf("%dand%d",&a,&b);  2and3scanf("a=%d,b=%d,c=%d",&a,&b,&c); a=2,b=3,c=4

三.不同类型数据的自然分割

#include 
int main(){ int a, b, c;char op;scanf("%d%c%d",&a,&op,&b);if(op=='+'){ c=a+b;printf("会算%c,结果是:%d\n", op, c);}else{ printf("不会算%c\n", op);}return 0;}

四.输入的数据都被暂放在"缓冲区"

#include 
int main(){ int a, b, c; scanf_s("%d", &a); scanf_s("%d", &b); scanf_s("%d", &c); printf("%d,%d,%d,%d", a, b, c);}输入输出结果:1 2 3 4 51,2,3

五.指定宽度的输入

#include 
int main(){ int a, b; scanf_s("%2d%3d", &a,&b); printf("%d,%d", a, b);}输入输出结果:1234567812,345

六.注意控制字符和类型的匹配

#include 
int main(){ double a, b; int c, d; scanf_s("%lf%lf", &a, &b); scanf_s("%d", &c); scanf_s("%d", &d); printf("%f,%f,%d,%d", a, b, c, d);}输入输出结果:1 2 3 41.000000,2.000000,3,4

七.实践项目

用下面的scanf函数输入数据,使a=3,b=7,x=8.5,y=71.82,问在键盘上如何输入?

#include 
int main(){ int a, b; float x, y; scanf_s("a=%d b=%d", &a, &b); scanf_s("%f %f", &x, &y); printf("%d %d %f %e", a, b, x, y); return 0;}输入输出结果:a=3 b=78.5 71.823 7 8.500000 7.182000e+01

转载地址:http://yjqm.baihongyu.com/

你可能感兴趣的文章
mysql replace first,MySQL中处理各种重复的一些方法
查看>>
MySQL replace函数替换字符串语句的用法(mysql字符串替换)
查看>>
mysql replace用法
查看>>
Mysql Row_Format 参数讲解
查看>>
mysql select, from ,join ,on ,where groupby,having ,order by limit的执行顺序和书写顺序
查看>>
MySQL Server 5.5安装记录
查看>>
mysql server has gone away
查看>>
mysql slave 停了_slave 停止。求解决方法
查看>>
MySQL SQL 优化指南:主键、ORDER BY、GROUP BY 和 UPDATE 优化详解
查看>>
MYSQL sql语句针对数据记录时间范围查询的效率对比
查看>>
mysql sum 没返回,如果没有找到任何值,我如何在MySQL中获得SUM函数以返回'0'?
查看>>
mysql Timestamp时间隔了8小时
查看>>
Mysql tinyint(1)与tinyint(4)的区别
查看>>
mysql union orderby 无效
查看>>
mysql v$session_Oracle 进程查看v$session
查看>>
mysql where中如何判断不为空
查看>>
MySQL Workbench 使用手册:从入门到精通
查看>>
mysql workbench6.3.5_MySQL Workbench
查看>>
MySQL Workbench安装教程以及菜单汉化
查看>>
MySQL Xtrabackup 安装、备份、恢复
查看>>