Variable Types

So far, we have only looked at integer type variables. You may also want to store character data, or real numbers. A list of C data types is given in the table below.

Type of Data

Name of Variable Type

Comment

Real numbers

float

A float variable is appropriate for most real numbers such as 3.5 or 2.667.

 

double

Double allocates twice as much memory for the number as float. It is short for double precision. Variables of type double should be used when you need very precise results or are doing many calculations with a variable that involve numbers of different magnitudes.

Integer numbers

 int

int is the default type for integer data such as 232, or -3. The amount of storage space allocated will depend on the compiler. Generally, int will have the same amount of storage as either short or long .

 

 short

 The short data type is useful for small integers. The amount of space allocated is decided by the compiler. If the compiler allocates 16 bits, a short can store numbers from -32767 to 32768.

 

 long

The long type allocates twice as much storage as short. If the compiler allocates 32 bits for a long, the value can range over approximately +/- 2 billion. 

 Character data

char

The char type stores a single ASCII character. Characters are enclosed in single quotes. For example, 'a' or 'A' or '2'. 

 Boolean data

bool

Boolean data is either true or false. The bool data type is only available in C++.

We will now look at math operators.

next


 home                      table of contents                      about