C Data Types

 Data Types in C

 What is Data Type?

In Computer programming, classification of a particular type of information is called Data Type. It helps humans to differentiate the information.

C Language supports explicit declaration of ‘data types' which are used to define a variable before its use.


Data Types Available in C Language

char
int        
float     
double
void     
enum

There is not a Boolean data type, C does not have the traditional view about logical comparison.

Recent C++ compilers do have a Boolean data type.

char - data type
char defines characters.


    {
        char Letter;
        Letter = 'gcreddy';
    }

int - data type

int is used to define integer numbers.


    {
        int Count;
        Count = 123;
    }

float - data type

float is used to define floating point numbers.


    {
        float Miles;
        Miles = 1.34;
    }

double - data type

double is used to define BIG floating point numbers. It reserves twice the storage for the number. On PCs this is likely to be 8 bytes.


    {
        double Atoms;
        Atoms = 2500000;
    }


Modifiers

The three data types above have the following modifiers.

    short
    long
    signed
    unsigned

The modifiers define the amount of storage allocated to the variable. The amount of storage allocated is not cast in stone. ANSI has the following rules:


        short int <=    int <= long int
            float <= double <= long double

What this means is that a 'short int' should assign less than or the same amount of storage as an 'int' and the 'int' should be less or the same bytes than a 'long int'. What this means in the real world is:

 
Data Type
Range
Bytes
short int   
-32,768 to +32,767
2
unsigned short int   
0 to +65,535
2
unsigned int

0 to +4,294,967,295

4
int
-2,147,483,648 to +2,147,483,647

4
long int

-2,147,483,648 to +2,147,483,647

4
single char
-128 to  +127

1
unsigned char

0 to +255

1
float


4
double    


8
long double
 

12


These figures only apply to todays generation of PCs. Mainframes and midrange machines could use different figures, but would still comply with the rule above.

We can find out how much storage is allocated to a data type by using the size of operator.

Qualifiers

    const
    volatile

The const qualifier is used to tell C that the variable value can not change after initialisation.

const float pi=3.14159;

pi cannot be changed at a later time within the program.

Another way to define constants is with the #define preprocessor which has the advantage that it does not use any storage.

0 comments:

Post a Comment