use*_*009 41 c scanf conversion-specifier
我尝试使用C中的scanf()读入2个值,但系统写入内存的值不等于我输入的值.这是代码:
double a,b;
printf("--------\n"); //seperate lines
scanf("%ld",&a);
printf("--------\n");
scanf("%ld",&b);
printf("%d %d",a,b);
Run Code Online (Sandbox Code Playgroud)
如果我输入1和2,CMD返回正确的,但是b = -858993460这是我已经尝试的:使用float或int而不是double,使用scanf_s,使用scanf("%d或%f用于%i或%li或%lf或%e或%g),使用fflush(stdin)清除键盘缓冲区,首先读入b,尝试所有可能的组合.我发现32位操作系统上的双倍长度存在问题,这样你就不得不使用scanf("%lf",&f)读取一个double.无论我做什么,第二个值总是错误的.
我在Windows 7 32位操作系统上使用MS VS express 2012 for Desktop.
sim*_*onc 78
使用%lf格式说明符读取double:
double a;
scanf("%lf",&a);
Run Code Online (Sandbox Code Playgroud)
维基百科对可用的格式说明符有一个不错的参考.
您还需要使用%lf格式说明符来打印结果:
printf("%lf %lf",a,b);
Run Code Online (Sandbox Code Playgroud)
据我所知,这%d意味着decadic是无小数点的数字.如果要加载double值,请使用%lf转换(long float).对于printf你的值出于同样的原因是错误的,%d仅用于整数(如果你知道你在做什么,可能是字符)数字.
例:
double a,b;
printf("--------\n"); //seperate lines
scanf("%lf",&a);
printf("--------\n");
scanf("%lf",&b);
printf("%lf %lf",a,b);
Run Code Online (Sandbox Code Playgroud)