Search code examples
c++typessizeofunsigned

Is unsigned long long fixed to an int(4 byte) range, if so, why is it 8 bytes in memory?


Edit: Stupid question, I overlooked the format identifiers.

I had a program grab the size of a few unsigned types and their maximum value. This brought an anomaly to my attention, the fact that even thought unsigned long long is 8 bytes, it's range seems to be fixed to a 4 byte range. (char is a guaranteed 8-bit byte)

Question
I understand that unsigned long long is only defined by the standards to be at least large enough to hold an int (or rather >= long int >= int transitively). However, why is it using 8 bytes of memory instead of being the same size as int if it's limited to the range of 4 bytes too?

unsigned char:             255         1*sizeof(char)           
unsigned short:            65535       2*sizeof(char)        
unsigned short int:        65535       2*sizeof(char)       
unsigned int:              4294967295  4*sizeof(char)            
unsigned long int:         4294967295  4*sizeof(char)            
unsigned long long int:    4294967295  8*sizeof(char)  //range[0..18446744073709551615]       
unsigned long:             4294967295  4*sizeof(char)            
unsigned long long:        4294967295  8*sizeof(char)  //range[0..18446744073709551615]

Here is the source I used:

#include <iostream>
#include <cstdio>

int main(void){

    std::cout << "Type sizes: (multiples of char)"<< std::endl << "char: " << sizeof(char) << std::endl <<\
    "short: " << sizeof() << std::endl <<\
    "short int: " << sizeof(short int) << std::endl << std::endl <<\
    "int: " << sizeof(int) << std::endl <<\
    "long int: " << sizeof(long int) << std::endl <<\
    "long long int: " << sizeof(long long int) << std::endl << std::endl <<\
    "long: " << sizeof(long) << std::endl <<\
    "long long: " << sizeof(long long) << std::endl << std::endl <<\
    "float: " << sizeof(float) << std::endl <<\
    "double: " << sizeof(double) << std::endl;

    unsigned char c = -1;
    unsigned short s1 = -1;
    unsigned short int s2 = -1;

    unsigned int i = -1;
    unsigned long int i1 = -1;
    unsigned long long int i2 = -1;

    unsigned long l = -1;
    unsigned long long l1 = -1;

    printf("\
            \nUnsigned Max:     \n\
            \nchar:             %u\
            \nshort:            %u\
            \nshort int:        %u\
            \nint:              %u\
            \nlong int:         %u\
            \nlong long int:    %u\
            \nlong:             %u\
            \nlong long:        %u\
            ", c, s1, s2, i, i1, i2, l, l1);

    return 0;
}

Solution

  • You're using the wrong flag for printf, the values are actually alright but printf isn't displaying them correctly. Try %llu for unsigned long long.

    More info on printf (flags): http://www.cplusplus.com/reference/cstdio/printf/