当%D在一个使用printf发生浮点型变量是什么? [英] What happens to a float variable when %d is used in a printf?

查看:143
本文介绍了当%D在一个使用printf发生浮点型变量是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过阅读这本书学习C。我有一些编程经验,但不是用C

I'm trying to learn C by reading this book. I have some programming experience but not with C.

我目前在第1章我有以下的code:

I'm currently in Chapter 1. I have the following code:

  float f;
  for (f = 0.0; f <= 3; f += 1.1)
      printf("A: %3f B: %6.2f\n", f, f + 0.15);

它打印输出:

A: 0.000000 B:   0.15
A: 1.100000 B:   1.25
A: 2.200000 B:   2.35

看起来不错。

现在我改变的printf如下:

Now I change the printf as follows:

printf("A: %3d B: %6.2f\n", f, f + 0.15);

新的输出

A:   0 B:   0.00
A: -1610612736 B:   0.00
A: -1610612736 B: -625777476808257557292155887552002761191109083510753486844893290688350183831589633800863219712.00

这是怎么回事?我预计要转换的浮动为int因为我用%d个但那不是发生了什么事。此外,为什么B值出问题呢?发生了什么事到f这里?

What's going on here? I would expect the float to be converted to int because I used %d but that's not what happened. Also, why did the the value B go wrong as well? What happened to f here?

推荐答案

在名为:

printf("A: %3d B: %6.2f\n", f, f + 0.15);

ç自动转换浮动双击(它是当你调用一个函数标准转换提出,采用可变参数,如 INT的printf(为const char * FMT,...); )。为了便于讨论,我们假设的sizeof(INT) 4和的sizeof(双)是8(有也有例外,但他们却并不多见)。

C automatically converts the float values to double (it is a standard conversion made when you call a function that takes variable arguments, such as int printf(const char *fmt, ...);). For sake of argument, we will assume that sizeof(int) is 4 and sizeof(double) is 8 (there are exceptions, but they are few and far between).

通话,因此,推指针到堆栈,再加上一个8字节的两倍˚F,和另外8字节双 F + 0.15 。当处理格式字符串时,%d个告诉的printf()你推了4个字节的 INT 到格式字符串后堆栈。因为这不是你做了什么,你已经调用未定义的行为;无论发生什么事情接下来就是确定根据C标准。

The call, therefore, has pushed a pointer onto the stack, plus an 8-byte double for f, and another 8-byte double for f + 0.15. When it is processing the format string, the %d tells printf() that you pushed a 4-byte int onto the stack after the format string. Since that is not what you did, you have invoked undefined behaviour; whatever happens next is OK according to the C standard.

不过,最有可能实现轻率地读取4个字节,并将它们打印,好像他们是一个 INT (它相信你告诉它的真相)。然后,它遇到了%6.2f 格式;它会读取从堆栈中8个字节为双击。有一个外部的机会,这会导致未对齐访问的内存故障(它需要一个64位的机器与要求双是一个8字节边界上对齐,如SPARC),否则它会读取 4字节˚F 4个字节F + 0.15 ,把他们共同打造一些意外双击价值 - 为您的例子显示了

However, the most likely implementation blithely reads 4 bytes and prints them as if they were an int (it trusts you to tell it the truth). Then it comes across the %6.2f format; it will read 8-bytes off the stack as a double. There's an outside chance that this would cause a memory fault for misaligned access (it would require a 64-bit machine with a requirement that double be aligned on an 8-byte boundary, such as a SPARC), or it will read 4 bytes from f and 4 bytes from f + 0.15, putting them together to create some rather unexpected double value -- as your example shows.

这篇关于当%D在一个使用printf发生浮点型变量是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆