How to convert from long long to int and the other way back in c++ ?? also what are the properties of long long , especially its maximum size, thank in advance ..
3 Answers
int
is guaranteed to be at least 16 bits wide. On modern systems, it's most commonly 32 bits (even on 64-bit systems).
long long
, which didn't originally exist in C++, is guaranteed to be at least 64 bits wide. It's almost always exactly 64 bits wide.
The usual way to convert a value from one integer type to another is simply to assign it. Any necessary conversion will be done implicitly. For example:
int x = 42;
long long y = 9223372036854775807;
y = x; // implicitly converts from int to long long
x = y; // implicitly converts from long long to int
For a narrowing conversion, where the target type can't represent all the values of the source type, there's a risk of overflow; int
may or may not be able to hold the value 9223372036854775807
. In this case, the result is implementation-defined. The most likely behavior is that the high-order bits are discarded; for example, converting 9223372036854775807
to int
might yield 2147483647
. (This is clearer in hexadecimal; the values are 0x7fffffffffffffff
and 0x7fffffff
, respectively.)
If you need to convert explicitly, you can use a cast. A C-style cast uses the type name in parentheses:
(long long)x
Or you can use a C++-style static_cast
:
static_cast<long long>(x)
which is somewhat safer than a C-style cast because it's restricted in which types it can operate on.

- 254,901
- 44
- 429
- 631
Type long long is typically 64 bits.
Type int is likely to be 32 bits, but not on all machines.
If you cast an int to a long long, you can do
my_long_long = (long long) my_int
and it will be just fine. If you go the other direction, like
my_int = (int) my_long_long
and the int is smaller than 64-bits, it won't be able to hold all the information, so the result may not be correct.

- 1,429
- 8
- 8
-
10Does it just truncate the higher order bits when casting to a smaller size? – adu Nov 19 '13 at 11:54
-
4@adu: Maybe. That's the typical behavior, but the result is implementation-defined for a signed target type. For an unsigned target type, the result is defined by the language, and is equivalent to discarding the high-order bits. – Keith Thompson Jan 23 '15 at 02:03
-
7Downvoting this, since I believe a C++ cast like static_cast should always be preferred over C-style-casts when coding C++ . – FourtyTwo Oct 18 '17 at 09:37
-
@FourtyTwo static_cast is too verbose and for that simple digital to digital conversion c-cast looks better, imho. – Guinzoo Dec 29 '17 at 17:21
-
6@Guinzoo This is what most programmers think when they start using C++. But please reconsider: Somebody might refactor the code to actually make my_int a pointer instead of a local variable (the variable name might not reflect this). Then, guess what: The cast still succeeds, but gives you the *address* instead of the value of the variable. With static_cast, you get a compiler error. There are many ways of creating undefined behavior or even crashes of an application like this! Let the compiler help you where it can - the new C++ casts are there to help, and are *not* verbose at all! – FourtyTwo Jan 02 '18 at 08:48
-
The casts aren't necessary. You can just assign the values: `my_long_long = my_int;` or `my_int = my_long_long;` and the conversion will be done implicitly. This is actually safer, since there's no risk of using the wrong type in the cast. – Keith Thompson Jan 02 '18 at 17:41
Size of int is only 2 bytes whereas the other one is usually larger than int. So if you are looking to convert long into int then you would end up loosing information. But the other way is possible without sacrificing the correctness of information.
Suppose a
is of long type and b
is of int type. Then int to long covertion:a=(long)b;
. For other way:b=(int)a;
.

- 862
- 1
- 10
- 17
-
1The width of `int` is at least 16 bits. It's typically 32 bits on modern systems. (`sizeof (int)` could be as small as 1, but only if `CHAR_BIT >= 16`, which is unusual.) – Keith Thompson Jan 23 '15 at 02:05