Part 2
------
Hi. Now let us introduce ourselves to variables. Variables are an
essential part of most programs. They enable us to store information.
Each variable must have a type associated with it. Let's look at a
small snippet of code:
int x, y; /* x and y are variables of type int. */
float area, radius; /* floating point variables. */
double hyp; /* floating point variable...more accurate. */
long stockNum; /* A bigger int. */
There are others but for now we'll stick with these.
As you can see the variables follow the name of the type. In the
first example "int" is the type while "x" and "y" are the variables.
An "int" is a whole number. No decimal part. The same goes for a
"long". "float" and "double" types are for storing fractional
numbers.
Let's take a look at a program that makes use of variables:
/*------------------------------------------------------------------
Program: VAREXAMP.C
Author: CORVUS
Date: Aug. 20, 2001
Description: This program gives an example of variables.
------------------------------------------------------------------*/
#include <stdio.h>
int main(void)
{
int x, y;
double p, n;
x = 12; /* Set the variable x to 12 */
y = 32; /* Set the variable y to 32 */
p = 26.8934; /* Set the variable p to 26.8934 */
n = 2.0; /* Set the variable n to 2.0 */
printf("X + Y = %d\n", x + y); /* Add 12 + 32 */
printf("P * N = %f\n", p * n); /* Multiply 26.8934 by 2.0 */
return 0;
}
The output I got was:
X + Y = 44
P * N = 53.786800
The definitions part of the program should be self explanatory.
The "printf" statement might look a little strange though. Let's
go through it:
In the first "printf" statement we have double quotes, meaning we
want to display all the stuff after the quotes UNLESS we hit an
escape sequence or a Conversion Specifier. Sound weird? It does
to me too now that I'm reading it. Oh well. :-)
An escape sequence provides special formatting control.
Here's a list of a few:
\" = Double quote
\? = Question Mark
\' = Single Quote
\\ = Backslash
\a = Bell
\b = Backspace
\n = Newline
\t = Horizontal Tab
The conversion specifier tells "printf" what variable type to expect
and interepret. The "%d" tells printf to interpret the variable (or
equation in our above case) outside the quotation marks as an integer.
The "%f" in the seconds printf statement means that it is expecting
some type of floating point value.
Some of the more common Conversion Specifiers are:
%c Single character This is a char
%d integer (signed) This is for int
%ld Long Integer (signed) This is for long
%f floating point number This is for float or double
%s character string This is for arrays of char
%u Unsigned integer This is for Unsigned int
%lu Unsigned long integer This is for unsigned long
Let's change our above program...just a little bit.
/*-------------------------------------------------------------------
Program: VAREXAMP2.C
Author: CORVUS
Date: Aug. 20, 2001
Description: This is program number 2 that gives an example of
variables.
-------------------------------------------------------------------*/
#include <stdio.h>
int main(void)
{
int x, y;
double p, n;
x = 12; /* Set the variable x to 12 */
y = 32; /* Set the variable y to 32 */
p = 26.8934; /* Set the variable p to 26.8934 */
n = 2.0; /* Set the variable n to 2.0 */
printf("%d + %d = %d\n",x, y, x + y); /* Add 12 + 32 */
printf("%f * %f = %f\n",p, n, p * n); /* Multiply 26.8934 by 2.0 */
return 0;
}
This gives almost the same results as the last program except instead of
printing the letters "X + Y =" we replaced them with the numeric
equivilent. Neat-o eh?
There is one more item I would like to discuss. Macros. The following
was extracted from a Borland help file. I tried finding it in the
standard, but I have decided that this definition is easier to
understand.
#define macro_identifier <token_sequence>
"Macros provide a mechanism for token replacement with or without a set
of formal, function-like parameters."
This can be quite a mouthful. Especially if you don't know what a
"token" is. It might be better understood by looking at an example.
...
#define MAX 30
int main(void)
{
int Name[MAX]; /* Replace MAX with the number 30. */
...
...
As the comment above says, the word MAX is replaced by the value 30.
So, after the compiler runs through it (the pre-processor actually),
it replaces MAX with 30.
You can use MAX wherever you need the number 30. A warning though; The
value can never change. That is, you cannot increment or decrement it.
It is constant.
You can do some truly amazing things with macros. People have written
Most of their program using macros. That said, never use macros to
rename C reserved words. For example:
#define BEGIN {
#define END }
There is no reason for this. If you want to write code in Pascal or
Ada, then USE Pascal or Ada.
Ok...Enough for this Part. Let's go to Part 3!