【发布时间】:2016-06-24 07:11:05
【问题描述】:
我只知道这个链接的 %i 格式说明符
Difference between format specifiers %i and %d in printf
我尝试用这个程序来实现它。
#include <stdio.h>
int main(){
long long a,b;
printf("Input: ");
scanf("%i %lld",&b,&a);
printf("Output: %i %lld",b,a);
}
%i 工作正常,但 %lld 将垃圾值存储在变量 a 中。
这是这个程序的输出。
输入:033 033
输出:27 141733920846
进程返回 0 (0x0) 执行时间:4.443 s 按任意键继续。
谁能解释一下,为什么我在变量 a 中得到垃圾值?
【问题讨论】:
-
你没有输入 %11d ,你输入了 033\n (以及之前执行的一些垃圾),这些都是由 scanf 解释的,这里的错误是 scanf 中的格式,和 scanf 本身。
-
将无效的东西传递给 scanf 是未定义的行为,并且 %lld 不接受八进制文字。
-
使用错误的格式说明符会调用未定义的行为stackoverflow.com/q/4231891/995714, stackoverflow.com/q/16864552/995714
标签: c format-specifiers