I’ve been searching for a while but couldn’t find a definite answer to this apparently simple question: what is the default length of an int?
I know that by default, an int is signed. But is it short or long?
According to the “Fundamental data types”table found in the following page, an int is a long int by default (4 bytes).
http://www.cplusplus.com/doc/tutorial/variables/
Is it always true, or does this depend on the OS (32bit/64bit), the compiler or other things?
It depends on the compiler implementor. An
intis supposed to be the best “native” length for the platform. Best native here typically refers to whichever size is most handy/efficient/fast for the targeted processor to work with. Often you can expectintto have the same size as the processor’s (integer) registers.As others have pointed out, there are certain relationships about the various integer types’ sizes that the compiler must adhere to, so it’s the implementor is not free to choose anything. For instance,
intcan’t be larger thanlong, and so on.You often talk about programming models in relationship with issues like these, e.g. a compiler can chose to make the various types different sizes depending on the chosen model.