After years of studying how OpenTibia works and how the community look like, I finally decided to write tutorials on how to code and understand the structure of OpenTibia
using C NOT C++. Since there's not many people over here who know how to edit source code or fix a simple bug themselves (This tutorial does not explain that ofcourse, but
it should give you a start on how to). I used C because it's a good start to programming.
Some QA anyone would like to know before learning (Psuedo-code-like):
Q: How does a C program look like?
A: a simple C program would like this:
Q: How do I compile a C program?
A: By downloading any C/C++ IDE (i.e DEV-C++, Code::Blocks, etc.) or if you prefer using command line, download MinGW. (protip: stick to IDEs for now if youre not using linux)
Please read Evan's post, he wrote a nice tutorial on how to get started with Microsoft Visual C++ which is the best IDE for C++ : Learning C/C++ - Tutorial
Now, let's begin:
A simple console program to start with
Explanation of each line of the previous program:
This line tells the compiler that we'll be using the functions from the file "stdio.h" which is input/output library in C.
A C program contains functions and variables. This is the main function of our program (basically the overall logic of the program), if this function is not found, the program will fail to compile, ofcourse there are command line parameter's passed but I don't want to make it complicated yet.
This is a paranthesis, it's important to have opening paranthesis at every start of a function.
This function is from the included file "stdio.h" prints out "Hello world!" to our console. the \n prints out new line.
Exits the program with code 0.
Function paranthesis end.
Now, that was a good start how to create a simple console program that outputs stuff on console, yes you can call this function many times and test it out ;-)
NOTE: the variable name is case-sensitive, that means, i is NOT the same as I, and k is NOT the same as K.
Psuedo code example:
data_type var_name;
Every data type of the has have modifiers which are: short, long, signed, unsigned. The modifier defines the storage allocated to the variable:
Ranges:
Every data type of the above has qualifier(s) which is const (constant), const can be written in many ways i.e:
The const qualifier tells the compiler that the variable will NOT be modified after the initialization, that means you cannot do:
i = 10;
A Psuedo function would look like:
A real function would look like:
A function has a return value depends on what data type it uses, for example, a function declared with datatype int, then it must return int or the compiler will complain.
Using the function you've made in the main routine (calling it!):
Comparison operators/relational operators
Arithmetic operators:
Compound assignment operators:
Pointer is 2 things:
1. Reference:
A reference is represented by the (&) operator.
Say we have the following psuedo code:
"first" now is the address of "second" that means we are no longer talking about the content of "first" itself but about its reference,
(i.e, it's address in memory) - Don't worry if you don't understand yet.
2. Dereference:
A dereference is represented by the (*) operator.
Say we have the following psuedo code:
And say we have the following real code:
Yet again, don't worry if you don't understand it fully.
Strings in C:
A String type-name is "char". I explained before in the data types:
char c = 'c'; // that's only a single char! not a full string.
To assign a single character, you must use: ' and not " just like what I did above.
To assign a string, you must use " and not '.
A String in C looks like this:
can also be const:
For more information on strings, see: C Tutorial - Strings and Text Handling
Let's see some psuedo-code:
Real code:
The "for", "do", "while", and "while" statements;
These statements are used for looping with conditions.
Let's see examples:
The for loop:
Basically the for loop psuedo would look like this:
an infinite loop would look like this:
The while loop:
Basically the while loop psuedo would look like this:
an infinite loop would look like this:
The do-while loop:
The break and continue statements:
The switch statement:
Basically the switch statement psuedo would look like this:
Considering A and B are inputs and out is the output:
1. operator AND (&)
A B out
1 1 1
0 1 0
1 0 0
0 0 0
That may have looked alittle weird, but to make it simple the AND operator is more like a multiplication operation.
Example usage in C or C++:
2. operator OR (|):
A B out
1 1 1
0 1 1
1 0 1
0 0 0
Therefore, operator OR loves ones, so whenever it finds a 1 it steals it.
Example usage:
Excersie 2: Write a program that asks the user for your 5 favourite drinks (coca-cola, beer, ...), note that he must write his choice by entering a number between 1 and 5.
If your program uses if statements, try and use switch.
Modify your program so that if the user enters a choice other than 1 and 5 then it will output some error.
Excersie 3: Write a program that asks the user the numbers he want to calculate & using what operation (+, -, /, *, ...) - can be done with switch statements and characters.
I am open to questions and suggestions. Everyone is welcome to post his opinion.
using C NOT C++. Since there's not many people over here who know how to edit source code or fix a simple bug themselves (This tutorial does not explain that ofcourse, but
it should give you a start on how to). I used C because it's a good start to programming.
Programming in C (basics)- Part 1.
After reading this tutorial, you'll be able to create simple console programs that's like any other console program (i.e get output/input from a user, etc.).Some QA anyone would like to know before learning (Psuedo-code-like):
Q: How does a C program look like?
A: a simple C program would like this:
C++:
// A Comment.
/* a
multi
line
comment
*/
include "somefile.h"
int main() {
// ...
return 0;
}
Q: How do I compile a C program?
A: By downloading any C/C++ IDE (i.e DEV-C++, Code::Blocks, etc.) or if you prefer using command line, download MinGW. (protip: stick to IDEs for now if youre not using linux)
Please read Evan's post, he wrote a nice tutorial on how to get started with Microsoft Visual C++ which is the best IDE for C++ : Learning C/C++ - Tutorial
Now, let's begin:
A simple console program to start with
C++:
#include <stdio.h>
int main() {
printf("Hello world!\n");
return 0;
}
Explanation of each line of the previous program:
C++:
#include <stdio.h>
C++:
int main()
C++:
{
C++:
printf("Hello world!\n");
C++:
return 0;
C++:
}
Now, that was a good start how to create a simple console program that outputs stuff on console, yes you can call this function many times and test it out ;-)
Variables
A variable is just a named area of storage that can hold a single value (numeric or character). The C language demands that you declare the name of each variable that you are going to use and its type, or class, before you actually try to do anything with it.NOTE: the variable name is case-sensitive, that means, i is NOT the same as I, and k is NOT the same as K.
Psuedo code example:
data_type var_name;
Data Types
C++:
int (integer numbers) -> int i = 5;
float (floating point numbers) -> float f = 5.0;
double (big floating numbers) -> double d = 500000000;
char (character) -> char c = 'c';
Every data type of the has have modifiers which are: short, long, signed, unsigned. The modifier defines the storage allocated to the variable:
Ranges:
C++:
Type -> from -> to
short int -> -32,768 -> +32,767
unsigned short int -> 0 -> +65,535
unsigned int -> 0 -> +4,294,967,295
int -> -2,147,483,648 -> +2,147,483,647
long int -> -2,147,483,648 -> +2,147,483,647
signed char -> -128 -> +127
unsigned char -> 0 -> +255
float ->
double ->
long double ->
Every data type of the above has qualifier(s) which is const (constant), const can be written in many ways i.e:
C++:
const int i = 5;
int const i = 5;
i = 10;
Functions in C
Full functions explanation, how to make your own function and make use of it.A Psuedo function would look like:
C++:
data_type function_name(parameter1, parameter2)
{
function_body, you write stuff here.
}
A real function would look like:
C++:
int add(int first, int second)
{
int _add = first + second;
return _add;
}
A function has a return value depends on what data type it uses, for example, a function declared with datatype int, then it must return int or the compiler will complain.
Using the function you've made in the main routine (calling it!):
C++:
#include <stdio.h>
int add(int first, int second)
{
return first + second;
}
int main()
{
int a = add(5, 6); // a now is 11.
printf("Adding 5 to 6: %d\n", a); // Try reading documentation of these C library functions to understand how exactly they work ;-)
return 0;
}
Operators in C
C Operators are (That's not everything, just what you need for now!):Comparison operators/relational operators
C++:
a == b -> if a is equal to b.
a != b -> if a is not equal to b.
a > b -> if a is greater than b.
a < b -> if a is smaller than b.
a >= b -> if a is greater than or equal to b.
a <= b -> if a is smaller than or equal to b.
Arithmetic operators:
C++:
a =[/color] b -> basic assignment.
++a -> increment a
--a -> decrement a
a + b -> add
a - b -> substract
Compound assignment operators:
C++:
a +=[/color]b -> same as a = a + b;
a -= b -> same as a = a - b;
a *= b -> same as a = a * b;
a /= b -> same as a = a / b;
Pointers and Strings in C
First i'm going to discuss what a pointer is, as a string depends on pointers.Pointer is 2 things:
1. Reference:
A reference is represented by the (&) operator.
Say we have the following psuedo code:
C++:
first = &second;
(i.e, it's address in memory) - Don't worry if you don't understand yet.
2. Dereference:
A dereference is represented by the (*) operator.
Say we have the following psuedo code:
C++:
second = *first;
C++:
int a;
int *deref;
deref = &a; // store the address of a in a deref pointer.
*deref = 1; // a = 1; how? we stored the address before in the dereference pointer.
printf("a = %d\n", a); // outputs "a = 1"
Strings in C:
A String type-name is "char". I explained before in the data types:
char c = 'c'; // that's only a single char! not a full string.
To assign a single character, you must use: ' and not " just like what I did above.
To assign a string, you must use " and not '.
A String in C looks like this:
C++:
char *string = "hello world"; // able to reassign it
C++:
const char *string = "hello world"; // cannot be re-assigned
Using the "if", "else if", "else", "for", "do", "break", "continue", "switch", and "while"
The "if", "else", and "else if" statements:Let's see some psuedo-code:
C++:
if (I didnt write this tutorial) {
some people wouldnt know how to do their own programs.
}
C++:
int main() {
int num = 2;
if (num == 1) {
// false!
} else if (num == 0) {
// false!
} else {
// true.
}
return 0;
}
The "for", "do", "while", and "while" statements;
These statements are used for looping with conditions.
Let's see examples:
The for loop:
C++:
int main() {
int i;
for (i = 0; i < 5; i++) { // increment i by 1, each time it loops.
printf("i now is: %d\n", i);
}
// let's try something else to do with i:
for (i = 0; i < 100; i += 10) { // increment i by 10 each time it loops.
printf("i now is: %d\n", i);
}
// set i's value to 100, and check everytime it loops if i reached 0 as well as decrease i by 10 each time.
for (i = 100; i == 0; i -= 10) {
printf("i now is: %d\n", i);
}
return 0;
}
C++:
for (start value; condition; increase/decrease value) {
// do work
}
an infinite loop would look like this:
C++:
for (;;) {
// do work
}
The while loop:
C++:
int main() {
int i = 100;
while (i > 0) {
--i;
printf("i now is: %d\n", i);
}
i = 100; // reassign i, since we changed it in the previous loop!
int j = 200;
while (j > i) {
++i;
printf("i: %d, j: %d\n", i, j);
}
return 0;
}
Basically the while loop psuedo would look like this:
C++:
while (condition) {
// code.
}
an infinite loop would look like this:
C++:
while (1) {
// code.
}
The do-while loop:
C++:
int main() {
int i = 0;
do {
++i;
printf("i now is: %d\n", i);
} while (i < 100);
}
The break and continue statements:
C++:
int main() {
int i;
for (i = 0; i < 20; ++i) {
if (i == 19) {
break; // break loop here, makes it not continue anymore.
} else if (i == 17) {
++i; // increment i
continue; // stop it here and then re-run the loop, so i now is 19 because we incremented it manually and the loop will do the same aswell.
}
// here we write some useless stuff that will be excuted if i < 17 and < 19.
printf("i is: %d\n", i);
}
}
The switch statement:
C++:
int main() {
int i = 5;
switch (i) {
case 1:
printf("i is 1.");
break;
case 2:
printf("i is 2.");
break;
default:
printf("i is not 1 or 2.");
break;
}
return 0;
}
Basically the switch statement psuedo would look like this:
C++:
switch (variable) {
incase var is 1:
// do work.
break;
default: // var is not any of the cases above.
// do work.
break;
}
Getting input from user in a C program.
Using the function scanf is the best way for C beginners to get inputted stuff from user, Let's see an example:
C++:
#include <stdio.h>
int main() {
int input = 0;
// loop until the user has guessed the right number which is 5.
while (input != 5) {
printf("Write a random number between 1 and 10: ");
scanf("%d", &input);
printf("\n"); // write a newline to seperate text written on console. printf does not write it by default.
}
printf("You guessed it right!\n");
return 0;
}
Logical Operators
In order to learn how logical operations work, have a look at these tables:Considering A and B are inputs and out is the output:
1. operator AND (&)
A B out
1 1 1
0 1 0
1 0 0
0 0 0
That may have looked alittle weird, but to make it simple the AND operator is more like a multiplication operation.
Example usage in C or C++:
C++:
int i = 1;
printf("%d\n", i & 0); // 0
2. operator OR (|):
A B out
1 1 1
0 1 1
1 0 1
0 0 0
Therefore, operator OR loves ones, so whenever it finds a 1 it steals it.
Example usage:
C++:
int i = 1;
printf("%d\n", i | 0); // 1
Some excersies to do based on what you have learned:
Excersie 1: Write a program that asks the user for his name, age, city, etc. and output them on console!Excersie 2: Write a program that asks the user for your 5 favourite drinks (coca-cola, beer, ...), note that he must write his choice by entering a number between 1 and 5.
If your program uses if statements, try and use switch.
Modify your program so that if the user enters a choice other than 1 and 5 then it will output some error.
Excersie 3: Write a program that asks the user the numbers he want to calculate & using what operation (+, -, /, *, ...) - can be done with switch statements and characters.
I am open to questions and suggestions. Everyone is welcome to post his opinion.