I have a simple C++ program that multiplies two long int variables and prints the result. Here's the code:
#include <iostream> using namespace std; int main() { long int a = 100000; long int b = 100000; long int c = a * b; cout << c << endl; return 0; } On online compilers, the output is as expected: 10000000000. However, when I run the same code on my computer, the output is 1410065408. I'm confused as to why this discrepancy is occurring.
Could someone please explain why this difference in output is happening? Is it related to compiler settings or some other factor? And how can I ensure consistent behavior across different environments?
longhas a width of 64 bits, whereas you are probably using Microsoft Windows, on which alongis usually 32 bits wide. I suggest that you modify your program to also printstd::numeric_limits<long>::maxas well assizeof(long), to see what these values are on the platforms on which you are running the program.longin Microsoft is only 32 bits, but it's understandable. They're the kings of backwards compatibility, and 20 years ago 32 bits was perfectly defensible - but now they're stuck with it forever so that existing code won't break.int64_t. That type is guaranteed to be 64 bits wide on all platforms.int64_tmight not be supported on every platform. But at least if it isn't supported you'll get a compile error instead of some unexpected output.