当前位置:网站首页>scanf函数在混合接受数据(%d和%c相连接)时候的方式
scanf函数在混合接受数据(%d和%c相连接)时候的方式
2022-08-11 05:25:00 【Archimedes' boat】
如下代码,怎样输入才是正确的?
#include<stdio.h>
void main(){
int a,b;
char c,d;
scanf("%d%c%d%c",&a,&c,&b,&d);
printf("%d-%d-%c-%c",a,b,c,d);
}
假设希望a、b、c、d分别是10、20、A、B:
输入“10A20B”,输出“10-20-A-B”;输入“10A 20B”,输出“10-20-A-B”;输入“10A 20B”,输出仍然是“10-20-A-B”,这些都是正确的输入方式。
若输入“10 A20B”,则输出“10-1- -”,很明显不对了。
分析&总结:
输入时:
首先:如果是“%c%d”的形式,中间加不加空格都无所谓;
然后:如果是“%d%c”的形式,不应该在中间加上空格;
但是如果%c不连接在%d后面,则按照空格或者逗号修改输入方式:
#include<stdio.h>
void main(){
int a,b;
char c,d;
scanf("%d %c%d%c",&a,&c,&b,&d); /*此时输入“10 A20B”或者"10 A 20B”都可正常输入,
但是“10A20B”反而不能正常输入了*/
printf("%d-%d-%c-%c",a,b,c,d);
}
同理,“%d %c %d%c”和"%d %c%d%c"的效果一致,"%d,%c%d%c"对应的输入方式应该是“10,A20B"或者“10,A 20B”等等。
边栏推荐
- Diagnostic Log and Trace——为应用程序和上下文设置日志级别的方法
- Node-1.高性能服务器
- 目标检测学习目录(持续更新)
- Robust 3D Object Detection in Cold Weather Conditions
- 360° large field of view helmet recognition system-deep learning intelligent video analysis
- The kernel communicates with user space through character devices
- arduino的esp32环境搭建(不需要翻墙,不需要离线安装)
- CMT2380F32模块开发11-RTC例程
- CVPR2022——Not All Points Are Equal : IA-SSD
- Maykle Studio - Second Training in HarmonyOS App Development
猜你喜欢
随机推荐
weex入门踩坑
物联网基础知识学习
【调试记录1】提高MC3172浮点运算能力,IQmath库的获取与导入使用教程
需求文档(PRD)撰写指南
aPaaS和iPaaS的区别
mAPH - Waymo dataset
Maykel Studio - Django Web Application Framework + MySQL Database Third Training
SCNet: Semantic Consistency Networks for 3D Object Detection
HTTP缓存机制详解
目标检测——Faster-RCNN 之 RCNN
Use regex to verify whether the file name is legal
The selection points you need to know about the helmet identification system
目标检测——LeNet
Mei cole studios - sixth DjangoWeb application framework + MySQL database training
STM32学习总结(二)——GPIO
Node-2.垃圾回收机制
OpenPCDet installs the latest version: spconv in one step
ASP.NET MVC 4中实现action的事务功能
梅科尔工作室-华为云ModelArts第一次培训
Joint 3D Instance Segmentation and Object Detection for Autonomous Driving