赞
踩
当做到一道结构体问题时,输出一直错误,找不到原因。问题如下:
- typedef struct student
- {
- float grade[3];
- long long int number;
- }stu;
- int main()
- {
- stu stu1;
- scanf("%d;%f,%f,%f", &(stu1.number), &(stu1.grade[0]), &(stu1.grade[1]), &(stu1.grade[2]));
- printf("The each subject score of No. %d is %.2f, %.2f, %.2f.\n",stu1.number,stu1.grade[0],stu1.grade[1],stu1.grade[2]);
- return 0;
- }
-
- //输入:1410202;77,88,99
- //输出:The each subject score of No. 1410202 is 0.00, 0.00, 0.00.
- //预期结果:The each subject score of No. 1410202 is 77.00, 88.00, 99.00.
这里,就是因为number是long long int类型,而该代码中无论是输入还是输出都是%d,导致输出结果错误。
为了避免这种问题,在对16进制数输入输出时都要用%lld格式输入输出
例:long long int number;
scanf("%lld",&number);
printf("%lld\n",number);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。