Reputation: 375
So I have the following code:
#include <iostream>
#include <string>
#include <array>
using namespace std;
int main()
{
array<long, 3> test_vars = { 121, 319225, 15241383936 };
for (long test_var : test_vars) {
cout << test_var << endl;
}
}
In Visual Studio I get this output:
The same code executed on the website cpp.sh gave the following output:
I expect the output to be like the one from cpp.sh. I don't understand the output from Visual Studio. It's probably something simple; but I'd appreciate it nonetheless if someone could tell me what's wrong. It's has become a real source of annoyance to me.
Upvotes: 1
Views: 336
Reputation:
The MSVC uses a 4Byte long
. The C++ standard only guarantees long
to be at least as large as int
. Therefore the max number representable by a signed long
is 2.147.483.647
. What you input is too large to hold by the long
and you will have to use a larger datatype with at least 64bit.
The other compiler used a 64bit wide long
which is the reason why it worked there.
You could use int64_t
which is defined in cstdint
header. Which would guarantee the 64bit size of the signed int.
Your program would read:
#include <cstdint>
#include <iostream>
#include <array>
using namespace std;
int main()
{
array<int64_t, 3> test_vars = { 121, 319225, 15241383936 };
for (int64_t test_var : test_vars) {
cout << test_var << endl;
}
}
Upvotes: 7