Saif Bashar
Saif Bashar

Reputation: 21

Why short int and int has same size in 32bit architectures?

#include <iostream>
 
int main()
{
    std::cout << "bool:\t\t" << sizeof(bool) << " bytes\n";
    std::cout << "char:\t\t" << sizeof(char) << " bytes\n";
    std::cout << "wchar_t:\t" << sizeof(wchar_t) << " bytes\n";
    std::cout << "char16_t:\t" << sizeof(char16_t) << " bytes\n";
    std::cout << "char32_t:\t" << sizeof(char32_t) << " bytes\n";
    std::cout << "short:\t\t" << sizeof(short) << " bytes\n";
    std::cout << "int:\t\t" << sizeof(int) << " bytes\n";
    std::cout << "long:\t\t" << sizeof(long) << " bytes\n";
    std::cout << "long long:\t" << sizeof(long long) << " bytes\n";
    std::cout << "float:\t\t" << sizeof(float) << " bytes\n";
    std::cout << "double:\t\t" << sizeof(double) << " bytes\n";
    std::cout << "long double:\t" << sizeof(long double) << " bytes\n";
 
    return 0;
}

This will show that int and short has same size in 32 bit system but why? bool 1 byte
char 1 byte
wchar_t 1 byte
char16_t 2 bytes
char32_t 4 bytes
short 2 bytes
int 2 bytes
long 4 bytes
long long 8 bytes
float 4 bytes
double 8 bytes
long double 8 bytes

Upvotes: 0

Views: 929

Answers (2)

Pete Becker
Pete Becker

Reputation: 76498

The language definition imposes some requirement on the sizes and on the relative sizes of integral types. The standard doesn't care about 32-bit systems, 64-bit systems, or whatever else you might have.

The size requirements are:

char -- at least 8 bits
short -- at least 16 bits
int -- at least 16 bits
long -- at least 32 bits
long long -- at least 32 bits

The relative size requirements are that, in the list above, each type must be at least as large as the one that precedes it. (So, for example, if short is 32 bits, int must be at least 32 bits).

So, as far as the language definition is concerned, short and int can certainly be the same size. And on many systems they are.

Upvotes: 0

eerorika
eerorika

Reputation: 238461

Why short int and int has same size in 32bit architectures?

Ask yourself: Why should they not be the same size? The language standard specifies same minimum limits for the range of both types, and allows them to be the same size. On some language implementations this is the case.

Note that this is not universally true for all language implementations (whether 32 bit or not), and on some (such as those on x86 CPU architectures) the sizes do differ.

Upvotes: 1

Related Questions