当前位置:网站首页>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”等等。
边栏推荐
- Robust 3D Object Detection in Cold Weather Conditions
- 梅科尔工作室-HarmonyOS应用开发第四次培训
- LAGRANGIAN FLUID SIMULATION WITH CONTINUOUS CONVOLUTIONS
- 张小龙的微信公开课(2019年)
- STM32学习总结(一)——时钟RCC
- 产品版本号是如何确定的
- ActiveReports报表分类之页面报表
- 安全帽识别系统-为安全生产保驾护航
- 智慧工地 安全帽识别系统
- Joint 3D Instance Segmentation and Object Detection for Autonomous Driving
猜你喜欢
华为云IOT平台设备获取api调用笔记
The selection points you need to know about the helmet identification system
Use regex to verify whether the file name is legal
OpenPCDet installs the latest version: spconv in one step
梅科尔工作室-华为云ModelArts第二次培训
CNN-based Point Cloud De-Noising
aPaaS和iPaaS的区别
weex入门踩坑
KANO模型——确定需求优先级的神器
Introduction of safety helmet wearing recognition system
随机推荐
Pay “Attention” to Adverse Weather
STM32学习总结(二)——GPIO
Wisdom construction site safety helmet identification system
支付牌照是什么意思
自定义形状seekbar学习
The selection points you need to know about the helmet identification system
CMT2380F32模块开发4-UART例程
C语言的编译
梅科尔工作室-DjangoWeb 应用框架+MySQL数据库第五次培训
Node-1.高性能服务器
Node-2.垃圾回收机制
Diagnostic Log and Trace——DLT 离线日志存储
Asp doNet Mvc4绑定js脚本用法
安全帽识别系统
Safety helmet recognition - construction safety "regulator"
CMT2380F32模块开发5-CLK例程
产品经理与演员有着天然的相似
AI-based intelligent image recognition: 4 different industry applications
红外线一认识
Hard hat identification