C++ does not load well when console input large values

Asked 1 years ago, Updated 1 years ago, 73 views

Thank you for your help.

1 AA BB 1010^18, 1 CC, D 1010^9
I tried to enter a number from the console when
It doesn't seem to fit into each variable well.Small values are acceptable, but
If you grow up, you can't go in.How can I get a big price?
Below is the code written.I'm running it on Windows.
Thank you for your cooperation.

#include<iostream>
using namespace std;
int main() {
    long A, B, C, D;

    cin>>A>>B>>C>D;

    printf("%ld, %ld, %ld, %ld", A, B, C, D);
    return 0;
}

c++ windows

2022-09-30 21:41

2 Answers

The C++ language integer type has no default size.As shown in the table, long is "at least 32-bit wide", which can be represented from -2,147,483,648 to 2,147,483,647.
Also, long is "at least 64-bit width", so it can be represented from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

Alternatively, int64_t can be clearly obtained by using int64_t in the fixed-width integer type provided by <cstdint>.

In addition, in a Windows environment, long is defined as a 4-byte width.(Note that long appears in the wrong place due to machine translation on the Japanese page.)

Both Champon and Peridotite seem to misunderstand, but long is defined as 4 bytes wide in Windows environments, regardless of whether Windows is 32bit/64bit and MinGW is 32bit/64bit.
It basically follows the size determined by the platform (in this case, Microsoft) because it interferes with API calls and printf formatting. (MinGW often has problem because it is the wrong size.)


2022-09-30 21:41

How many bits of pc are you using?
For Linux, the long type can be stored up to 10^18, but for Windows, the long range is -2147483648 to 2147483647.
Please look into the details.
As a solution, I think you can use the long long type.


2022-09-30 21:41

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.