Post Reply 
Zinga's quick C tutorial
Author Message
ZiNgA BuRgA
Smart Alternative

Posts: 17,023.4213
Threads: 1,174
Joined: 19th Jan 2007
Reputation: -1.71391
E-Pigs: 446.0333
Offline
Post: #1
Zinga's quick C tutorial
Intro
Following on from this thread (about a game project), I've had a look at a number of game making applications.  Unfortunately, none of them I find really suitable, and from the looks of it, wee'll probably have to go to at least some scripting language to get an ideal solution.


Note: C++ is an extension of the C language - C++ is practically fully backward compatible with C, meaning you can compile a C program with a C++ compiler - I'll refer to C/C++ as "C" here

Why C?  It's by far the most popular language used, and as such, the majority of languages out there, especially scripting, follow C's syntax.

Wee may not decide to use C for the game application, however, this tutorial should be able to provide enough concepts to make it useful for any scripting language.


Notes
This is meant to be a really quick introduction to programming.  This isn't aimed to be a comprehensive guide, or even mostly accurate, mainly aimed to get people who haven't done programming before, an idea of how things work.  And since this is a discussion board, you can always re-read stuff you don't understand clearly, or ask questions.
Don't worry if there are parts you don't completely understand - to make it quick, I'll be leaving a few things unexplained, or explained later on - you should try to read it through, whether or not you're having trouble understanding a certain part.

The purpose of this guide is basically to give some incentive to those who want to learn programming, but can't be bothered to read a thick book on it.  Thus I'll try to only go over the important concepts (and skip over more useless stuff).
If you're serious about learning C, I'd suggest a more proper book or tutorial or class.

Setting up
To make things simple, I'm going to assume you have Windows.  So, wee'll just use a copy of DevC++ (download and install that application).

Now start DevC++ after installing it - if there's any prompts etc, just close them.
If there isn't a blank file open after you start it, press Ctrl+N to bring up a blank editor.

Note: a "compiler" is a program which converts your code into binary executable data - in our case, the compiler will convert our C code into a Windows EXE


Your First Program
Okay, wee'll start with the boring command-line app.
Copy and paste the following code into the textbox:

C Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
#include <stdlib.h>

int main()
{
    // declare a variable of type INTeger, named "i"
    int i;
    // set the value of the variable "i" to 5
    i = 5;
    if(i > 3) // "i" is 5, so it is indeed larger than 3
    {
         printf("Goodbye, world...\n");
    }
    system("pause");
    return 0;
}

Now go Execute menu » Compile and Run (or press F9).
You'll be asked to save the file you just created somewhere.  I recommend placing it into a blank folder (since other files will be generated there).
If everything has been successful until this point, you'll get the computer doing something, then a black popup console window, with the text "Goodbye, world..." followed by a newline and "Press any key to continue...".  Pressing a key will close the window.

Well, that's your first program.  Looking over it, you should be able to guess what some things do.  I've actually introduced many concepts in the above code, so I'll be going over them line by line, for now:
(note, tabs, spaces, etc (aka "whitespace") are generally not important in programming.  Typically, some places require at least one space, but having more, the computer will generally ignore the rest)

C Code
1
2
3
4
5
6
7
8
9
#include <stdio.h>
#include <stdlib.h>

int main()
{

...

}

Wee'll ignore the meaning of these lines for now.  Wee'll assume that all code should go between the two braces ({ and }), replacing the "..." section above.


C Code
// declare a variable of type INTeger, named "i"

This is an example of a "comment".  Comments are bits of text which the computer will completely ignore.  In C, comments can start with "//".  Anything after the "//" will be ignored by the computer, until the next line.  There is also another form of comment, which can span multiple lines (to save you from typing "//" all the time) - these look like this:

C Code
/* this is a "stream" comment */



C Code
int i;

Okay, this is a variable declaration.  Recall algebra?  You typically used "x" as a variable, representing a number.  In this case, wee're not using the name "x", wee use "i" (in fact, many people seem to like "i", so why not?).
One thing to note is that variables are a little different in programming than they are in maths.  Variables in computing are... variable, in other words, they generally change in value, as opposed to always being the same value, as it would be in maths.
Wee'll talk more about this later...
For now, wee'll assume "int" just means to declare a variable.

C Code
i = 5;

Now this is where wee change the value of a variable.
In mathematical terms, the above means "let the value of i be equal to 5".  The above is really a simple statement.  Wee could always feed a more complex value in, for example

C Code
i = 5*4; // assigns the value 20 to i, means let the value of i be equal to 5 times 4



C Code
if(i > 3)

It pretty much says what it means.  If the value of i is greater than 3, then process the statements in the braces following the "if statement".  In our example, wee only have one statement.

C Code
printf("Goodbye, world...\n");

printf basically means output text to the screen.  Note that the text must be enclosed in quotation marks.  "\n" is a special sequence, which denotes a new line.

C Code
    system("pause");
    return 0;

Wee'll just place these two lines at the bottom of every program and ignore it's meaning.





Now, you should be able to understand what our program does (note, not exactly how it works, just what the computer is doing; if you don't, ask), a good exercise would be to try doing the following modifications to it - after each modification, try to Compile & Run it to make sure it actually does what you intended it to do:
- Change the text that is outputted to the screen
- Change the name of the variable from "i" to "x"
- Make it so that the "if" statement's condition evaluates to "false", rather than "true"


Okay, whilst you can modify the above program, it's unlikely that you can write such a thing up yourself, if requested.
I'm going to talk a bit more about syntax and technical aspects.

One thing to note: most lines which tell the computer to "do something" (an action) end with a ";" character.




Okay, that'll be all for now - this is mostly to see how many people are really interested in trying this out, and seeing whether you can get everything set up correctly.
Thanks for reading.


Index
Part 2
(This post was last modified: 01/10/2008 06:26 PM by ZiNgA BuRgA.)
30/09/2008 05:57 AM
Visit this user's website Find all posts by this user Quote this message in a reply
Slushba132
BustyLoli-Chan

Posts: 3,125.3993
Threads: 508
Joined: 20th Feb 2008
Reputation: -8.27558
E-Pigs: 73.1299
Offline
Post: #2
RE: Zinga's quick C tutorial
sigh, I remember how tedious this gets...
and how difficult it can become

30/09/2008 06:06 AM
Visit this user's website Find all posts by this user Quote this message in a reply
-PHENOMENON-
Previously Jaze

Posts: 335.2800
Threads: 29
Joined: 9th Aug 2008
Reputation: 3.706
E-Pigs: 174.8217
Offline
Post: #3
RE: Zinga's quick C tutorial
Sure it seems easy now, wait till you advance further..hehehehe.

Generic sig goes here.
30/09/2008 06:37 AM
Find all posts by this user Quote this message in a reply
Mc Cabe
Storm Trooper

Posts: 1,218.1771
Threads: 177
Joined: 14th Aug 2007
Reputation: 1.43435
E-Pigs: 38.5281
Offline
Post: #4
RE: Zinga's quick C tutorial
woot i understood that :P

umm?
30/09/2008 07:25 AM
Visit this user's website Find all posts by this user Quote this message in a reply
LaneLander
Storm Trooper

Posts: 236.1425
Threads: 26
Joined: 3rd Jan 2008
Reputation: 3.41035
E-Pigs: 1.4619
Offline
Post: #5
RE: Zinga's quick C tutorial
Well there are plenty of C game engines that are free and open source.  Have wee made any decisions on the next step?

[Image: 361059790.png]
30/09/2008 07:53 AM
Find all posts by this user Quote this message in a reply
roberth
Resident Full Stop Abuser.....

Posts: 4,580.2098
Threads: 200
Joined: 18th Jun 2007
Reputation: -5.5814
E-Pigs: 43.8419
Offline
Post: #6
RE: Zinga's quick C tutorial
well, i have loads of C tutorial books, and actually started to learn...i might start again actually (also, that post was better than the introduction to basically any of the books ive read thus far XD)

30/09/2008 08:41 AM
Find all posts by this user Quote this message in a reply
LaneLander
Storm Trooper

Posts: 236.1425
Threads: 26
Joined: 3rd Jan 2008
Reputation: 3.41035
E-Pigs: 1.4619
Offline
Post: #7
RE: Zinga's quick C tutorial
LaneLander tries to spell see plus please and fails

[Image: 361059790.png]
30/09/2008 08:49 AM
Find all posts by this user Quote this message in a reply
boogschd
boogyman
Worlds End

Posts: 4,954.3196
Threads: 90
Joined: 29th Nov 2007
Reputation: 4.19708
E-Pigs: 43.6852
Offline
Post: #8
RE: Zinga's quick C tutorial
ive got a borland c++ 5.02 compiler if anybody wants

tis leik 90mb Rar'd
(This post was last modified: 30/09/2008 08:57 AM by boogschd.)
30/09/2008 08:56 AM
Visit this user's website Find all posts by this user Quote this message in a reply
Assassinator
...

Posts: 6,646.6190
Threads: 176
Joined: 24th Apr 2007
Reputation: 8.53695
E-Pigs: 140.8363
Offline
Post: #9
RE: Zinga's quick C tutorial
ZiNgA BuRgA Wrote:

C Code
    system("pause");
    return 0;

Wee'll just place these two lines at the bottom of every program and ignore it's meaning.


^
|
Hahaha
30/09/2008 09:25 AM
Find all posts by this user Quote this message in a reply
PSPkiller
*The Sweaty Indian/Welsh Guy*

Posts: 2,393.2167
Threads: 251
Joined: 24th Jun 2007
Reputation: 0.56947
E-Pigs: 53.7911
Offline
Post: #10
RE: Zinga's quick C tutorial
Mc Cabe Wrote:woot i understood that :P

I've  had a go at  the C++ video tut's on 3D Buzz. they sort of  made sense and were slightly funny and entertaining, but  by the 3rd video i was completely lost...

[Insert Signature Here]
30/09/2008 12:57 PM
Find all posts by this user Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: 1 Guest(s)

 Quick Theme: