Graphics Programs Reference
In-Depth Information
called constants . Returning to the driving example, the speed of the car would
be a variable, while the color of the car would be a constant. In pseudo-
code, variables are simple abstract concepts, but in C (and in many other
languages), variables must be declared and given a type before they can be
used. This is because a C program will eventually be compiled into an exe-
cutable program. Like a cooking recipe that lists all the required ingredients
before giving the instructions, variable declarations allow you to make prep-
arations before getting into the meat of the program. Ultimately, all variables
are stored in memory somewhere, and their declarations allow the compiler
to organize this memory more efficiently. In the end though, despite all of
the variable type declarations, everything is all just memory.
In C, each variable is given a type that describes the information that is
meant to be stored in that variable. Some of the most common types are int
(integer values), float (decimal floating-point values), and char (single char-
acter values). Variables are declared simply by using these keywords before
listing the variables, as you can see below.
int a, b;
float k;
c har z;
The variables a and b are now defined as integers, k can accept floating-
point values (such as 3.14), and z is expected to hold a character value, like A
or w . Variables can be assigned values when they are declared or anytime
afterward, using the = operator.
int a = 13, b;
float k;
char z = 'A';
k = 3.14;
z = 'w';
b = a + 5;
After the following instructions are executed, the variable a will contain
the value of 13, k will contain the number 3.14, z will contain the character w ,
and b will contain the value 18, since 13 plus 5 equals 18. Variables are simply
a way to remember values; however, with C, you must first declare each
variable's type.
0x242 Arithmetic Operators
The statement b = a + 7 is an example of a very simple arithmetic operator.
In C, the following symbols are used for various arithmetic operations.
The first four operations should look familiar. Modulo reduction may
seem like a new concept, but it's really just taking the remainder after divi-
sion. If a is 13, then 13 divided by 5 equals 2, with a remainder of 3, which
means that a % 5 = 3 . Also, since the variables a and b are integers, the
Search WWH ::




Custom Search