C Data types.

Variable declaration

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

The declaration 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 boolian data type. C does not have the traditional view about logical comparison, but thats another story.


int - data type

int is used to define integer numbers.
	{
	  int Count;
	  Count =index.html 5;
	}

float - data type

float is used to define floating point numbers.
        {
          float Miles;
          Miles =index.html 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 =index.html 2500000;
        }

char - data type

char defines characters.
        {
          char Letter;
          Letter =index.html '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 <=index.html LONG 
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 bytes than a 'long int'. What this means in the real world is:
	    short int - 2 bytes (16 bits)
	      int int - 2 bytes (16 bits)
	     long int - 4 bytes (32 bits)
	  signed char - 1 byte  (Range -128 ... +127)
	unsigned char - 1 byte  (Range    0 ... 255)
	        float - 4 bytes
	       double - 8 bytes
	  long double - 8 bytes
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.

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 06-Nov-95