Amazon Honor System Click Here to Pay Learn More

Introduction to C
by Forest J. Handford

This chapter is geared to people who have never programmed in C++ and those who have never programmed at all.

In this tutorial I will make tons of parallels to the C++ tutorial.  This for the most part will be done because the C++ tutorial was our first tutorial and you probably read it first.  If you didn't, you may want to read it before going through this tutorial.

C is not considered an object oriented language.  It is considered a middle level language.  A lower level language is Assembly or Machine language.  Some middle level language are C, PASCAL, and BASIC.  C++ and Java are considered higher level languages.  If your not sure why they are different, it doesn't matter.  I just wanted you to be aware of what C is!

The BIG difference between C and C++ is that C doesn't have classes.  Both languages have structs.  A programmer can access any data in a struct but in C++ a programmer can only access private and protected members of a class through public functions of the class.

Here are some advantages of using C over C++:

  1. Programs written in C are usually faster then those written in C++
  2. More companies use C than C++
  3. C code can be used in C++ programs

In C++ cout and cin are used for text output and input.  cout and cin are parts of a class so they didn't exist when C was developed.  With C we will use functions for text input and output.

The printf function is used to output text to the screen in a console application.  printf is defined in the stdio.h header.  Consider the following code:
printf("Senator Palpatine will become the emperor!");

This code would cause the following output:
Senator Palpatine will become the emperor!

Just like in C we can use \n to form a newline.  Consider the following:
printf("Senator Palpatine \nwill become the emperor!");

The above code will cause the following output:
Senator Palpatine
will become the emperor!

In C and C++ there has to be a main function in a console application for it to be executed.  C compilers tend to be more finicky about the main function then C++ compilers.  Let's review our first program, Game Programmers are Huggable:


< Files >

// Intro.c for the introduction to C class
// East Coast Games
// Forest J. Handford
// Copyright (c) 1998 - 1999
//////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>

int main ()
{
 printf("Game Programmers are Huggable!\n");

 return (EXIT_SUCCESS);
}

Just like C++ anything on a line of code after // is a comment.  As you probably know, and don't want to hear again, a comment is text that is ignored by the compiler and used to document code.  To use a function or data from a header file you must use a preprocessor statement.  Preprocessor statements are statements that have priority over regular statements and are dealt with by compilers first.  To use a header file place a #include statement above the body of your main function.  If the header is a standard header and comes with your compiler you should include it as follows:
#include <stdio.h>

To include a header file that is in the same directory as your source file do the following:
#include "header.h"

The stdlib.h is used to define EXIT_SUCCESS .  EXIT_SUCCESS is a constant and when it is returned it tells the operating system that the program exited successfully.

The return statement is used to return a value of a function, or in the case of the main function the program, to whatever called it.  The int before the main function tells the compiler that the function will return an integer.  The code of a function must be enclosed in { } as shown above.

That's enough for this chapter.  For those of you that hate math, your not going to be too impressed with the programs in the upcoming chapters.  Two of the programs in the upcoming chapters are actually answers to questions on a game programming exam from an Electronic Arts company.  One of them is important for 3D programming because it multiplies matrices.

tions on a game programming exam from an Electronic Arts company.  One of them is important for 3D programming because it multiplies matrices.

an Electronic Arts company.  One of them is important for 3D programming because it multiplies matrices.

HOME       FIRST CHAPTER

Amazon Honor System Click Here to Pay Learn More