Contents General Beginner's Guide to C Windows -coming soon- Dos -coming soon- Misc. |
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()
void func_1()
After you have compiled it and ran it, the programs output should have looked like this:
Now that you have that typed in, lets look at the individual parts a little closer:
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()
void func_1()
void func_2()
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 \ns 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()
//keyboard
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:
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.
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.
|
|||||||||||||||||||||||
|