81

Is it always true that long int (which as far as I understand is a synonym for long) is 4 bytes?

Can I rely on that? If not, could it be true for a POSIX based OS?

7
  • 13
    Just to remind you guys that sizeof takes into consideration of padding and alignment. It's standard compliant to have a sizeof(unsigned long) of 8 bytes but the overflow behaves like 4 bytes. It's way wrong trying to use sizeof and CHAR_BITS to calculate the limit of an integer type. Use limits.h when it should be used. Mar 7, 2016 at 16:07
  • 11
    You really should read the (abridged first, then the full version of the) C-faq, which originated on Usenet at a time where it was the place where people with knowledge on a subject gathered in the same place (now, it's widespread amongst several sites...), and therefore it led to incredibly thourough and well done FAQs. See c-faq.com/versions.html . Reading it is an eye opener on this question and on dozens more you probably don't even know you need to know about yet (section 5 - null pointers - is one containing FAQs I was shocked (and grateful) to discover) Mar 7, 2016 at 17:43
  • 7
    no, it's 8 on my pc
    – RiaD
    Mar 7, 2016 at 19:06
  • 3
    Possible duplicate of Range of values in C Int and Long 32 - 64 bits
    – T.E.D.
    Mar 7, 2016 at 19:51
  • 4
    It's 8 on most 64bit systems (e.g. x86-64 Linux and OS X). Windows is the major exception, because so much code written for 32bit Windows was written for 32bit Windows, with little to no effort made to write portable code that didn't make assumption. Also for ABI reasons: I think some structs have longs in their definitions, but they don't need to be 64bit. So MS decided to make long 32bit to keep structs the same size in their ABIs and for existing codebases. Mar 8, 2016 at 3:13

10 Answers 10

121

The standards say nothing regarding the exact size of any integer types aside from char. Typically, long is 32-bit on 32-bit systems and 64-bit on 64-bit systems.

The standard does however specify a minimum size. From section 5.2.4.2.1 of the C Standard:

1 The values given below shall be replaced by constant expressions suitable for use in #if preprocessing directives. Moreover, except for CHAR_BIT and MB_LEN_MAX, the following shall be replaced by expressions that have the same type as would an expression that is an object of the corresponding type converted according to the integer promotions. Their implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign.

...

  • minimum value for an object of type long int

    LONG_MIN -2147483647 // −(2^31−1)

  • maximum value for an object of type long int

    LONG_MAX +2147483647 // 2^31−1

This says that a long int must be a minimum of 32 bits, but may be larger. On a machine where CHAR_BIT is 8, this gives a minimum byte size of 4. However on machine with e.g. CHAR_BIT equal to 16, a long int could be 2 bytes long.

Here's a real-world example. For the following code:

#include <stdio.h>

int main ()
{
    printf("sizeof(long) = %zu\n", sizeof(long));
    return 0;
}

Output on Debian 7 i686:

sizeof(long) = 4

Output on CentOS 7 x64:

sizeof(long) = 8

So no, you can't make any assumptions on size. If you need a type of a specific size, you can use the types defined in stdint.h. It defines the following types:

  • int8_t: signed 8-bit
  • uint8_t: unsigned 8-bit
  • int16_t: signed 16-bit
  • uint16_t: unsigned 16-bit
  • int32_t: signed 32-bit
  • uint32_t: unsigned 32-bit
  • int64_t: signed 64-bit
  • uint64_t: unsigned 64-bit

The stdint.h header is described in section 7.20 of the standard, with exact width types in section 7.20.1.1. The standard states that these typedefs are optional, but they exist on most implementations.

15
  • 5
    But these typedefs are in the standard! It's just implementation defined whether they actually exist and which types they represent!
    – Vincent
    Mar 7, 2016 at 14:10
  • 10
    @nouney don't confuse minimum number of bits and the minimum number of bytes. I don't remember anything that forbids 1 == sizeof(char) == sizeof(int). Could you provide a specific quote from the standard that says that "int is at least 2 bytes"?
    – jfs
    Mar 7, 2016 at 17:40
  • 4
    @nouney char can be larger than 8 bits. Of course having it be, say, 32 bits (still sizeof 1, of course) makes working with data like 8 bit text encodings painful, but generally platforms like that aren't used for text processing.
    – hyde
    Mar 7, 2016 at 18:43
  • 6
    I wouldn't say that long is typically 64-bit, since it isn't on Windows, and Windows is kind of widely used. The standard itself doesn't define this, of course, so both implementations are standard-conforming, but the end result is tons of unix C (and C++) code that truncates 64-bit pointers on Windows (just stop storing pointers in integers already, dammit! :D). Of course, it's fundamentally caused by using a type that by standard has a range of [−2147483647, +2147483647] to store values bigger than that, which is kind of ridiculous, but oh well :)
    – Luaan
    Mar 8, 2016 at 9:47
  • 3
    I've always been learned that sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) and it's all implementation defined.
    – Chris K
    Mar 8, 2016 at 17:33
39

No, neither the C standard nor POSIX guarantee this and in fact most Unix-like 64-bit platforms have a 64 bit (8 byte) long.

2
  • Ints on most 32-bit and up platforms are 32 bit (AFAIK, anyway), what would be the point of having a long that's the same size as an int? On my system (Intel, 64-bit Linux, gcc) it's 32 for int, 64 for long.
    – jamesqf
    Mar 7, 2016 at 18:13
  • 7
    @jamesqf: On the vast majority of platforms released between 1980 and 2005, short was 16 bits, long was 32 bits, and "int" was 16 or 32 bits at the convenience of the implementation. Having fixed-size types for 16 and 32 bit types before C99 added stdint.h was helpful. As a nice additional complicating factor with the latter, though, on a system with 32-bit "int", int32_t may compatible with "int" or with "long", but conforming implementations are not allowed to make it compatible with both.
    – supercat
    Mar 7, 2016 at 20:10
19

Use code sizeof(long int) and check the size. It will give you the size of long int in bytes on the system you're working currently. The answer of your question in particular is NO. It is nowhere guaranteed in C or in POSIX or anywhere.

5
  • 2
    IMO, that does not answer the question.
    – nouney
    Mar 7, 2016 at 13:14
  • 6
    It actually helped me :) Mar 7, 2016 at 13:25
  • @nouney how does stating that there is no exact size garuanteed by the C standards, or POSIX standards not answer the question? it's not the best answer, but certainly not wrong.
    – Leliel
    Mar 7, 2016 at 23:22
  • 3
    @Leliel because the anwser was just "use sizeof" when I posted the comment.
    – nouney
    Mar 8, 2016 at 7:15
  • 1
    sizeof(long int) factors padding and alignment restrictions into its result. It's perfectly valid for sizeof to return "16", even if you're only allowed to use four of those bytes.
    – Mark
    Mar 8, 2016 at 20:40
13

As pointed out by @delnan, POSIX implementations keep the size of long and int as unspecified and it often differs between 32 bit and 64 bit systems.

The length of long is mostly hardware related (often matching the size of data registers on the CPU and sometimes other software related issues such as OS design and ABI interfacing).

To ease your mind, sizeof isn't a function, but a compiler directive*, so your code isn't using operations when using sizeof - it's the same as writing a number, only it's portable.

use:

sizeof(long int)

* As Dave pointed out in the comments, sizeof will be computed at runtime when it's impossible to compute the value during compilation, such as when using variable length arrays.

Also, as pointed out in another comment, sizeof takes into consideration the padding and alignment used by the implementation, meaning that the actual bytes in use could be different then the size in memory (this could be important when bit shifting).

If you're looking for specific byte sized variables, consider using a byte array or (I would assume to be supported) the types defined by C99 in stdint.h - as suggested by @dbush.

4
  • 4
    Not (just) hardware. You can run a 32 bit Linux on 64 bit hardware, and you can even use the x32 ABI where data types are sized as on 32 bit hardware but the hardware actually runs in 64 bit mode.
    – user395760
    Mar 7, 2016 at 13:33
  • @delnan , yap, that's true. I should probably clarify this by changing usually to mostly and add a info in the parenthesis.
    – Myst
    Mar 7, 2016 at 13:35
  • @delnan , I didn't mean to step over your answer, I just thought it would be best to clarify that sizeof is a compiler directive, to remove any perceived reasons anyone might have for guessing at the size.
    – Myst
    Mar 7, 2016 at 13:44
  • 1
    Small correction: sizeof is computed at runtime when (and if) you use a Variable Length Array or variably modified type in C99 or later (or 'gnu89' as a GCC extension) but most code doesn't do this. Mar 8, 2016 at 1:00
13

The standard says nothing about the size of long int, so it is dependent on the environment which you are using.

To get the size of long int on your environment you can use the sizeof operator and get the size of long int. Something like

sizeof(long int)

C standard only requires the following points about the sizes of types

  • int >= 16 bits,
  • long >= 32 bits,
  • long long (since C99) >= 64 bits
  • sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)
  • sizeof(char) == 1
  • CHAR_BIT >= 8

The remaining are implementations defined, so it's not surprised if one encountered some systems where int has 18/24/36/60 bits, one's complement signed form, sizeof(char) == sizeof(short) == sizeof(int) == sizeof(long) == 4, 48-bit long or 9-bit char like Exotic architectures the standards committees care about and List of platforms supported by the C standard

The point about long int above is completely wrong. Most Linux/Unix implementations define long as a 64-bit type but it's only 32 bits in Windows because they use different data models (have a look at the table here 64-bit computing), and this is regardless of 32 or 64-bit OS version.

Source

13
+150

When we first implemented C on ICL Series 39 hardware, we took the standard at its word and mapped the data types to the natural representation on that machine architecture, which was short = 32 bits, int = 64 bits, long = 128 bits.

But we found that no serious C applications worked; they all assumed the mapping short = 16, int = 32, long = 64, and we had to change the compiler to support that.

So whatever the official standard says, for many years everyone has converged on long = 64 bits and it's not likely to change.

4
  • 2
    Really? At least on GCC/Glibc/GNU/Linux, platforms seem to be converging on int = 32, long long = 64, long = native word size (either 32 or 64) and certainly not long = 64. E.g. on x86, which is not exactly a niche architecture, long is 32 bit. OSX, Solaris, z/OS, SysV are some other OSs that follow this convention. Mar 8, 2016 at 1:44
  • For an even more widely used system than OSX, Linux, z/OS, or SysV, there is Windows, which has yet another different convention than yours (always 64) or those (always the same as a pointer), namely always 32 bits. Mar 8, 2016 at 13:13
  • IMHO, the proper remedy would be to have a standard means by which code can specify what kind of "int", "long", etc. are required within certain contexts [typically per file, so that a header file that declares a function as accepting "int" will always have the same meaning]. Having a 64-bit system emulate a 16-bit or 32-bit "int" should be fairly straightforward, and would probably be more reliable than trying to adjust code given all the odd little nuances in C (like the fact that on a 64-bit system, uint32_t modular multiplication doesn't work for all values).
    – supercat
    Mar 8, 2016 at 15:30
  • Most compilers for x86_16, x86_32, 32-bit ARM, AVR, PIC and 8051 do use 32 bit integers for long, half of them use 16 bit integers for int. But i think the sizeof(short)*CHAR_BIT==16 is probably true. Jul 8, 2021 at 11:51
7

The compiler determines the size based on the type of hardware and OS.

So, assumptions should not be made regarding the size.

1

No, you can't assume that since the size of the “long” data type varies from compiler to compiler.
Check out this article for more details.

1

From Usrmisc's Blog:

The standard leaves it completely up to the compiler, which also means the same compiler can make it depend on options and target architecture.

So you can't.

Incidentally even long int could be the same as long.

1
  • 1
    What's wrong with this answer? Why did someone give her a negative point? After all, it is the answer to the question and is distinguished by its simplicity compared to other answers, which may be important for some people who are looking for an answer. Isn't it? Jul 6, 2020 at 16:26
0

Short answer: No! You cannot make fixed assumptions on the size of long int. Because, the standard (C standard or POSIX) does not document the size of long int (as repeatedly emphasized). Just to provide a counter example to your belief, most of the 64 bit systems have long of size 64! To maximize portability use sizeof appropriately.

Use sizeof(long int) to check the size, it returns the size of long in bytes. The value is system or environment dependent; meaning, the compiler determines the size based on the hardware and OS.

Not the answer you're looking for? Browse other questions tagged or ask your own question.