float型和double型的问题

muyangpiaomiao 2014-04-17 08:47:12
有段程序是这样的:
struct stu
{
int num;
char *name;
char sex;
float score;
} boy1={102,"Zhang ping",'M',78.5},*pstu;
main()
{
pstu=&boy1;
printf("Number=%d\nName=%s\n",boy1.num,boy1.name);
printf("Sex=%c\nScore=%f\n\n",boy1.sex,boy1.score);
printf("Number=%d\nName=%s\n",(*pstu).num,(*pstu).name);
printf("Sex=%c\nScore=%f\n\n",(*pstu).sex,(*pstu).score);
printf("Number=%d\nName=%s\n",pstu->num,pstu->name);
printf("Sex=%c\nScore=%f\n\n",pstu->sex,pstu->score);
},运行正常,但是在我用double型替换float型后,如下,score项只显示0,这是为什么呢?
struct stu
{
int num;
char *name;
char sex;
double score;
} boy1 = { 102, "Zhang ping", 'M', 78.5 }, *pstu;
void main()
{
pstu = &boy1;
printf("Number=%d\nName=%s\n", boy1.num, boy1.name);
printf("Sex=%c\nScore=%d\n\n", boy1.sex, boy1.score);
printf("Number=%d\nName=%s\n", (*pstu).num, (*pstu).name);
printf("Sex=%c\nScore=%d\n\n", (*pstu).sex, (*pstu).score);
printf("Number=%d\nName=%s\n", pstu->num, pstu->name);
printf("Sex=%c\nScore=%d\n\n", pstu->sex, pstu->score);
}
...全文
172 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
muyangpiaomiao 2014-04-17
  • 打赏
  • 举报
回复
引用 13 楼 xiaohuh421 的回复:
参数 有道词典结果 n. 论证;论据;争吵;内容提要 查看详细结果»
哦,是有啊,这微软真是的,好好说话不行吗,老用生僻字,头疼!
xiaohuh421 2014-04-17
  • 打赏
  • 举报
回复
参数 有道词典结果 n. 论证;论据;争吵;内容提要 查看详细结果»
muyangpiaomiao 2014-04-17
  • 打赏
  • 举报
回复
引用 2 楼 xiaohuh421 的回复:
The type character is the only required format field; it appears after optional format fields. The type character determines whether the associated argument is interpreted as a character, string, or number. The types C and S, and the behavior of c and s with printf functions, are Microsoft extensions and are not ANSI-compatible. printf Type Field Characters Character Type Output Format c int or wint_t When used with printf functions, specifies a single-byte character. C int or wint_t When used with printf functions, specifies a wide character. d int Signed decimal integer. i int Signed decimal integer. o int Unsigned octal integer. u int Unsigned decimal integer. x int Unsigned hexadecimal integer, using "abcdef." X int Unsigned hexadecimal integer, using "ABCDEF." e double Signed value having the form [ – ]d.dddd e [sign]ddd where d is a single decimal digit, dddd is one or more decimal digits, ddd is exactly three decimal digits, and sign is + or –. E double Identical to the e format except that E rather than e introduces the exponent. f double Signed value having the form [ – ]dddd.dddd, where dddd is one or more decimal digits. The number of digits before the decimal point depends on the magnitude of the number, and the number of digits after the decimal point depends on the requested precision. g double Signed value printed in f or e format, whichever is more compact for the given value and precision. The e format is used only when the exponent of the value is less than –4 or greater than or equal to the precision argument. Trailing zeros are truncated, and the decimal point appears only if one or more digits follow it. G double Identical to the g format, except that E, rather than e, introduces the exponent (where appropriate). n Pointer to integer Number of characters successfully written so far to the stream or buffer; this value is stored in the integer whose address is given as the argument. p Pointer to void Prints the address of the argument in hexadecimal digits. s String When used with printf functions, specifies a single-byte–character string. Characters are printed up to the first null character or until the precision value is reached. S String When used with printf functions, specifies a wide-character string. Characters are printed up to the first null character or until the precision value is reached.
“argument”在这里是如何翻译的,我查了下只有争论一类的意思呀?
li4c 2014-04-17
  • 打赏
  • 举报
回复
printf("Sex=%c\nScore=%d\n\n", pstu->sex, pstu->score);
改成
printf("Sex=%c\nScore=%lf\n\n", pstu->sex, pstu->score);
muyangpiaomiao 2014-04-17
  • 打赏
  • 举报
回复
引用 7 楼 zhao4zhong1 的回复:
printf里面的%和变量的一一对应关系 scanf里面的%和变量以及变量前加不加&的一一对应关系 是C代码中非常容易出错的地方,而且通常编译还不出错。 所以在编译源代码之前值得专门仔细检查一遍甚至多遍。
这个以后是得注意点,看来还是c++在输出上简单些,都不用这些格式控制符!
muyangpiaomiao 2014-04-17
  • 打赏
  • 举报
回复
引用 6 楼 starytx 的回复:
格式化输出要和对应的数据类型匹配,整型用%d,浮点型用%f,double用%lf
谢谢,这才明白,看书马虎了
muyangpiaomiao 2014-04-17
  • 打赏
  • 举报
回复
引用 5 楼 mujiok2003 的回复:
输出浮点数(double或float),可以使用%f , %F, %e, %E, %g, %G ,%a , %A 输出长浮点数(long double),使用%Lf , %LF, %Le, %LE, %Lg, %LG ,%La , %LA 不要使用%d.
谢谢,我查了下是有这么多的格式呀,我原来吧%d当作是输出双精度型了,马虎呀
赵4老师 2014-04-17
  • 打赏
  • 举报
回复
printf里面的%和变量的一一对应关系 scanf里面的%和变量以及变量前加不加&的一一对应关系 是C代码中非常容易出错的地方,而且通常编译还不出错。 所以在编译源代码之前值得专门仔细检查一遍甚至多遍。
starytx 2014-04-17
  • 打赏
  • 举报
回复
格式化输出要和对应的数据类型匹配,整型用%d,浮点型用%f,double用%lf
mujiok2003 2014-04-17
  • 打赏
  • 举报
回复
输出浮点数(double或float),可以使用%f , %F, %e, %E, %g, %G ,%a , %A 输出长浮点数(long double),使用%Lf , %LF, %Le, %LE, %Lg, %LG ,%La , %LA 不要使用%d.
CarlXie 2014-04-17
  • 打赏
  • 举报
回复
double是双精度浮点数,它的输出应该是%f或者%lf 而你的输出用的是%d,只能输出整数部分。 至于输出为0的话,则有可能是因为字节对齐的原因。 有可能双精度浮点数用8字节表示,%d取整数的时候取到了为0的高位。
阿达King哥 2014-04-17
  • 打赏
  • 举报
回复
Score在输入的时候应该用%lf
xiaohuh421 2014-04-17
  • 打赏
  • 举报
回复
The type character is the only required format field; it appears after optional format fields. The type character determines whether the associated argument is interpreted as a character, string, or number. The types C and S, and the behavior of c and s with printf functions, are Microsoft extensions and are not ANSI-compatible. printf Type Field Characters Character Type Output Format c int or wint_t When used with printf functions, specifies a single-byte character. C int or wint_t When used with printf functions, specifies a wide character. d int Signed decimal integer. i int Signed decimal integer. o int Unsigned octal integer. u int Unsigned decimal integer. x int Unsigned hexadecimal integer, using "abcdef." X int Unsigned hexadecimal integer, using "ABCDEF." e double Signed value having the form [ – ]d.dddd e [sign]ddd where d is a single decimal digit, dddd is one or more decimal digits, ddd is exactly three decimal digits, and sign is + or –. E double Identical to the e format except that E rather than e introduces the exponent. f double Signed value having the form [ – ]dddd.dddd, where dddd is one or more decimal digits. The number of digits before the decimal point depends on the magnitude of the number, and the number of digits after the decimal point depends on the requested precision. g double Signed value printed in f or e format, whichever is more compact for the given value and precision. The e format is used only when the exponent of the value is less than –4 or greater than or equal to the precision argument. Trailing zeros are truncated, and the decimal point appears only if one or more digits follow it. G double Identical to the g format, except that E, rather than e, introduces the exponent (where appropriate). n Pointer to integer Number of characters successfully written so far to the stream or buffer; this value is stored in the integer whose address is given as the argument. p Pointer to void Prints the address of the argument in hexadecimal digits. s String When used with printf functions, specifies a single-byte–character string. Characters are printed up to the first null character or until the precision value is reached. S String When used with printf functions, specifies a wide-character string. Characters are printed up to the first null character or until the precision value is reached.
xiaohuh421 2014-04-17
  • 打赏
  • 举报
回复
Score=%d 这里全部换成%f %d是整数.
赵4老师 2014-04-17
  • 打赏
  • 举报
回复
引用 10 楼 u014496787 的回复:
[quote=引用 7 楼 zhao4zhong1 的回复:] printf里面的%和变量的一一对应关系 scanf里面的%和变量以及变量前加不加&的一一对应关系 是C代码中非常容易出错的地方,而且通常编译还不出错。 所以在编译源代码之前值得专门仔细检查一遍甚至多遍。
这个以后是得注意点,看来还是c++在输出上简单些,都不用这些格式控制符![/quote] 不用格式控制符,输出恰好是你期望的时候好说;等到输出不是你期望的时候,你会觉得还是用格式控制符更方便、更靠谱。 摒弃cin、cout 使用scanf,printf

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧