Overview
The following is a description of some of the basic data types
native to C as well as a quick introduction to some of the more
complex data types that will be covered in later sections. I won’t
cover all basic data types and I won’t cover all of their
qualifiers either, if you’re interested in a complete introduction
see a text. All examples assume a 32 bit Windows OS ( NT4, 98,
2000).
Numbers
The three most common number types are:
Keyword
|
Type
|
Example
|
int
|
integer
|
1 or 5 or –4
|
float
|
floating point
|
3.14159 or –123.567
|
double
|
double precision
float
|
3.1415926535897
|
A keyword commonly used
with the int type is: unsigned. This means that all numbers
assigned to the variable will be positive thus increasing the range
of positive numbers that the variable can hold ( i.e. the range is
all on the positive side of 0 rather than being split down the
middle by 0 ). See Ranges
below.
int
An integer is a whole number, that is no decimal. By default an int
is assumed to be “signed” and can thus be a positive or negative
number. If defined as “unsigned” then the numbers can only be
positive.
float
A float is a number that can contain decimal information. A float
has a precision of 7 digits, and the decimal counts as one of those
digits. Floats are used when you need more precision than an int
allows.
double
A double is a float that has a greater precision, 15 digits. The
greater precision is at the expense of memory as a double is double
the size of a float. Usually you can get away with floats, be
careful with doubles… memory adds up quickly.
There are other
qualifiers such as small, short, long and hyper; see a text for
more info.
Characters
a char is text
If you are unfamiliar with C entirely then you will no doubt find
the way it handles “strings” rather cumbersome. I don’t really
blame you if you do, while I can see how it makes sense and is
quite powerful it is at the same time confusing to beginners. A
character is a single letter or symbol, or simply: text. A string
is a “string” of characters, or: words and sentences. There is no
string type in C, rather we use an array of characters to make up
word and sentences ( or text files ). We’ll see how this is done
later in Strings.
letters and symbols are represented by ASCII
codes
Ok, read this next part carefully because it will be important
later on. A char is really an integer. Bear with me on this. C uses
a code to represent each letter and symbol called an ASCII code.
The codes for the uppercase letters range from 65 to 90, and
lowercase ranges from 98 to 115. So if you wrote the following line
of code:
charmychar = 65;
it would be equivalent
to the following line:
charmychar = ‘A’;
Note that we use single
quotes around characters ( later we will see that we use double
quotes around strings, so keep them straight ). The fist line
doesn’t have quotes around the number 65 so we are assigning the
value of 65 directly to the char, that is we are setting it’s ASCII
code to 65 (which corresponds to the letter A). In the second line
we use ‘A’ instead which gets interpreted as the number 65.
chars
as colors?
Under ordinary circumstances you wouldn’t need to concern yourself
with this, however graphics programming has a rather peculiar use
for chars that you might not expect. When representing the value of
a color component (i.e. red, green or blue) the data type of choice
is an unsigned char. The reason for this is that a char is 1 byte
which gives it a range of 0 to 255 when it is unsigned. The
standard color schemes used by most 3D apps use 256 levels for each
color component giving 256*256*256 (or 16,777,216) color
combinations. A char is used for memory considerations, using an
int which is 4 times as large as a char would greatly increase the
size of images and RAM needed for 3D work. Just keep that in mind
later on, a char is basically a 1 byte int that can be interpreted
as a character.
Arrays
an
array is a collection of items of similar type
Arrays are not really a data type but rather an extension to all
data types ( more or less ). In other words you can have an array
of integers or an array of characters. An array is just a
collection of items of similar type. To get us prepared for strings
later on we’ll look at an example of an array of characters.
A variable must be
declared as an array at the time you declare the variable. It is
done by appending a [] to the variable name. Inside the square
brackets you place a number to indicate the size of the array. The
size indicates the number of items are in the array ( not the
number of bytes ). So to create an array of 10 characters we write
the following:
charmystring[10];
elements in an array are accessed by index
You access each element in the array with an “index”. The index
starts at 0 and then goes up to the total number of elements minus
1. This is one of the things that gives beginners a lot of trouble.
The number of elements in an array is often called ‘n’ in most
programming notation (actually n is commonly used to represent “the
number of” something where that number can vary). So an array’s
indices for an array of size n range from 0 to n-1. In the example
above the last element in the array is mystring[9].
To set the value of an
element you simply specify the index number you want to
change:
mystring[5] = ‘j’;
initializing an array at declaration
Most texts on C will take you through the grueling task of creating
strings by assigning each letter individually. While that may be an
important lesson we’ll skip ahead a bit. An array’s size need not
be set explicitly, rather you can initialize the variable at
declaration which will implicitly set the size of the array. So the
following is perfectly legal:
charmystring[] = “Hello World”;
Here an array of chars
will be created with a size of 12 chars. Yes 12, I’ll explain why
the extra one when we get to Strings. You can only assign a value
to an array like this at declaration, after that you must access
each index individually ( again, not entirely true but close enough
).
So an array of ints is
just as easy:
int
my_int_array[10]; //
creates an array of 10 ints
int my_int_array[] =
{ 0, 1, 2, 3, 8, 5, 6, 7, 8, 9};
// also creates an array of 10 ints
// but also initializes their values
Using
the second definition from above my_int_array[4] would equal
8.
Since
you are no doubt familiar with arrays in whatever scripting
language you are used to I’ll leave the discussion at that. That
should be enough to get you started with them.
Pointers
We will discuss pointers in depth in the next section. For know
just know that they allow you to use a variable to “reference” some
other data. In other words if I have an array of 1000 ints, I can
create a variable that is a pointer to an array of ints that will
reference that large array. Why you would want to do that will be
discussed later, be patient.
Ranges
This table shows the size of each data type and the range of
possible values and precision of each type. Don’t worry too much
about memorizing it, just remember where it is when you need to
come back to it.
Type
|
Size in bytes
|
Range
|
Precision
|
int
|
4
|
–2,147,483,648 to
2,147,483,647
|
na
|
unsigned int
|
4
|
0 to 4,294,967,295
|
na
|
char
|
1
|
-128 to 127
|
na
|
unsigned char
|
1
|
0 to 255
|
na
|
float
|
4
|
3.4e +/- 38
|
7 digits
|
double
|
8
|
1.7e +/- 308
|
15 digits
|
|