1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Size and limits of Different Variable types in C

Discussion in 'C' started by lionaneesh, Jun 13, 2011.

  1. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    Note: This article is a continuation to my previous article on Variables in C , I suggest a glance over it before proceeding .

    In that article I talked about some C data types, Different types of keywords to declare some variables constants etc. In this tutorial we’ll be know why actually we need to use double and long definitions when we have ints , floats etc. So whats the waiting then lets get started.

    The ‘sizeof’ Operator


    The sizeof operator simply gives us the amount of storage in bytes required to store an object of the type of the operand.

    Syntax :-
    Code:
    sizeof unary-expression
    sizeof(type-name)
    

    Now to get the size(in bytes) of an integer we can simply use :-

    Code:
    sizeof(int)
    
    Data.c
    Code:
    #include<stdio.h>
    
    int main()
    {
        printf("Size of an integer %d\n",sizeof(int));
        printf("Size of a long %d\n",sizeof(long));
    
        printf("Size of a float %d\n",sizeof(float));
        printf("Size of a double %d\n",sizeof(double));
    
        return(0);
    }
    
    Output (32 bit compiler) :-

    Code:
    Size of an integer 4
    Size of a long 4
    Size of a float 4
    Size of a double 8
    
    Note: According to my compiler the size of long is same as the size of an int (As I am using a 32 bit compiler) but 64 bit compilers show different sizes for int and long. This is because The C standard doesn't specify the size in memory that each type can use, so it's left to the implementation.


    Limits of variables


    C has header file known as ‘limits.h’ which includes some constants pointing the limits of common C variable types and a header file known as ‘float.h’ which included constants pointing to the limits of floating point variables in C , Let’s have a look.


    limits.c

    Code:
    #include<stdio.h>
    #include<limits.h>
    #include<float.h>
    
    int main()
    {
        printf("Minimum Value of an integer %d\n",INT_MIN);
        printf("Maximum Value of an integer %d\n",INT_MAX);
    
        printf("Minimum Value of a long %d\n",LONG_MIN);
        printf("Maximum Value of a long %d\n",LONG_MAX);
    
        printf("Minimum Value of a float %d\n",FLT_MIN);
        printf("Maximum Value of a float %d\n",FLT_MAX);
    
        printf("Minimum Value of a double %d\n",DBL_MIN);
        printf("Maximum Value of a double %d\n",DBL_MAX);
    
        return(0);
    }
    
    Output (32 bit compiler) :-


    Code:
    Minimum Value of an integer -2147483648
    Maximum Value of an integer 2147483647
    Minimum Value of a long -2147483648
    Maximum Value of a long 2147483647
    Minimum Value of a float 0.000000
    Maximum Value of a float 340282346638528860000000000000000000000.000000
    Minimum Value of a double 0.000000
    Maximum Value of a double 179769313486231570000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000000000000000000000000000000000
    000000000000000.000000
    
    That's all for this tutorial stay tuned for more.
     

Share This Page