Advertisement



< Prev
Next >



C - Primitive Data Types and Sizes





In C, each variable has a specific data type, where a data type tells us the size, range and the type of a value that can be stored in a variable. In C, there are about seven primitive data types. These data types are : short, int, long, char, float, double and few of their variants.




Primitive data types are categorized into these parts







Primitive Data types in C


In C, the number of bytes used to store a data type depends on the Compiler(depending on the bit size of a compiler and also the OS). But irrespective of the bit-size of the compiler and OS, the following rules are followed, such as -

Data Type Size Range
char at least 1 byte -128 to 127
unsigned char at least 1 byte 0 to 255
short at least 2 bytes -32768 to 32767
unsigned short at least 2 bytes 0 to 65535
int at least 2 bytes -32768 to 32767
unsigned int at least 2 bytes 0 to 65535
long at least 4 bytes -2,147,483,648 to 2,147,483,647
unsigned long at least 4 bytes 0 to 4,294,967,295
float at least 2 bytes 3.4e-038 to 3.4e+038
double at least 8 bytes 1.7e-308 to 1.7e+308
long double at least 10 bytes 1.7e-4932 to 1.7e+4932





How to calculate range of a data type?


For many of us who like the reasoning behind equations. We will use a formula to calculate a minimum range and a maximum range for the data types like short, char, int and long.


Minimum Range- It is the minimum value that can be stored in a data type.
Maximum Range- It is the maximum value that can be stored in a data type.


The minimum range is calculated by -2(bits-1) and for the maximum range it is 2(bits-1)-1, while bits = size of a data type.

For example, A short variable of size takes at least 2 bytes(16 bits) in size. And, as per formula above. The minimum range is -215(-32768) and the maximum range is 215-1(32767). Hence, the range of values for short data type is -32768 to 32767.




short


C provides a variant of an integer data type called short. Short integer was designed to provide integer values of smaller range than an int data type.

  • In C, the number of bytes used to store a data type depends on the Compiler(depending on the bit size of a compiler and also the OS), although the size of a short variable is at least 16 bits or 2 bytes and never bigger than size of int.

    Hence, for a short variable of 2 bytes, the minimum value that can be stored in a short is -215 or -32768 and a maximum is 215-1 or +32767. So, it's range is -32768 to 0 to 32767.



  • Advertisement




    unsigned short


    When we already know that we only wish to store a positive short integer value then we could use a variant of an short data type called unsigned short. The unsigned short data type is used to store only positive short integer values.



    It stores double more positive short integer values than an short data type, because the highest bit which is usually occupied to store the sign of an integer value is now free and not used to store the sign of the integer.

    Hence, for an unsigned short variable of 2 bytes, the minimum value that can be stored is 0 and a maximum is 216 or 65535. So, it's range is 0 to 0 to 65535









    int


    In C, the number of bytes used to store a data type depends on the Compiler. Depending on the bit size of a compiler and also the OS, although the size of an int variable is at least 16 bits or 2 bytes and never bigger than long.

    For example -






    Compiler decides an appropriate size of a data type depending on the operating system and hardware for which it is written.


    The highest bit decides the sign of an integer i.e. if the highest bit is 1 then the integer value is negative and if the highest bit is 0 then the integer value is positive. The int data type could be signed and unsigned.

    By default an int data type stores both positive and negative integer values, hence it could also be termed as a signed int data type, which means it stores both positive and negative integer values.




    unsigned int


    When we already know that we wish to store a positive integer value then we could use a variant of an int data type called unsigned int. The unsigned int data type is used to store only positive integer values.



    It stores double more positive integer values than an int data type, because the highest bit which is usually occupied to store the sign of an integer value is now free and not used to store the sign of the integer.










    long


  • Depending on the bit size of a compiler and also the OS, the size of a long variable in C is at least 32 bits or 4 bytes.



    The minimum range of a value that can be stored in a long is -231 while, the maximum is 231-1
    Note : Because of a wider range than int, long data type is used to store a larger number.







  • unsigned long


    When we already know that we wish to store a positive long integer value then we could use a variant of an long data type called unsigned long. The unsigned long data type is used to store only positive long integer values.



    It stores double more positive integer values than an long data type, because the highest bit which is usually occupied to store the sign of an integer value is now free and not used to store the sign of the integer.






    < Prev
    Next >
    < The First C Program
    Variables, Keywords, Constants >



    Advertisement

    Please Subscribe

    Please subscribe to our social media channels for daily updates.


    Decodejava Facebook Page  DecodeJava Twitter Page




    Advertisement



    Notifications



    Please check our latest addition

    C#, PYTHON and DJANGO


    Advertisement