Sunday, 1 July 2012

C Programming : Data Types

C Programming Data Types

In C, variables(datas) should be declared before it can be used in program. Data types are the keywords, which is used for assigning a type to a variable.

Data types in C:

Data values passed in a program may be of different types. Each of these data types are represented differently within the computer’s memory and have different memory requirements. These data types can be augmented by the use of data type qualifiers/modifiers.
  1. Fundamental Data Types
    • Integer types
    • Floating Type
    • Character types
  2.  Derived Data Types
    • Arrays
    • Pointers
    • Structures
    • Enumeration

      int:

      It is used to store an integer quantity. An ordinary int can store a range of values from INT_MIN to INT_MAX as defined by in header file <limits.h>. The type modifiers for the int data type are: signed, unsigned, short, long  and long long.




       
    • A short int occupies 2 bytes of space and a long int occupies 4 bytes.
    • A short unsigned int occupies 2 bytes of space but it can store only positive values in the range of 0 to 65535.
    • An unsigned int has the same memory requirements as a short unsigned int. However, in case of an ordinary int, the leftmost bit is reserved for the sign.
    • A long unsigned int occupies 4 bytes of memory and stores positive integers in the range of 0 to 4294967295.
    • By default the int data type is signed.
    • A long long int occupies 64 bits of memory. It may be signed or unsigned. The signed long long int stores values from −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 and the unsigned long long ranges from 0 to 18,446,744,073,709,551,615.

    char:

    It stores a single character of data belonging to the C character set. It occupies 1 byte of memory, and stores any value from the C character set. The type modifiers for char are signed and unsigned.
    Both signed and unsigned char occupy 1 byte of memory but the range of values differ. An unsigned char can store values from 0 to 255 and a signed char can store values from -128 to +127. Each char type has an equivalent integer interpretation, so that a char is really a special kind of short integer. By default, char is unsigned.

    float:

    It is used to store real numbers with single precision i.e. a precision of 6 digits after decimal point. It occupies 4 bytes of memory. The type modifier for float is long. It has the same memory requirements as double.






    double:

    It is used to store real numbers with double precision. It occupies 8 bytes of memory. The type modifier for double is long. A long double occupies 10 bytes of memory.

    void:

    It is used to specify an empty set containing no values. Hence, it occupies 0 bytes of memory.



    User defined type declaration:

    C language supports a feature where user can define an identifier that characterizes an existing data type. This user defined data type identifier can later be used to declare variables. In short its purpose is to redefine the name of an existing data type.


    Syntax:


     typedef <type> <identifier>; like
    typedef int number;

    Now we can use number in lieu of int to declare integer variable. For example: “int x1” or “number x1” both statements declaring an integer variable. We have just changed the default keyword “int” to declare integer variable to “number”.


    Data Types in C, Size & Range of Data Types:

     C language supports various data types, this charts shows you how much space each data type like int, char, float occupies space in memory along with its data range and keyword used by programmer in C program.
     C language supports various data types, this charts shows you how much space each data type like int, char, float occupies space in memory along with its data range and keyword used by programmer in C program.

No comments:

Post a Comment