C Data types.


Variable definition

C has a concept of 'data types' which are used to define a variable before its use.

The definition of a variable will assign storage for the variable and define the type of data that will be held in the location.

So what data types are available?

int float double char void enum

Please note that there is not a boolean data type. C does not have the traditional view about logical comparison, but thats another story.

Recent C++ compilers do have a boolean datatype.


int - data type

int is used to define integer numbers.


    {
        int Count;
        Count = 5;
    }


float - data type

float is used to define floating point numbers.


    {
        float Miles;
        Miles = 5.6;
    }


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;
    }


char - data type

char defines characters.


    {
        char Letter;
        Letter = 'x';
    }


Modifiers

The three data types above have the following modifiers.

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:


                 Type  Bytes  Bits                Range

            short int    2      16          -16,384 -> +16,383          (16kb)
   unsigned short int    2      16                0 -> +32,767          (32Kb)
         unsigned int    4      16                0 -> +4,294,967,295   ( 4Gb)
                  int    4      32   -2,147,483,648 -> +2,147,483,647   ( 2Gb)
             long int    4      32   -2,147,483,648 -> +2,147,483,647   ( 2Gb)
          signed char    1       8             -128 -> +127
        unsigned char    1       8                0 -> +255
                float    4      32
               double    8      64
          long double   12      96

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.

The standard requires a char to be at least 8 bits, a short to be at least 16 bits, a long to be at least 32 bits, and an int must be greater than or equal to a short and less then or equal to a long.

You can find out how much storage is allocated to a data type by using the sizeof operator.


Qualifiers

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 (but who counts bytes these days?).


See also:

Data type conversion

Storage classes.

cast

typedef keyword.


Top Master Index Keywords Functions


Martin Leslie