Most programming languages have a concept of variables (pure functional langauges like Haskell are a good example of ones without variables). A variable is an area of (usually) memory where a value can be stored, modified, and recalled. Each variable has a type that says what kind of thing is being stored, and how to interpret it when recalling it. In the C langauge, all types fall into one of the following categories: an enumeration, a structure, a union, a pointer, a boolean, a floating point or an integer.
We will look at the first four later.
A boolean is capable of storing one of two states: true and false. Control structures in C that depend on conditionals historically treated 0 as false and everything else as true. In C99, an explicit boolean type was introduced, with the type name _Bool. C programs that use this type will typically include the standard header stdbool.h which creates the aliases bool to _Bool, true to 1 and false to 0. Internally, _Bool stores true as a 1 and false as a 0. Anything else stored to a boolean variable is converted to a 1.
The name float is short for floating point. A floating point number is stored in a sort of binary scientific notation, a significand times two to the power of an exponent. They can be very small values or very large values, and the precision stays the same number of significant figures. The name int is short for integer. These store the whole number in binary without a decimal, having a fixed range.
The following table summarizes all of the numeric types in C.
Name | Standard requirement | Common implementation |
---|---|---|
float | not specified | IEEE 754's binary32 |
double | not specified | IEEE 754's binary64 |
long double | not specified | Intel's 80-bit floating point |
char | character size | 8 bits |
unsigned char | unsigned char | 8 bits, unsigned |
signed char | signed char | 8 bits, 2's compliment |
short | at least 16 bits, signed | 16 bits, 2's compliment |
unsigned short | unsigned short | 16 bits, unsigned |
int | at least 16 bits, signed | 32 bits, 2's compliment |
unsigned | unsigned int | 32 bits, unsigned |
long | at least 32 bits, signed | 64 bits, 2's compliment |
unsigned long | unsigned long | 64 bits, unsigned |
long long | at least 64 bits, signed | 64 bits, 2's compliment |
unsigned long long | unsigned long long | 64 bits, unsigned |