Home  >  Article  >  Backend Development  >  Is `long` Guaranteed to be at Least 32 Bits in C ?

Is `long` Guaranteed to be at Least 32 Bits in C ?

Susan Sarandon
Susan SarandonOriginal
2024-10-26 18:10:30670browse

Is `long` Guaranteed to be at Least 32 Bits in C  ?

Is long Guaranteed to be at Least 32 Bits?

The C Standard dictates the following order of precedence in terms of data storage for integral fundamental types:

sizeof(char) ≤ sizeof(short int) ≤ sizeof(int) ≤ sizeof(long int)

Further clarification on the size of char is provided in section 1.7/1:

The fundamental storage unit in the C++ memory model is the byte. A byte is at least large enough to contain any member of the implementation’s basic character set and is composed of a contiguous sequence of bits, the number of which is implementation-defined.

This suggests that sizeof(char) can be anywhere from 1 to n bits, where n is implementation-dependent. However, the C Standard also requires knowledge of the C Standard (1.2/1), which defines minimum limits for the values that long can accommodate:

LONG_MIN = -2147483647 // -(2^31 - 1)
LONG_MAX = +2147483647 //   2^31 - 1

Since long must be able to hold both positive and negative values up to these limits, it is logically deduced that long must be at least 32 bits in size.

In short, while the C Standard leaves the number of bits in a byte unspecified, the minimum limits imposed on LONG_MIN and LONG_MAX imply that long must have at least 32 bits of storage.

The above is the detailed content of Is `long` Guaranteed to be at Least 32 Bits in C ?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn