Like most high-level languages, C has its own set of data types. Only simple data types are defined by C: all other data types can be derived from simple ones. They are defined in (guess what) libraries. Here is a table of simple data types and their widths in bits.
| Type | Description | Bits |
|---|---|---|
char |
A single byte | 8 |
int |
A signed integer | 16/32 |
float |
Floating point number | 32 |
double |
Double precision float | 64 |
Strange as this may sound, this is all. To make things easier, there are modifiers which change the width and format of the four data types. The modifiers are short, long, signed, and unsigned. The first two change the width and range of the data types; the latter two change between signed and unsigned formats. Here is a table of all the meaningful combinations of modifiers and data types:
| Modified Type | Bits | Range |
|---|---|---|
char
|
8 | -128 ... 127 |
unsigned char |
8 | 0 ... 255 |
intsigned int |
16/32 | -32768 ... 32767 or -2147483649 ... 2147483648 |
unsigned int |
16/32 | 0 ... 65535 or 0 ... 4294967295 |
short intsigned short int |
8/16 | -128 ... 127 or -32768 ... 32767 |
unsigned short int |
8/16 | 0 ... 255 or 0 ... 65535 |
long intsigned long int |
32 | -2147483649 ... 2147483648 |
unsigned long int |
32 | 0 ... 4294967295 |
float |
32 | -3.4E-38 ... 3.4E+38 |
double |
64 | -1.7E-308 ... 1.7E+308 |
long double |
64 | -3.4E-4932 ... 1.1E+4932 |
As you can see, this gives a rather impressive collection. Note that there are defaults to each modifier. If you do not specify the data type, int is assumed (so it is more common to write long than long int). The default format is signed and the default size is normal (no modifier).
An important concept in C is that, although there are very concretely defined data types, you are not forced to obey them: you can store a value of 255 in a signed char. The compiler might warn you of possible problems, but will not generate an error. The interpretation of the bit patterns, however, depends on the variable's data type. So, when reading the signed char from the previous example, we will not get 255, but -1 (binary 11111111 = unsigned 255, but -1 in signed or twos' complement).
Another point (important to BASIC users) is that C does not allocate variables dynamically: you must declare them before use. The declaration is a line like one of the following:
char x, y; /* x and y are chars */ int i; /* i is an int */ float X; /* note that x is NOT X */ long a=15; /* variable initialisation */
Variable declarations are placed between an open curly bracket (‘{’) and the following statement, or outside function definitions, at the top level of the program. In the first case, they are local variables: accessible only within the block they were defined in (i.e. only accessible to the statements within the curly brackets). Variables defined at the top level are global: they are available to all your program.
Literals are actual (literal) constant numbers used like that in C code. The 15 in the previous code snippet, and the "Hello world!" in the canonical ‘Hello World’ program are both literals. Like most languages, C allows us to specify literal values in different ways. Here they are:
| Constant | Description |
|---|---|
| 13 | 13 decimal |
| 13. | 13.0 in floating point |
| 13.45 | 13.45 in floating point |
| 0xd | 13 in hexadecimal |
| 015 | 13 in octal |
| 'A' | ASCII ‘A’ or 65 |
| '\101' | ASCII ‘A’ in octal |
| '\0' | ASCII 0 or NUL
|
| '\n' | ASCII 13 or CR
|
You can assign any of these to any of the data types described above! If you assign the character literal '@' to an int, the value you read back will be 64. If you assign 33 to a char, you'll read back !. If you assign 42 to a float, it's automatically converted to 42.01. Try not to assign 42.0 to an int. Bad things may happen2.
- 1. Depending on the system's architecture and how
intandfloatare represented by the computer's CPU, they may or may not be magic at play here. There's no magic at play on the Oric, and to avoid problems, you should always add the decimal point when you assign literals tofloat. For example, sayfloat x = 10.;, notfloat x = 10;. I once spent a frustrating week trying to locate a bug caused by something like this. Decimal points are difficult to spot on print. - 2. See above.

Add new comment