The discussion centers around the output of the expression 1000 * 1000 in a C programming context, specifically examining why the result may differ based on compiler behavior and data type sizes. Participants explore the implications of integer overflow and type casting in C.
Discussion Character
Technical explanation
Debate/contested
Exploratory
Main Points Raised
Some participants report that the output is 16960, suggesting it is due to the compiler using two-byte integers, which cannot represent the full value of 1000 * 1000.
Others argue that their compilers produce 1000000.000000 as the output, indicating that the behavior may vary based on the compiler and its settings.
A participant explains that the multiplication is performed using two-byte integers, leading to truncation and resulting in 16960, and provides a hexadecimal interpretation to illustrate this point.
Some participants suggest that using long integers (by appending L) could prevent this issue and ensure the multiplication is done correctly.
One participant mentions the possibility of compiler options or pragmas that control implicit casts, indicating that behavior may depend on specific compiler configurations.
Another participant proposes using floating-point numbers for the multiplication to avoid integer overflow by adding a decimal point to each number.
Areas of Agreement / Disagreement
Participants do not reach a consensus; there are multiple competing views regarding the output of the expression and the underlying reasons for the discrepancies observed across different compilers.
Contextual Notes
The discussion highlights potential limitations related to compiler behavior, data type sizes, and the implications of implicit type casting in C programming. Specific assumptions about compiler configurations and integer sizes are not universally applicable.
#1
sunakshi
2
0
Hello,
I want to know the answer of following program and the reason behind it...as soon as possible
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.
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.
Once you posted about 2 bytes I started to think in terms of 1000*1000 mod 65536 - same result
#9
airborne18
25
0
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.