Chapter 2
Basic Types

C lets us use types to classify variables. The most basic type is void, which is no type at all. Then, there is the boolean type bool, that allows us to talk about values that can be either true or false. And then, there are the various integer and floating-point types, essential in any programming language.

5 void

The most elementary type in C is the void type. It is used when functions return no value, as in

void my_function(int my_arg);

Or when functions receive no arguments, as in

int my_function(void);

It is also used when we wish to have a pointer that points at anything, at any kind of data, as in

void *my_pointer;

6 bool

A boolean variable is a variable that can be either true or false. It is declared with the type bool, like so

bool has_bananas;

After it is declared, we may use the boolean values

false
true

in assignments, comparisons, and so forth. For example,

has_bananas = true;

7 Integer Numbers

Integers are probably the most useful types of any programming language. That's probably why C has a lot of them. They vary in possession or not of a sign (only positive values or both positive and negative values), and in the size they occupy in memory. Here they all are:

Integer TypeTypical Size (Bytes)
char1

signed char1
signed short2
signed int2 or 4
signed long4
signed long long4 or 8

unsigned char1
unsigned short2
unsigned int2 or 4
unsigned long4
unsigned long long4 or 8

The signed keyword may be omitted, except with chars. The sizes of ints depend on the compiler implementation. After declaring a variable of an integer type, say

unsigned int bananas;

we may initialize it with a specific constant. These come in several flavours. They may be decimal, octal, or hexadecimal. A decimal constant begins with a digit different from 0, and is followed by any of the usual decimal digits. An octal constant begins with a 0, and is followed by a sequence of digits, any of which may be between 0 and 7. A hexadecimal constant begins with the sequence 0x or 0X, and is followed by a sequence of digits between 0 and 9, or by letters A through F, or a through f. Constants may have the suffixes u or U if we wish to say they're unsigned. They may have the suffixes l or L if they are long, or the suffixes ll or LL if they are long long. Here are some examples of integer constants.

10      // int (decimal)
010     // int (octal)
0x1A    // int (hexadecimal)
-0X1f   // int (hexadecimal)
20U     // unsigned int
-020L   // long
30UL    // unsigned long 
020ll   // long long
0x20uLL // unsigned long long

A character constant is just a symbol enclosed in single quotes. For example,

'a'
'\'' // the character '
'\n'

8 Floating-Point Numbers

A floating-point number is just a computer approximation of a real number. There are 3 different floating-point types in C, as follows.

Floating-Point TypeTypical Size (Bytes)
float4
double8
long double10

The amount of memory these occupy depends on the compiler implementation. A floating-point constant is a number with a period separating the integral from the decimal part. They may also be specified using scientific notation. For example,

1.0
-10.
.25
3e-3
3.5E+2
-2.75f
15.2625L
.6P5
15.25p-6L

The number 3e-3 is 3×10-3, and .6P5 is 0.6×25. The suffix f or F means the constant is a float, and the suffix l or L means the constant is a long double.