Question about data types

I'm new to c++ and I am only a beginner programmer with experience in C, Javascript, TI-BASIC and C++ so please forgive my newbishness.

I was wondering, how come there are different data-types that represent the same values? In the book I'm working through it explains the data types as follows:

INT = 4 bytes
Short Int = 2 bytes
Long Int = 4 bytes
Char = 1 byte
boole = 1 byte
float = 4 bytes
double float = 8 bytes
Long long int = 8 bytes

So like with INT and Long int. They take up exactly the same amount of space and they are both integers. How come they need two different categories?
The number of bytes used in the object representation of, say, int is implementation-defined. All that C++ requires is that long int can't be narrower than int, and can't be wider than long long int etc.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main() 
{
    std::cout << "      sizeof(short): " << sizeof(short) << '\n'
              << "        sizeof(int): " << sizeof(int) << '\n'
              << "       sizeof(long): " << sizeof(long) << '\n'
              << "  sizeof(long long): " << sizeof(long long) << '\n' ;
              
    std::cout << "      sizeof(float): " << sizeof(float) << '\n'
              << "     sizeof(double): " << sizeof(double) << '\n'
              << "sizeof(long double): " << sizeof(long double) << '\n' ;
}


On one implementation:
1
2
3
4
5
6
7
      sizeof(short): 2
        sizeof(int): 4
       sizeof(long): 8
  sizeof(long long): 8
      sizeof(float): 4
     sizeof(double): 8
sizeof(long double): 16

http://coliru.stacked-crooked.com/a/fb3e9214f1feeb54

On another:
1
2
3
4
5
6
7
      sizeof(short): 2
        sizeof(int): 4
       sizeof(long): 4
  sizeof(long long): 8
      sizeof(float): 4
     sizeof(double): 8
sizeof(long double): 8

http://rextester.com/OGB65684

For fixed-width integer types like std::int32_t, see: http://en.cppreference.com/w/cpp/header/cstdint
Last edited on
It depends on the platform's implementation of the data type. The standard dictates that sizeof(long int)>=sizeof(int), therefor it is perfectly possible for them to have the same size. On some platforms, however, you could also have long int be longer than int

edit: ninja'd :/
Last edited on
Topic archived. No new replies allowed.