The language guarantees that int is at least 16 bits, long is at least 32 bits, and long can represent at least all the values that int can represent.

If you assign a long value to an int object, it will be implicitly converted. There's no need for an explicit cast; it would merely specify the same conversion that's going to happen anyway.

On your system, where int and long happen to have the same size and range, the conversion is trivial; it simply copies the value.

On a system where long is wider than int, if the value won't fit in an int, then the result of the conversion is implementation-defined. (Or, starting in C99, it can raise an implementation-defined signal, but I don't know of any compilers that actually do that.) What typically happens is that the high-order bits are discarded, but you shouldn't depend on that. (The rules are different for unsigned types; the result of converting a signed or unsigned integer to an unsigned type is well defined.)

If you need to safely assign a long value to an int object, you can check that it will fit before doing the assignment:

#include <limits.h> /* for INT_MIN, INT_MAX */

/* ... */

int i;
long li = /* whatever */

if (li >= INT_MIN && li <= INT_MAX) {
    i = li;
}
else {
    /* do something else? */
}

The details of "something else" are going to depend on what you want to do.

One correction: int and long are always distinct types, even if they happen to have the same size and representation. Arithmetic types are freely convertible, so this often doesn't make any difference, but for example int* and long* are distinct and incompatible types; you can't assign a long* to an int*, or vice versa, without an explicit (and potentially dangerous) cast.

And if you find yourself needing to convert a long value to int, the first thing you should do is reconsider your code's design. Sometimes such conversions are necessary, but more often they're a sign that the int to which you're assigning should have been defined as a long in the first place.

Answer from Keith Thompson on Stack Overflow
Top answer
1 of 2
52

The language guarantees that int is at least 16 bits, long is at least 32 bits, and long can represent at least all the values that int can represent.

If you assign a long value to an int object, it will be implicitly converted. There's no need for an explicit cast; it would merely specify the same conversion that's going to happen anyway.

On your system, where int and long happen to have the same size and range, the conversion is trivial; it simply copies the value.

On a system where long is wider than int, if the value won't fit in an int, then the result of the conversion is implementation-defined. (Or, starting in C99, it can raise an implementation-defined signal, but I don't know of any compilers that actually do that.) What typically happens is that the high-order bits are discarded, but you shouldn't depend on that. (The rules are different for unsigned types; the result of converting a signed or unsigned integer to an unsigned type is well defined.)

If you need to safely assign a long value to an int object, you can check that it will fit before doing the assignment:

#include <limits.h> /* for INT_MIN, INT_MAX */

/* ... */

int i;
long li = /* whatever */

if (li >= INT_MIN && li <= INT_MAX) {
    i = li;
}
else {
    /* do something else? */
}

The details of "something else" are going to depend on what you want to do.

One correction: int and long are always distinct types, even if they happen to have the same size and representation. Arithmetic types are freely convertible, so this often doesn't make any difference, but for example int* and long* are distinct and incompatible types; you can't assign a long* to an int*, or vice versa, without an explicit (and potentially dangerous) cast.

And if you find yourself needing to convert a long value to int, the first thing you should do is reconsider your code's design. Sometimes such conversions are necessary, but more often they're a sign that the int to which you're assigning should have been defined as a long in the first place.

2 of 2
4

A long can always represent all values of int. If the value at hand can be represented by the type of the variable you assign to, then the value is preserved.

If it can't be represented, then for signed destination type the result is formally unspecified, while for unsigned destination type it is specified as the original value modulo 2n, where n is the number of bits in the value representation (which is not necessarily all the bits in the destination).

In practice, on modern machines you get wrapping also for signed types.

That's because modern machines use two's complement form to represent signed integers, without any bits used to denote "invalid value" or such – i.e., all bits used for value representation.

With n bits value representation any integer value is x is mapped to x+K*2n with the integer constant K chosen such that the result is in the range where half of the possible values are negative.

Thus, for example, with 32-bit int the value -7 is represented as bitpattern number -7+232 = 232-7, so that if you display the number that the bitpattern stands for as unsigned integer, you get a pretty large number.

The reason that this is called two's complement is because it makes sense for the binary numeral system, the base two numeral system. For the binary numeral system there's also a ones' (note the placement of the apostrophe) complement. Similarly, for the decimal numberal system there's ten's complement and niners' complement. With 4 digit ten's complement representation you would represent -7 as 10000-7 = 9993. That's all, really.

🌐
Reddit
reddit.com › r/c_programming › is c guaranteed to correctly convert "long" and friends as "int"?
r/C_Programming on Reddit: Is C guaranteed to correctly convert "long" and friends as "int"?
October 11, 2022 -

I'm using a library function which takes arrays of long's as inputs (and single long's too), and modifies these arrays. As long as all the numbers inside the library stay within the int size limit (i.e. there are no calculations that would overflow int), is it 100% safe for me to pass it arrays of int's, or should I manually typecast my arrays using for loops from int to long before input? And then assume that these arrays still contain int's?

What if this was some other integer type, like unsigned int?

I am worried that, for example, number 5 has different bit representation (and is stored using a different number of bits) across these various data types. Will C figure it out and do the correct thing?

Top answer
1 of 3
9
No. It is UB. Strict aliasing rule are violated. See 6.5p7 : An object shall have its stored value accessed only by an lvalue expression that has one of the following types: a type compatible with the effective type of the object, a qualified version of a type compatible with the effective type of the object, a type that is the signed or unsigned type corresponding to the effective type of the object, a type that is the signed or unsigned type corresponding to a qualified version of the effective type of the object, an aggregate or union type that includes one of the aforementioned types among its members (including, recursively, a member of a subaggregate or contained union), or a character type. Type int and long int are not compatible and they are not enlisted on the list of exceptions above. Thus undefined behavior is invoked. However, the program is defined for unsigned int due to "a type that is the signed or unsigned type corresponding to the effective type of the object" exception.
2 of 3
5
long and int can have different sizing depending on platform. On Windows, they're both 32 bit so you probably won't have an issue (though still technically undefined behavior). On x86_64 Linux though, int is 32 bit while long is 64 bit. What will happen on Linux if you pass in your array of int is it'll read in 2 of your 32 bit elements thinking it's 1 64 bit element giving you a completely different number. Then it'll probably read outside the bounds of your array producing undefined behavior. You'll need to create a new array of longs and use a loop to convert them before you pass it off to the library. Now if the function is taking in a single long by value, it's fine to give it an int. C will do the conversion for you. It's only a problem with passing arrays and pointers.
🌐
TutorialsPoint
tutorialspoint.com › cplusplus-program-to-convert-int-type-variables-to-long
C++ Program to Convert int Type Variables to long
Conversion of int to long can be done in various different ways, out of them we have dicussed only two. The first one is through implicit conversion and the second one is using explicit conversion. Explicit type conversion requires mentioning the resultant datatype in the code and implicit type conversion is done by the compiler itself.
🌐
Quora
quora.com › How-do-you-convert-an-unsigned-int-to-a-long-in-C
How to convert an unsigned int to a long in C++ - Quora
Answer (1 of 3): While looks like simple question it is complicated one. Why? Because size of INT and LONG depend on CPU and OS. On 64 bit Windows both INT and LONG are 32 bits but under 64 bit Linux INT is 32 bit while LONG is 64 bits. So, reliably converting UINT to LONG under Windows or any 3...
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › how-to-use-int-as-long-long-int-for-competitive-programming
How to Use int as long long int for Competitive Programming? - GeeksforGeeks
July 23, 2025 - It is because the range of numbers integer data type can hold is 4 bytes meaning it can hold integer numbers ranging from -2,147,483,647 to 2,147,483,647. Here in our case, the output exceeds the maximum integer a variable can hold so do throw a warning of implicit constant conversion. So we do have to use a long datatype as it can hold 8 bytes.
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 134988-casting-long-int-int.html
Casting long int to int
Assuming that int is smaller than long, what will the result of the cast of a long int to int, given that the number is bigger than INT_MAX before the cast? Is that what you meant would be CPU dependent? The extra bits will be thrown away. Yes, that's what i mean.
🌐
TutorialsPoint
tutorialspoint.com › cplusplus-program-to-convert-long-type-variables-to-int
C++ Program to Convert long Type Variables to int
Conversion between different types of variables is very common in C++ or any programming language as different data types provide different ways to represent and manipulate the same kind of data. We mainly use two types of conversions, which are implicit and explicit types of conversions to convert between long and int.
🌐
Stack Overflow
stackoverflow.com › questions › 42472533 › how-to-convert-a-long-int-to-an-int-in-c
how to convert a long int to an int in C - Stack Overflow
void SpinReel(int *a, int *b) { long int reela = random() % 40; a= (int) reela; printf("&d\n", a); long int reelb = random() % 40; b= (int) reelb; printf("&d\n", b); } I am fairly new to C so I may just be misunderstanding the problem as well.
Find elsewhere
🌐
SysTutorials
systutorials.com › home › systutorials posts › converting string to long integer in c
Converting String to long Integer in C - SysTutorials
March 25, 2023 - In summary, converting a string to a long integer in C is a simple task that can be accomplished using the strtol and strtoul functions. These functions provide a convenient way to convert strings to long integers in various programming tasks.
🌐
w3resource
w3resource.com › c-programming-exercises › variable-type › c-variable-type-exercises-3.php
C Program: Convert a string to a long integer - w3resource
Write a C program to convert a string with a '+' or '-' sign to a long integer using strtol() and implement error detection.
🌐
TutorialsPoint
tutorialspoint.com › home › cprogramming › c type casting
C Type Casting
June 10, 2012 - On other occasions, the C compiler ... example, if you want to store a 'long' value into a simple integer then you can type cast 'long' to 'int'....
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Converting Long to int - Programming - Arduino Forum
March 7, 2014 - Hi, can any one please tell me how to convert data type long to an integer in Arduino. Quick response is appreciated Regards, Hema
Top answer
1 of 3
36

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.

2 of 3
18

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.

🌐
Quora
quora.com › How-do-I-convert-long-long-to-unsigned-int-in-C-C
How to convert long long to unsigned int in C/C++ - Quora
Answer (1 of 7): The question is “How do I convert long long to unsigned int in C/C++?” First and foremost, you start by understanding your data and what you’re expressing with a “long long int”. If you can be sure that all of the possible values of your usage of a long long int can be expresse...
🌐
Quora
quora.com › How-do-you-convert-a-long-long-int-to-a-string-in-C-I-get-it-correctly-when-I-was-programming-it-in-C-but-I-cant-seem-to-find-an-equivalent-of-to_string-in-C-I-also-tried-the-sprintf-though-Im-not-sure-if-I-did-it
How do you convert a long long int to a string in C? I get it correctly when I was programming it in C++, but I can't seem to find an equ...
Answer (1 of 5): Answered as: How do you convert a long long int to a string in C? I get it correctly when I was programming it in C++, but I can't seem to find an equivalent of to_string() in C. I also tried the sprintf(), though I'm not sure if I did it correctly, but its error I’ll try to ans...
🌐
GeeksforGeeks
geeksforgeeks.org › c language › converting-string-to-long-in-c
Converting String to Long in C - GeeksforGeeks
July 23, 2025 - Here, we will see how to build a C Program For String to Long Conversion using strtol() function. ... The third argument denotes the base in which the number is represented. To know more about visit strtol() function.
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Int to unsigned long - Programming - Arduino Forum
May 7, 2015 - I have an int that I need to convert to an unsigned long. How would I do this?