In order to be useful a program must be able to represent real life quantities or data e.g. a person’s name, age, height, bank balance, etc. This data will be stored in memory locations called variables that we will name ourselves. However so that the data may be represented as aptly as possible the variables will have to be of different types to suit their data. For example while an integer can represent the age of a person reasonably well it won’t be able to represent the pounds and pence in a bank balance or the name of an individual quite so well.
Basic Data Types
There are five basic data types char, int, float, double, and void. All other data types in C are based on these. Note that the size of an int depends on the standard size of an integer on a particular operating system.
|char||1 byte ( 8 bits ) with range -128 to 127|
16-bit OS : 2 bytes with range -32768 to 32767
32-bit OS : 4 bytes with range -2,147,483,648 to 2,147,483,647
|float||4 bytes with range 10-38 to 1038 with 7 digits of precision|
|double||8 bytes with range 10-308 to 10308 with 15 digits of precision|
|void||generic pointer, used to indicate no function parameters etc.|
Modifying Basic Types
Except for type void the meaning of the above basic types may be altered when combined with the following keywords.
- 1. signed
- 2. unsigned
- 3. long
- 4. short
The signed and unsigned modifiers may be applied to types char and int and will simply change the range of possible values. For example an unsigned char has a range of 0 to 255, all positive, as opposed to a signed char which has a range of -128 to 127. An unsigned integer on a 16-bit system has a range of 0 to 65535 as opposed to a signed int which has a range of -32768 to 32767.
Note however that the default for type int or char is signed so that the type signed char is always equivalent to type char and the type signed int is always equivalent to int.
The long modifier may be applied to type int and double only. A long int will require 4 bytes of storage no matter what operating system is in use and has a range of -2,147,483,648 to 2,147,483,647. A long double will require 10 bytes of storage and will be able to maintain up to 19 digits of precision.
The short modifier may be applied only to type int and will give a 2 byte integer independent of the operating system in use.
NB : Note that the keyword int may be omitted without error so that the type unsigned is the same as type unsigned int, the type long is equivalent to the type long int, and the type short is equivalent to the type short int.