3

Is it correct that a long in C has a size of 4 bytes for a 32-bit platform and 8 bytes for a 64-bit platform?

John
  • 59
  • 1
  • 5
  • 1
    Typically, but not necessarily. If you want a type of a fixed size, use `int32_t` or `int64_t`. – dbush Jun 22 '18 at 22:25
  • `long` has a minimum size of 32 bits, that's it. – Weather Vane Jun 22 '18 at 22:26
  • 2
    Depends on the specific platform. `long` is at least 32 bits, but quite often it's 64 bits. `int` is at least 16 bits and it never has more bits than `long`. These are all the guarantees the language makes. – AlexP Jun 22 '18 at 22:30
  • 2
    https://en.wikipedia.org/wiki/64-bit_computing#64-bit_data_models – Ismail Badawi Jun 22 '18 at 22:40
  • Depends on the *data model*, they have formal names. LLP64 and LP64 are the common ones. https://en.wikipedia.org/wiki/64-bit_computing#64-bit_data_models. – Hans Passant Jun 22 '18 at 22:40
  • Sorry, hans, `LLP64 and LP64 are the common ones.` it is not common, it is undefined. – wildplasser Jun 22 '18 at 22:46

2 Answers2

4

The size of long (and the sizes of objects generally) is determined by the C implementation, not the platform programs execute on.

Generally speaking, a C implementation is a compiler plus the libraries and other supporting software needed to run C programs.1 There can be more than one C implementation for a platform. In fact, one compiler can implement multiple C implementations by using different switches to request various configurations.

A general C implementation typically uses sizes for short, int, and long that work well with the target processor model (or models) and give the programmer good choices. However, C implementations can be designed for special purposes, such as supporting older code that was intended for a specific size of long. Generally speaking, a C compiler can write instructions for whatever size of long it defines.

The C standard imposes some lower limits on the sizes of objects. The number of bits in a character, CHAR_BIT, must be at least eight. short and int must be capable of representing values from −32767 to +32767, and long must be capable of representing −2147483647 to +2147483647. It also requires that long be capable of representing all int values, that int be capable of representing all short values, and short be capable of representing all signed char values. Other than that, the C standard imposes few requirements. It does not require that int or long be a particular size on particular platforms. And operating systems have no say in what happens inside a programming language. An operating system sets requirements for running programs and interfacing with the system, but, inside a program, software can do anything it wants. So a compiler can call 17 bits an int if it wants, and the operating system has no control over that.

Footnote

1 The C 2011 standard (draft N1570) defines an implementation, in clause 3.12, as a “particular set of software, running in a particular translation environment under particular control options, that performs translation of programs for, and supports execution of functions in, a particular execution environment.”

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • 1
    C implementations generally need to conform to the ABI of the OS. – Barmar Jun 22 '18 at 22:38
  • @Barmar: The OS is part of the implementation. – Eric Postpischil Jun 22 '18 at 22:40
  • If that's true, then 32-bit OS = 32-bit C, 64-bit OS = 64-bit C, right? – Barmar Jun 22 '18 at 22:41
  • @Barmar: No. General-purpose computers are, for practical purposes, Turing complete. This means you can execute any computation on any general-purpose computer (up to the physical bounds of the hardware). You could write a C compiler with 16-bit `int` on a 64-bit machine, and you could write a C compiler with 64-bit `int` on a 16-bit machine. The C standard does not require a C implementation to provide a direct interface to the operating system. It only requires the standard library routines, and those can be written to perform any necessary adjustments to work with the operating system. – Eric Postpischil Jun 22 '18 at 22:44
1

No. It is up to the implementation!

The only rules are char must be CHAR_BIT wide, and the sizes must be: char <= short <= int <= long <= long long, and char must be at least 8 bits, short at least 16 bits, long at least 32 bits, and long long at least 64 bits.

So actually all integer types (except long long) could be 32-bits wide and the C Standard is perfectly fine with that as long as CHAR_BIT is set to 32.

Zan Lynx
  • 53,022
  • 10
  • 79
  • 131
  • i dont think so. sizeof(char) is defined as 1. it would be weird to have all sizes be a multiple of 4 bytes – pm100 Jun 22 '18 at 22:41
  • @pm100: sizeof is defined in numbers of chars. Not bytes. So sizeof(char) is always 1. Even if it is 32 bits wide. – Zan Lynx Jun 22 '18 at 22:42
  • @pm100 See https://stackoverflow.com/questions/2215445/are-there-machines-where-sizeofchar-1-or-at-least-char-bit-8 – Zan Lynx Jun 22 '18 at 22:45
  • @pm100: The C standard defines a byte as an “addressable unit of data storage large enough to hold any member of the basic character set of the execution environment.” A C implementation may use 32-bit bytes. – Eric Postpischil Jun 22 '18 at 22:47
  • @ZanLynx: There are additional rules. `CHAR_BIT` must be at least 8, and, in effect, `short` and `int` must be at least 16 bits, and `long` must be at least 32. – Eric Postpischil Jun 22 '18 at 22:50
  • @ZanLynx In C, a `char` is one "byte" *by definition*. A byte, as C defines the word, is at least 8 bits, but can be more. So `sizeof` is defined in terms of chars and, equivalently, in terms of bytes. – Keith Thompson Jun 22 '18 at 22:59
  • just saying it would be wierd to have a computer with no object smaller that 32 bits – pm100 Jun 22 '18 at 23:52