Home >Backend Development >C++ >Why does 'long long' behave differently than 'int' or 'long' in C/C ?
Understanding Long Long in C/C
In C and C , the 'long long' data type represents a signed integer with 64 bits of precision. This allows it to represent extremely large or small numbers that cannot be accommodated by standard integer types like 'int' or 'long'.
In the provided code snippet, several questions have been raised regarding the behavior of 'long long' variables:
Size of 'long long':
The 'sizeof' operator returns the size of a variable or data type in bytes. Since 'num3' is not initialized with any value, it remains uninitialized, and its size is undefined, resulting in 0. To remedy this, explicitly initialize 'num3' with a 'long long' value.
Integer Overflow:
C handles integer overflow silently. When the value assigned to a variable exceeds its capacity, it wraps around and continues counting from the lowest value. In this case, assigning 10000000000 to a 'long' variable results in overflow, and the result is a negative value. However, since 'num2' is a 'long long', it can accommodate the large value without overflow.
Literal Integer Constants:
In C and C , integer constants are strongly typed. Assigning a literal integer constant that exceeds the limits of the destination data type results in a compilation error. In this example, the constant 100000000000 exceeds the range of 'long', and adding 'LL' explicitly specifies that it should be treated as a 'long long' constant.
The above is the detailed content of Why does 'long long' behave differently than 'int' or 'long' in C/C ?. For more information, please follow other related articles on the PHP Chinese website!