[英]Is there some problem with the following c code
当我编译并运行这个程序时,我的输入字符串与 output 不同。
#include<stdio.h>
int main()
{
int n; char ch[100];
scanf("%d : %5s", &n, ch);
printf("%d : %s", n, ch); // there is some problem with output of the string
return 0;
}
输入:45 asdf output:45:∟sëuⁿ■a
去掉 scanf() 中的 ":" 和 "5",在 "%d" 输入之后什么都没有读取
scanf 部分会一直读取,直到碰到冒号,但它不会读取冒号本身,这意味着下面的 integer 将无法正确读取,并且字符串的 rest 将被错误地解析(如果有的话)。
尝试从 scanf 中删除冒号 (:)
#include <stdio.h>
int main()
{
int n; char ch[100];
scanf("%d %5s", &n, ch);
printf("%d : %s", n, ch);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.