Understand the Output of 1000*1000 in C Programming

In summary: printf("%f",j);or you could have used the const modifier on the floating point variable:j = const float 1000. * 1000.;or you could have used a type conversion:j = (float)1000. * 1000.;
  • #1
sunakshi
2
0
Hello,
I want to know the answer of following program and the reason behind it...as soon as possible

void main()
{
float j;
j=1000*1000;
printf("%f",j);
}
 
Technology news on Phys.org
  • #2
sunakshi said:
Hello,
I want to know the answer of following program and the reason behind it...as soon as possible

void main()
{
float j;
j=1000*1000;
printf("%f",j);
}

What do you get when you attempt to compile this code?
 
  • #3
It answers as 16960.00000
but i don't know y?
 
  • #4
Does it? It doesn't compile here without additional headers, and it displays 1000000.000000 as a result.
 
  • #5
sunakshi said:
It answers as 16960.00000
but i don't know y?
OK, I see what's going on. I'm pretty sure that your compiler uses two-byte (signed, probably) ints, which can hold a largest (signed) value of 32,767. More modern compilers use four bytes for ints.

The compiler does the multiplication using two-byte quantities, getting a value of 16960.

This is easier to see if you work with hex (base-16) numbers. In hex, each pair of bytes is one byte, so a four-digit hex number fits in two bytes.
1000 * 1000 in base 10 is the same as 3E8 * 3E8 in hex, which is F4240 in hex (or 1,000,000 in decimal).

Since the multiplication is done using two-byte integers, there is no room for the F digit, so the result is truncated to the lower two bytes, leaving 4240 (hex), which is 16960 (decimal).

This value is stored in the float variable j, and is later displayed by the printf statement. A float can store much larger numbers, but the damage has already been done, and you get the result that you did.

You can force the multiplication to be done using long ints by appending L to the two constants, as in the following code.

Also, I have #included "stdio.h" to provide the prototype for printf.

Code:
#include "stdio.h"

void main()
{
   float j;
   j=1000L*1000L;
   printf("%f",j);
}
 
Last edited:
  • #6
Mark44 said:
I'm pretty sure that your compiler uses two-byte (signed, probably) ints, which can hold a largest (signed) value of 32, 767.

LOL, I was working with these compilers for many years (Z80, then 8086) but somehow I didn't catch what was going on.

Shame on me
 
  • #7
Don't be so hard on yourself. We've been working with compilers that use 4-byte ints for what, 14 or 15 years?

1000 * 1000 == 16960 is sort of a surprising result. It was something of a fluke that it occurred to me that that maybe truncation was causing this output. Once I started down that path, I discovered that 0xF4240 - 0xF0000 = 0x4240, which is 16960 in decimal.
 
  • #8
Once you posted about 2 bytes I started to think in terms of 1000*1000 mod 65536 - same result :smile:
 
  • #9
I think you guys are being too hard on yourselves. Off the top of my head I believe you will find a pragma or compiler option that controls implicit casts and addresses this.

This is one of those things that really was not underscored until I played with C++ and the distinction of assignment versus initialization had to be made. The whole scope of operator overloading and the casting operator. ( I wrote a ton of printf debugging programs to understand how the c++ compiler treated expressions with implied casting ).

In c++ it is not straightforward, and is compiler dependent.
 
  • #10
You could have done the multiplication using floating point by adding a period to each number:

j = 1000. * 1000. ;
 

1. What exactly does the output of 1000*1000 in C Programming represent?

The output of 1000*1000 in C Programming represents the multiplication of two numbers, 1000 and 1000, resulting in the product of 1,000,000.

2. How is the output of 1000*1000 calculated in C Programming?

In C Programming, the multiplication operator (*) is used to perform mathematical multiplication between two numbers. In this case, 1000*1000 is calculated as 1000 multiplied by 1000, resulting in the output of 1,000,000.

3. Can the output of 1000*1000 in C Programming be a different value?

No, the output of 1000*1000 in C Programming will always be 1,000,000 as long as both numbers are integers. If one or both numbers are floating-point numbers, the output may be a decimal value.

4. What happens if one of the numbers in 1000*1000 is negative?

If one of the numbers in 1000*1000 is negative, the output will also be negative. For example, -1000*1000 will result in an output of -1,000,000.

5. Can variables be used in place of the numbers in 1000*1000 in C Programming?

Yes, variables can be used in place of the numbers in 1000*1000 in C Programming. As long as the variables are assigned integer values, the output will still be 1,000,000.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
4
Views
740
  • Programming and Computer Science
Replies
21
Views
2K
  • Programming and Computer Science
Replies
1
Views
759
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
22
Views
3K
  • Programming and Computer Science
Replies
8
Views
1K
Back
Top