Numeric Variable Types
C provides several different types of numeric variables because different numeric values have varying memory storage requirements. These numeric types differ in the ease with which certain mathematical operations can be performed on them.
Small integers require less memory to store, and your computer can perform mathematical operations with such numbers very quickly. Large integers and floating-point values require more storage space and more time for mathematical operations. By using the appropriate variable types, you ensure that your program runs as efficiently as possible.
C's numeric variables fall into the following two main categories:
- Integer variables
- Floating-point variables
Within each of these categories are two or more specific variable types. Table given next, shows the amount of memory, in bytes, required to hold a single variable of each type.
The type char may be equivalent to either signed char or unsigned char, but it is always a separate type from either of these.
In C there is no difference between storing characters or their corresponding numerical values in a variable, so there is also no need for a function to convert between a character and its numerical value or vice versa. For the other integer types, if you omit signed or unsigned the default will be signed, so e.g. int and signed int are equivalent.
The type int must be greater than or equal to the type short, and smaller than or equal to the type long. If you simply need to store some values which are not enormously large it's often a good idea to use the type int; it usually is the size the processor can deal with the easiest, and therefore the fastest.
With several compilers double and long double are equivalent. That combined with the fact that most standard mathematical functions work with type double, is a good reason to always use the type double if you have to work with fractional numbers.
The following table is to better describe the variable types:
Variable Type |
Keyword |
Bytes Required |
Range |
Format |
Character |
char |
1 |
-128 to 127 |
%c |
Integer |
int |
2 |
-32768 to 32767 |
%d |
Short integer |
short |
2 |
-32768 to 32767 |
%d |
Long integer |
long |
4 |
-2,147,483,648 to 2,147,438,647 |
%ld |
Unsigned character |
unsigned char |
1 |
0 to 255 |
%c |
Unsigned integer |
unsigned int |
2 |
0 to 65535 |
%u |
Unsigned short integer |
unsigned short |
2 |
0 to 65535 |
%u |
Unsigned long integer |
unsigned long |
4 |
0 to 4,294,967,295 |
%lu |
Single floating-point |
float |
4 |
-3.4E38 to 3.4E38 |
%f |
Double floating-point |
double |
8 |
-1.7E308 to 1.7E308 |
%lf |
Long double floating-point |
long double |
10 |
-1.7E4932 to 1.7E4932 |
%Lf |
|