What's the difference between long long and long? And they both don't work with 12 digit numbers (600851475143), am I forgetting something?
#include <iostream>
using namespace std;
int main(){
long long a = 600851475143;
}
On major 32-bit platforms:
intis 32 bitslongis 32 bits as welllong longis 64 bits
On major 64-bit platforms:
intis 32 bitslongis either 32 or 64 bitslong longis 64 bits as well
Going by the standard:
intmust be at least 16 bitslongmust be at least 32 bitslong longmust be at least 64 bits
Correct me if I'm wrong.
If you need a specific integer size for a particular application, rather than trusting the compiler to pick the size you want, #include <stdint.h> (or <cstdint>) so you can use these types:
int8_tanduint8_tint16_tanduint16_tint32_tanduint32_tint64_tanduint64_t
You may also be interested in #include <stddef.h> (or <cstddef>):
size_tptrdiff_t
long long does not exist in C++98/C++03, but does exist in C99 and c++0x.
long is guaranteed at least 32 bits.
long long is guaranteed at least 64 bits.
To elaborate on @ildjarn's comment:
And they both don't work with 12 digit numbers (600851475143), am I forgetting something?
The compiler looks at the literal value 600851475143 without considering the variable that you're assigning it to/initializing it with. You've written it as an int typed literal, and it won't fit in an int.
Use 600851475143LL to get a long long typed literal.
Your C++ compiler supports long long, that is guaranteed to be at least 64-bits in the C99 standard (that's a C standard, not a C++ standard). See Visual C++ header file to get the ranges on your system.
Recommendation
For new programs, it is recommended that one use only bool, char, int, and double, until circumstance arises that one of the other types is needed.
Depends on your compiler.long long is 64 bits and should handle 12 digits.Looks like in your case it is just considering it long and hence not handling 12 digits.
来源:https://stackoverflow.com/questions/6462439/whats-the-difference-between-long-long-and-long