Thread: confused about available bit sizes in various types

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808

    confused about available bit sizes in various types

    hi as an exersize i was asked to write a program that will print out the size of int short long float double and long double. the following code is what i came up with (copied from the example in the chapter)

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        printf("size of int is: %zu\n", sizeof(int));
        printf("size of short is: %zu\n", sizeof(short));
        printf("size of long is: %zu\n", sizeof(long));
        printf("size of float is: %zu\n", sizeof(float));
        printf("size of double is: %zu\n", sizeof(double));
        printf("size of long double is: %zu\n", sizeof(long double));
        return 0;
    }
    when run i get the following answers int = 4 short = 2 long = 8 float = 4 double = 8 and long double = 16

    the text said that a signed int can be upto 32767 on a 16 bit machine (mine is 64) but as an int only has 4 slots ie 1111 the max number is 15!!!!!!!!!!!!!!!!! so how do i get any number over that

    coop

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    All integer sizes depends on the platform.
    In old compilers for 16 bits DOS, int was 16 bits long, on modern OSs, it is 32.

    Some rare machines use short as 32 bits long, but it is usually 16. Even char is dependent of the processor, very old computers (I believe, PDP-7) had 9 bits chars. That's why limits.h has a CHAR_BITS symbol defined.

    long int depends on the OS: Windows uses 32 bits, but Unixes (Linux, BSD, MacOS), in x86-64 mode, uses 64.
    float and double are both 32 and 64 bits long, respectively, accondingly to IEEE-754 standard.
    long double is 80 bits long (yes! 80 bits: 10 bytes!), but the compiler aligns this type to a 16 bytes (128 bits) bondary (it is easier to deal with sizes by powers of 2).

    For 64 bits modes, take a look at "64 bits models" in this article. In this table you can change the acronyms (sorry, english is not my primary language - I believe "initials" is more appropriate term) LLP64 and LP64 to IL32P64 and I32LP64, respectively. (I for int, L for long and P for Poiinter).

    Your copiler should provide symbols telling you the appropriate sizes. For example, with GCC, the symbols __SIZEOF_???__ are automatically defined (my GCC is amd64 by default):

    Code:
    $ gcc -dM -E - < /dev/null | grep SIZEOF
    #define __SIZEOF_FLOAT80__ 16
    #define __SIZEOF_INT__ 4
    #define __SIZEOF_POINTER__ 8
    #define __SIZEOF_LONG__ 8
    #define __SIZEOF_LONG_DOUBLE__ 16
    #define __SIZEOF_SIZE_T__ 8
    #define __SIZEOF_WINT_T__ 4
    #define __SIZEOF_PTRDIFF_T__ 8
    #define __SIZEOF_FLOAT__ 4
    #define __SIZEOF_FLOAT128__ 16
    #define __SIZEOF_SHORT__ 2
    #define __SIZEOF_INT128__ 16
    #define __SIZEOF_WCHAR_T__ 4
    #define __SIZEOF_DOUBLE__ 8
    #define __SIZEOF_LONG_LONG__ 8
    Sizes in bytes.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    PS: Some microcontrollers (Microchip's PIC, for example) uses different sizes than x86.
    If you don't want to deal with precompiler symbols, use stdint.h and the int??_t or uint??_t typedefs to make sure you are using the appropriate integer type size.

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by cooper1200 View Post
    the text said that a signed int can be upto 32767 on a 16 bit machine (mine is 64) but as an int only has 4 slots ie 1111 the max number is 15!!!!!!!!!!!!!!!!! so how do i get any number over that coop
    Ahhhh... sizeof() returns the size in BYTES, not bits... The binary 0b1111 is only 4 BITS long and 0b1111 is 15, in decimal.

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    Quote Originally Posted by flp1969 View Post
    Ahhhh... sizeof() returns the size in BYTES, not bits... The binary 0b1111 is only 4 BITS long and 0b1111 is 15, in decimal.

    so id i multiply the 4 by 8 (8 bits to a byte) i get 32 bits ie a big number thanks that makes more sense now.

    can i assume that as its using 4 lots of 8 its compiling for a 32 bit cpu as i am using codeblocks as an ide do i need to change a setting somewhere to make it 64 bits
    many thanks
    coop

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by cooper1200 View Post
    so id i multiply the 4 by 8 (8 bits to a byte) i get 32 bits ie a big number thanks that makes more sense now.
    That's it!

    Nowadays, most processors use 8 bits as the size of 1 byte, but there are, still, some systems which use different sizes for BYTE (the word is a joke someone made about "eating" the smallest amout possible of bits [which is a 'little bite']).... The type char is defined to be 1 byte long (somewhere in the ISO 9989 standard), but the number of bits of a char is defined in the header limits.h in the symbol CHAR_BITS, so the portable way is to multiply to CHAR_BITS (I think).

    But, as said, most processors use 8 bits...

  7. #7
    Registered User
    Join Date
    Apr 2019
    Posts
    62
    Quote Originally Posted by cooper1200 View Post
    the text said that a signed int can be upto 32767 on a 16 bit machine (mine is 64) but as an int only has 4 slots ie 1111 the max number is 15!!!!!!!!!!!!!!!!! so how do i get any number over that
    That is the number of bytes, not bits. An int on most platforms is 4 bytes, or 32 bits. This give a range in the order of +2 billion to -2 billion for signed and 4 billion to 0 for unsigned. On a 16-bit machine, such as MS-DOS when targeting realmode, your text is correct, an int will have a size of 2 bytes, or 16 bits.

    Also note that the "bits" of a machine get complicated. On an AMD64 machine (which includes Intel machines), the size of an int is still 4 bytes, but the size of an int* is 8 bytes. Confusingly, the CPU does actually have 64-bit general purpose registers and could potentially use 64-bit values for everything, but as that wastes memory for no real reason (32-bit int already has a range of billions, most applications don't need larger ints), they go for a hybrid 32-bit/64-bit approach.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 01-22-2014, 09:48 AM
  2. File sizes
    By bradszy in forum C++ Programming
    Replies: 18
    Last Post: 04-28-2008, 05:24 AM
  3. How to convert integral types into pointer types?
    By rohit99 in forum C++ Programming
    Replies: 3
    Last Post: 03-20-2008, 09:57 AM
  4. arrays with different sizes..
    By zoso123 in forum C Programming
    Replies: 8
    Last Post: 06-30-2006, 04:24 PM
  5. SQL Variable sizes
    By jverkoey in forum Tech Board
    Replies: 4
    Last Post: 01-23-2005, 05:59 PM

Tags for this Thread