If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Programming in C (Part 1)

Ok, so C may be slightly outdated by C++. But sometimes it can be useful to know the basics behind it for older codes etc.

I find C simpler than C++, though this may be different for other people. If you don't really care about C, or think that C++ is all that you really need to know, then don't read on.

First bit - the compiler

First, you need to get a decent C compiler. I recommend the Borland 5.5 Compiler, which is available free from www.borland.com. As a text editor, I usually use MS-DOS EDIT, or notepad.
This tutorial assumes that you have set up the compiler and know how to compile code. If you have got the borland one, follow the instructions in the readme to set it up.

Part 1 - The main() function

All C is based around functions. The one that is always run on startup is the main function. Here is the first outline of the C program.

void main()
{

}

A step-by-step breakdown of the code:

void - this shows that the program will not 'return' a value after finishing. If this were anything different, a value would be sent back to the OS when the program finished

main() - this shows the function. The brackets are there to contain any 'arguments' - anything other data that is sent to the program on starting. More on these later.

{} - the curly brackets. These show where the function starts and ends. Any code between these brackets is run by this function.

2 - The first code

OK, now you know the very basics of C. Only problem is, if you compile and run this program, nothing happens. You need to add something to it. This is done by adding new functions into the main function. These are functions that are already specified by the compiler. The main function is one that you are specifying yourself.
Here is code for a program that actually does something:

#include &lt;stdio.h&gt;

void main()
{
printf("Hello and Goodbye");
}

New code breakdown:

#include &lt;stdio.h&gt; - this tells the compiler to include code from the file stdio.h. This file contains definitions of the standard i/o (input/output) functions of the C language.

printf("Hello and Goodbye"); - this is the printf function - one that prints text for the program. The text in the speech marks, and inside the brackets, is the text that gets printed - the argument of the printf function. This length of text is called a string. The semi-colon at the end tells the compiler that this line of code is finished - an error occurs if it is left out.

If you now compile and run the program, it will print "Hello and Goodbye" on the screen (without the speech marks), and then end. This is the most basic function of C.

3 - An Intoduction to Variables

Variables are the 'storage containers' of C - they hold data for the program. The first type of variable is the integer. The integer stores a single whole number, from -128 to 127. It is stored on the computer's memory as one byte - eight ones or zeros.
Here is some code that uses an integer variable:

#include &lt;stdio.h&gt;

void main()
{
int x;

x=5;
printf("The value of x is %i.",x);
}

New code breakdown:

int x; - this code tells the program that there is a new variable being declared. It will be an int, or integer variable, and its name is "x". Again, there is a semi-colon to show the end of the line of code.

x=5; - this tells the program to set the value of x to be 5. Whenever x is referred to in the code, the value 5 will be used.

printf("The value of x is %i.",x); - the new parts of this code are the "%i" and the ",x" inside the brackets. The "%" warns the compiler that the next letter is not to be printed directly, but that it represents a variable. In this case, the "i" shows that an integer variable will go here. The "," after the string shows that there is a new argument to the function - the integer x. The value of x will be printed instead of "%i".

If you compile and run this program, you should see the text "The value of x is 5." Program success!