博客
关于我
第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和SQL入门
查看>>
mysql在centos下用命令批量导入报错_Variable ‘character_set_client‘ can‘t be set to the value of ‘---linux工作笔记042
查看>>
Mysql在Linux运行时新增配置文件提示:World-wrirable config file ‘/etc/mysql/conf.d/my.cnf‘ is ignored 权限过高导致
查看>>
Mysql在Windows上离线安装与配置
查看>>
MySQL在渗透测试中的应用
查看>>
Mysql在离线安装时启动失败:mysql服务无法启动,服务没有报告任何错误
查看>>
Mysql在离线安装时提示:error: Found option without preceding group in config file
查看>>
MySQL基于SSL的主从复制
查看>>
Mysql基本操作
查看>>
mysql基本操作
查看>>
mysql基础
查看>>
mysql基础---mysql查询机制
查看>>
MySQL基础5
查看>>
MySQL基础day07_mysql集群实例-MySQL 5.6
查看>>
Mysql基础命令 —— 数据库、数据表操作
查看>>
Mysql基础命令 —— 系统操作命令
查看>>
MySQL基础学习总结
查看>>
mysql基础教程三 —常见函数
查看>>
mysql基础教程二
查看>>
mysql基础教程四 --连接查询
查看>>