Blogger templates

Pages

Wednesday, 20 June 2012

Difference Between an Integer , short and long Variables

Int , short , long

Primary Data Types themselves could be of several types. For example, a char could be an unsigned char or a signed char. Or an int could be a short int or long int. Due these variations in the primitive data types, its very difficult to understand the concept. So let us take a closer look at these variations of primary data types in the tutorial:

=> Integers , long and short:
  • The range of an integer constant depends upon the compiler.
  • For a 16 bit Compiler like Turbo C or Turbo C++ the range is -32768 to 32767. 
  • For a 32 bit compiler like Visual Studio or gcc the range would be -2147483648 to 2147483647.
  • C offers a variation of the Integer data type i.e short and long integer values. 
  • The intention of providing these variations is to provide Integers with different ranges whenever possible. 
 


Each compiler can decide appropriate sizes of int , long and short depending on the operating system and hardware, for which it is being written, subject to following Rules:
  • shorts are atleast 2 bytes big.
    • longs are atleast 4 bytes big.
      • shorts are never bigger than ints.
        • ints are never bigger than longs.

                    Compiler    short        int         long   
              16-bit (Turbo C/C++)           2      2      4
              32-bit (Visual Studio )           2      4      4



          => long Variables:
          • long variables which hold long Integers are declared using the keyword long.
          Example: 
                         long int a;
                         long int xyz; 

          • long integers causes the Program to run a bit slower, but the Range of values that we can use is Expanded tremendously. 
          • The value of long Integer typically can vary from  -2147483648  to  2147483647.
          => short Variables:
          • short variables which hold short integers are declared using keyword short as shown
          Example:
                        short int b;
                        short int cool;
          • short needs Less space in Memory and thus helps in speed of Program Execution.
          NOTE: C allows to write short int to short and of long int to long. So declaration made very easy.

          Example:
                         long a;
                         long xyz;
                         short b;
                         short cool ;

          NOTE: Sometimes we come across situations where the constant is small enough to be an int , but still we want to give it as much storage as a long. In such cases , we add the suffix 'L' or '1' at the end of the number i.e 23L

                                                                         That's All

          0 comments:

          Post a Comment