Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Long long in c99

In the C99 standard they introduced long long. What is the purpose of this? In my (limited) C programming experience, I've only every seen a 4-byte int and an 8-byte long. For example, from Compiler Explorer: enter image description here

If long is already 8 then, why is it necessary to add another long long type? What does this do to the compiler/architecture?

like image 207
carl.hiass Avatar asked Jan 09 '21 22:01

carl.hiass


3 Answers

If long is already 8 then, why is it necessary to add another long long type? What does this do to the compiler/architecture?

"If long is already 8" is not always true as much code exists that relies on 32-bit long and int as 32 or 16 bits.

Requiring long as 64-bit would break code bases. This is a major concern.


Yet requiring long to remain 32-bit (and no long long) would not make for access to standard 64-bit integers, hence a rationale for long long.

Allowing long as either 32-bit or 64-bit (or others) allows for transition.

Various functions pass in/return long like fseek(), ftell(). They benefit from long being more than 32-bit for large file support.

Recommended practice encourages a wider long: "The types used for size_t and ptrdiff_t should not have an integer conversion rank greater than that of signed long int unless the implementation supports objects large enough to make this necessary." This relates to memory sizes exceeding 32-bit.


Perhaps in the future an implementation may use int/long/long long/intmax_t as 32/64/128/256 bits.

IAC, I see fixed width types intN_t increasing in popularity over long and long long. I tend to use fixed width types or bool, (unsigned) char, int/unsigned, size_t, (u)intmax_t and leave signed char, (unsigned) short, (unsigned) long, (unsigned) long long for special cases.

like image 192
chux - Reinstate Monica Avatar answered Sep 28 '22 15:09

chux - Reinstate Monica


The C standard only guarantees that an int can be (loosely speaking) 2 bytes, a long can be 4 bytes, and a long long can be 8 bytes.

In fact, MSVC still uses a 4 byte long even though it has a 4 byte int.

like image 26
dbush Avatar answered Sep 28 '22 15:09

dbush


The only relevant requirement for int and long, then and now, is that int must be at least 16 bits and long must be at least 32 bits. 16- and 32-bit systems both tend to have 32-bit long, and 64-bit machines were much less common in the late 1990s. So prior to C99, programmers could not portably rely on having a 64-bit integer type available at all. That problem was solved by the introduction of long long, which is required to be at least 64 bits. (I believe it was already provided by GCC and maybe other compilers as an extension).

These days, many (but not all) 64-bit systems use a 64-bit long and do not bother to make long long any bigger, so it is 64 bits as well and is in some sense redundant. Those are presumably the systems with which you're familiar, but they do not represent everything out there.

like image 20
Nate Eldredge Avatar answered Sep 28 '22 17:09

Nate Eldredge