Why Are Both int and long 4 Bytes on 64-Bit Windows in C?

0
6
Asked By TechWhiz88 On

I'm curious why, on 64-bit Windows systems, both 'int' and 'long' are defined as 4 bytes in C. I've noticed that on many 32-bit systems, such as Windows and Linux, they are also 4 bytes. However, on a 64-bit Linux distribution, 'int' remains 4 bytes while 'long' expands to 8 bytes. Why does 64-bit Windows keep both types at 4 bytes? Is this a decision based on the compiler, or is it related to some standard for the operating system? By the way, I'm using MinGW, and I found that even Microsoft's official documentation states both 'int' and 'long' are 4 bytes.

3 Answers

Answered By CodeNinja45 On

It seems like a lot of Windows applications would have encountered issues if they changed 'long' to 8 bytes. Remember back in the DOS days, 'int' was just 2 bytes and 'long' was 4 bytes. A bunch of programmers who started with DOS got used to assuming 'long' could hold a 4-byte value. On Linux, though, the expectation is that developers will recompile software for different systems, so changing 'long' to 8 bytes isn't as big of a deal there.

Answered By DevGuru77 On

The main reason for this is to ease the transition from 32-bit to 64-bit Windows. Many core Windows features are based on binary serialization, like COM, and altering the data type sizes could lead to major compatibility issues.

Answered By ByteBuster02 On

Ultimately, it doesn't significantly impact most programs. The ISO C standard just requires 'long' to be at least 4 bytes and 'int' to be at least 2 bytes. It's curious that on Linux 'long' is 8 bytes, but on Windows, it's kept to 4 bytes, likely due to your use of MinGW. By the way, I've heard it suggested that 'int' was often designed around the fastest memory fetch width based on CPU architecture, but this seemed to change at 64 bits to avoid wasting memory and to optimize cache usage.

QuickThinker56 -

Yeah, I noticed MSVC keeps 'long' at 4 bytes too, which reinforces this idea.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.