Wrong output from printf of a number

2020-03-26 08:51发布

int main()
{
       double i=4;
       printf("%d",i);
       return 0;
}

Can anybody tell me why this program gives output of 0?

8条回答
冷血范
2楼-- · 2020-03-26 09:08

Because i is a double and you tell printf to use it as if it were an int (%d).

查看更多
聊天终结者
3楼-- · 2020-03-26 09:09

%d is for integers:

int main()
{

   int i=4;
   double f = 4;
   printf("%d",i); // prints 4
   printf("%0.f",f); // prints 4
   return 0;
}
查看更多
男人必须洒脱
4楼-- · 2020-03-26 09:16

Because "%d" specifies that you want to print an int, but i is a double. Try printf("%f\n"); instead (the \n specifies a new-line character).

查看更多
Juvenile、少年°
5楼-- · 2020-03-26 09:20

The simple answer to your question is, as others have said, that you're telling printf to print a integer number (for example a variable of the type int) whilst passing it a double-precision number (as your variable is of the type double), which is wrong.

Here's a snippet from the printf(3) linux programmer's manual explaining the %d and %f conversion specifiers:

   d, i   The int argument is converted to signed decimal  notation.   The
          precision,  if any, gives the minimum number of digits that must
          appear; if the converted value  requires  fewer  digits,  it  is
          padded  on  the  left  with  zeros.  The default precision is 1.
          When 0 is printed with an explicit precision 0,  the  output  is
          empty.

   f, F   The double argument is rounded and converted to decimal notation
          in the style [-]ddd.ddd, where the number of  digits  after  the
          decimal-point character is equal to the precision specification.
          If the precision is missing, it is taken as 6; if the  precision
          is  explicitly  zero,  no decimal-point character appears.  If a
          decimal point appears, at least one digit appears before it.

To make your current code work, you can do two things. The first alternative has already been suggested - substitute %d with %f.

The other thing you can do is to cast your double to an int, like this:

printf("%d", (int) i);

The more complex answer(addressing why printf acts like it does) was just answered briefly by Jon Purdy. For a more in-depth explanation, have a look at the wikipedia article relating to floating point arithmetic and double precision.

查看更多
兄弟一词,经得起流年.
6楼-- · 2020-03-26 09:22

Because the language allows you to screw up and you happily do it.

More specifically, '%d' is the formatting for an int and therefore printf("%d") consumes as many bytes from the arguments as an int takes. But a double is much larger, so printf only gets a bunch of zeros. Use '%lf'.

查看更多
小情绪 Triste *
7楼-- · 2020-03-26 09:26

When you create a double initialised with the value 4, its 64 bits are filled according to the IEEE-754 standard for double-precision floating-point numbers. A float is divided into three parts: a sign, an exponent, and a fraction (also known as a significand, coefficient, or mantissa). The sign is one bit and denotes whether the number is positive or negative. The sizes of the other fields depend on the size of the number. To decode the number, the following formula is used:

1.Fraction × 2Exponent - 1023

In your example, the sign bit is 0 because the number is positive, the fractional part is 0 because the number is initialised as an integer, and the exponent part contains the value 1025 (2 with an offset of 1023). The result is:

1.0 × 22

Or, as you would expect, 4. The binary representation of the number (divided into sections) looks like this:

0 10000000001 0000000000000000000000000000000000000000000000000000

Or, in hexadecimal, 0x4010000000000000. When passing a value to printf using the %d specifier, it attempts to read sizeof(int) bytes from the parameters you passed to it. In your case, sizeof(int) is 4, or 32 bits. Since the first (rightmost) 32 bits of the 64-bit floating-point number you supply are all 0, it stands to reason that printf produces 0 as its integer output. If you were to write:

printf("%d %d", i);

Then you might get 0 1074790400, where the second number is equivalent to 0x40100000. I hope you see why this happens. Other answers have already given the fix for this: use the %f format specifier and printf will correctly accept your double.

查看更多
登录 后发表回答