Amazon Honor System Click Here to Pay Learn More

Chapter 1
by Forest J. Handford

This chapter is going to discuss constants, variables, arithmetic operators and input.

A constant is a number that cannot change its value through the life of the program.  A constant can be created several ways in C.  A number written in the source code is a constant.  A constant can be declared so that no other value would ever be able to go into it by using the attribute const.  Another way to create a constant is to use the preprocessor with #define.

Most C programmers use upper case letters and underscores "_" to name constants.  I will try to stick to that convention throughout this tutorial.  The following are valid ways to create constants:

#define MAX_SIZE 90
const int MAX_SIZE = 90;
The preprocessor is a part of the compiler that searches for code that should be defined before going logically through the program.  The average compiler usually goes through code at least three times.  The first time is to look for preprocessor statements.  You have already seen two uses of the preprocessor.  The first use was the #include <stdio.h> and now #define THE_ANSWER 42.


Valid names for constants, functions, classes, and variables are flexible in C.  The names can begin with a letter or underscore "_".  Names may have digits in them and may end with digits but cannot begin with digits.  Names can be either case or a mixture of upper and lower case letters.  Names cannot conflict with keywords.  Names can be as long as 247 characters!  That means we could use the following as a valid name:


The above name could actually be longer but the editor I use crashes whenever I make it larger than 174 characters!  Please don't ever use names that long because it is a pain to read and to write!

Every year since 1984, except 1997, a contest called The International Obfuscated C Code Contest has been held. The contest is to see who can make the most confusing code. This type of code is exactly what you want to avoid if you want to program professionally.

Now it is time to discuss comments.  Comments are words or code that are ignored by the compiler.  In C there are two ways to create comments.  The first way is to use // before your comment.  Everything on that line after the // will be ignored by the compiler.  Another type of comment, that can span multiple lines, starts with /* and ends with */ .  Here is an example of both comments:

// I use this type of comment more than the other type. Usually when using 
// the other type people have to surround the edges to keep from confusion.

/* If code was commented out for what ever reason it could be hard to
see where the comment starts and ends unless the programmer has a color
coded compiler.  Color-coded compilers like Visual C++ and Borland
C++ usually gives a special color to comments.  Of course that doesn't
help if you are colorblind! */

* To make comments readable in text editors programmers often place asterisks *
* around their comments.  This also makes them look pretty!                   *

Comments are very important for professional programmers.  Student programmers are forced to use comments or their grades will pay for it.

Many pieces of software are indecipherable due to a lack of comments.  This has occasionally created job security!  At MEDITECH the development programmers seem to be allergic to comments.  I write custom code and fix bugs in the software, which is often complicated with uncommented programs.  If you ever program with other people PLEASE use comments.  comments help your partner(s) work with you.  Generally I identify all my variables as well as code that seems complicated to me.

There are several data types in C.  My favorite data type is int, or integer.  int types can not have decimal points and for most compilers can range from –32,768 to 32,767.  Here are more data types:

Type Name Bytes Other Name Range of Values
int system dependent * signed * integer system dependent 
unsigned int system dependent * unsigned integer system dependent
__int8 1 signed character -128 to 127
__int16 2 signed short integer -32,768 to 32,767
__int32 4 signed integer -2,147,483,648 to 2,147,483,647
__int64 8 large integer -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
char 1 signed character -128 to 127
unsigned char 1 unsigned character 0 to 255
short 2 signed short integer -32,768 to 32,767
unsigned short 2 unsigned short integer 0 to 65,535
long 4 signed long integer -2,147,483,648 to 2,147,483,647
unsigned long 4 unsigned long int 0 to 4,294,967,295
enum same as int none same as int
float 4 none 3.4E +/- 38 (7 digits)
double 8 none 1.7E +/- 308 (15 digits)
long double 10 none 1.2E +/- 4932 (19 digits)
* bool 1 bit boolean 0 to 1

* signed means the number can be positive or negative.

* an integer is a number without decimal values.

* unsigned means the number must be positive.

* A boolean value can be either 1 or 0.  bool is the smallest data type, representing one bit in memory.  bool is not implemented in most compilers.  Visual C++ 5 and Borland C++ 4 were the first to use bool.  The reason I like bool is for True/False values.

We need a few operators before we get to the next program.  We should start with the assignment operator = .  The name that will get assigned a new value must be on the left side of the operator.  When I started programming I missed that piece of information.  It took me a week to figure out what I was doing wrong!  Some other useful operators are + for addition, - for subtraction, * for multiplication, / for division and % for modulus.  If you don't know what a modulus is I'll discuss it later.  Parentheses are used to alter the order of operations.

int Variable  //Hey look it's a comment! Variable will hold a value

Variable = 32 + 5 - (9 / (2 + 7)) % 1  // Variable will get the number 37

To avoid using keywords I always start a variable's name with a capital letter.  I will use this convention throughout this tutorial.

scanf("") is the function to use to get input. Consider the following:

int Value; // This will hold a number
scanf("%i",&Value); // Get keyboard input for Value

The % sign is a symbol that says that the next character is special and should be replaced with a value. The letter i stands for integer. The & in front of Value tells the compiler to send the function the memory address of value. This function will only work in this type of format. Here is a table of the other letters available and their corresponding types:

letter(s) type(s)
d decimal integer (int)
i Decimal, hexadecimal, or octal integer (int)
o octal integer (int)
u unsigned integer (unsigned int)
x hexadecimal integer (int)
e, E, f, g, G Floating point value (float)
n * no input (int)

* I'm not really sure what this does so you may want to avoid using it!

To print values to the screen via printf you can use the same % syntax. Consider the following:

int Value = 100;
int Value2 = 200;
printf("The value %i is half of %i.",Value,Value2);

The compiler will use the order in the string to decide which arguments to use. So the output of the above is:

The value 100 is half of 200.

Now here is CAdder:

< Code >

// CAdder.c - This program is an adding machine for the C
// game tutorial of East Coast Games.
// Forest J. Handford
// Copyright (c) 1999 East Coast Games

#include <stdio.h> // for printf and scanf
#include <stdlib.h> // Defines EXIT_SUCCESS

int main()
int Input=0; // This will hold keyboard input
int Total=0; // This will hold the total

// Type a welcome message
printf("Welcome to the C Game Tutorials Adding Machine\n");

//Loop till the user enters -555
printf("Enter a value (-555 to exit): "); // Prompt user
scanf("%i",&Input); // Take input
Total = Total + Input; // Add total
printf("\nTotal = %i\n",Total); // Print total

printf("Good Bye!\n\n");

return (EXIT_SUCCESS);

OK, so I skipped two parts of the above program.  Don't worry it's not as overwhelming as it may seem.  Let's do the easy part first.  The not equal to operator, != .  With this we could say 5 != 4 and get a false value.

The while loop.  The while loop's syntax is:
    code that will be executed till the condition is false

In Program CAdder we looped till the user input = -555.  If the input wasn't = -555 the condition would be true.  The code in the braces is executed from top to bottom and then the condition is reevaluated.  The loop will only be exited if the code reaches the bottom of the loop and the condition is false.  BE CAREFUL BECAUSE YOU COULD CAUSE AN INFINITE LOOP.  An infinite loop is the bane of all programmers.  An infinite loop happens when the condition is always true.  If you ran the following code you would have to use the operating system or the power button to stop the program:

while( 42 != 4 )
   cout<<"Stop me if you can!"<<endl;

If you compare this chapter to chapter 2 of C++ you'll find it very similar. The programs do the same exact thing. This is a good way for us to look at the difference between C and C++. The executable for the C++ version is 44 kb and the C version is 32 kb. This means that the C version will be faster. The C++ version is a tad easier to program, in my opinion. So here I traded easy programming for speed. This type of comparison can be very important with full-sized games that need to be fast, even in the dawn of the gigahertz processor.


Amazon Honor System Click Here to Pay Learn More