Programming

Graphics

Music

Links

Download

Contents

General

Beginner's Guide to C

Windows

-coming soon-

Dos

-coming soon-

Misc.

Download

Email

Back to first page

Beginner's Guide to C

Section 1 - Why C?

If you are wanting to start programming, whether it is games or not, I would thouroughly reccomend C as the language you choose. I personally found C quite easy to learn as there are litterally hundred's of books to help you. Once you get into C you will find that you can do so much with it. Many famous software houses (like Id software, the makers of Quake, Quake 2, Doom etc) use C with a little bit of assembler to speed things up when they have to.

Section 2 - What do you need?

To start off programming in C you will need a compiler. A compiler turns your source code into object (assembley) files and then a linker turns the object files into binary .EXE's ( a linker will come along with the compiler). If you do not want to spend a lot of money on a compiler like Borland, Microsoft or Watcom then there are free compilers on the internet. Many people choose to use DJGPP which you can get for free here. I use Borland for Windows programming but DJGPP for games.

Section 3 - You're first program.

Start up your text editor (or Integrated Development Enviroment) and type the following lines of code:-

/* First C Program */

/* Beginners Guide to C */

/* Alastair MacGregor */

int number = 7; // declaring a integer called ‘number’

// and assigning the number 7 to it

char character = 'd'; // declaring a character called

//'character and assigning the letter d to

// it

void main()

{

printf(“Now in main()”);

void func_1();

printf(“\nBack in main()”);

printf(“\nThe variables are: %d %c”, number, character);

}

void func_1()

{

printf(“\nIn func_1()”)

}

 

After you have compiled it and ran it, the program’s output should have looked like this:

 

Now in main()

In func_1()

The variables are: 7 d

 

Now that you have that typed in, let’s look at the individual parts a little closer:

 

void main() This is a function. All C programs must have this. The instructions in this function are carried out first. The 'void' part tells the compiler that the function is of type void and therefore doesn't return a value.
printf(“Now in main”); Outputs “Now in main” on the screen. printf() is another example of a function. It is a built in function in the C standard libraries.
{

}

Opening and closing curly brackets. They show the compiler a block of code.

 

/* */ or // if you have a C/C++ compiler Comments. The compiler will ignore what is in between these. They are for your benefit only. Comments like this /* */ can be spread over a number of lines.

Also notice the ; after each statement, it is essential and you will see it in every coding example in this guide.

 

Section 4 Functions

C programs are made up of functions. In the program that you typed in moments ago you can see main(). That shows that whatever is between the opening and closing curly brackets should be carried out first. Main() is a function. You can have as many functions in a program as you like. This program illustrates that:

/* Program to illustrate functions */

/* Beginners Guide to C */

/* Alastair MacGregor */

void main()

{

printf(“This is in the function main\n”);

void func_1();

printf(“Now we are back in main\n”);

}

void func_1()

{

printf(“Now in func_1\n”);

void func_2();

printf(“Back in func_1\n”);

}

void func_2()

{

printf(“Now in func_2\n”);

}

When compiled and ran this program outputs:

This is function main

Now in func_1

Now in func_2

Back in func_1

Now we are back in main

To call a function you would put its name , e.g. func_1() and then a ; to show that it is a statement e.g. func_1(); would do what ever was in the function called func_1 and then go back to the line after you called it.

The \n inside the printf() creates a new line. You can have as many \n’s as you want:

prinf(“Line 1\n\n\nLine 4\n\n\nLine 7”);

Section 5 - Data Types in C

A data type describes the data that you want to store. The easiest way to explain this is to show an example:

void main()

{

char name[10]; //sets up an array

prinf(“Please enter your name: “);

scanf(“%s”, &name); //gets your name from the

//keyboard

printf(“Hello, %s”, name);

}

This program asks you to enter your name, and then displays a message with your name in it. The array that is initialised by the char name[10]; is an example of a data type. Here is a list of the most commonly used:

Type

Usage

Symbol

int integer, used for numbers between -32000 and 32000 %d
char character %c
char[ ] character array, string %s
float numbers with a decimal point %f

In C you must declare all variables before you use them. If you declare them inside a function then they are called 'Local Variables' and they are only availible to that function. If you declare them outside a function then they are called 'Global Variables' and are made available to every function in the program.

N.B. Remember variables are stored in RAM and are lost when the program ends.

The symbols are for if you use the types with scanf or printf e.g.

scanf("%d", &number);

would get a number when the user typed it in on the keyboard and store it 'at the address of' an integer called number. The & means 'at the address of' which has to do with memory locations but if you are just starting out all you need to remember is to put it before the variable you are wanting to get from the keybourd when you are using scanf().

Section 6 - The End

Hopefully this guide has given you a (very) rough idea of the computer language C. If you would like to contribute to this tutorial or add one of your own then email me.