If you already know the language, then this will seem very boring. This is designed for those who know nothing about C/C++.
To begin, C++ is just an extension of C that adds functionality. So to learn C++ you need to learn C first. In both, everything is case-sensitive which means that something named "ABC" is not the same as "abc" or "AbC". All are unique names.
In C, each command is followed by a semi-colon " ; " signifying the end of the command, and extra spaces, tabs, and line breaks (collectively called "white space") are ignored. So, you could put multiple commands on the same line if you wanted.
I.E. the line:
a=3;b=1;c=a+b;
is the same as:a=3;
b=1;
c=a+b;
b=1;
c=a+b;
But, for simplicity and to make it easier to debug (find and remove problems) you'll want each command on a separate line. You can also create segments of the code that the compiler (what turns the code into a program) will ignore by using a double backslash " // " (which will ignore everything after it on the same line) or by using backslash-star " /* " to start and star-backslash " */ " (which can span multiple lines) to end. These are called comments because they are commonly used for explaining what your code will do in a section.
So we could have written the above code like this:
a=3; //This part is ignored
b=1; /* and so
is this */
c=a+b;
b=1; /* and so
is this */
c=a+b;
In C, code can be separated into sections called blocks by using the curly brackets " { " and " } " to open and close a block respectively, and each command that you create (function) will need to have a block of code directly following it's name. So, lets create a function shall we:
int Add(int a, int b) //int is a variable type I will talk about in a second
{
return a+b; /* "return" causes the function to exit with the value that follows it (must be the same type the function is defined as) */
}
{
return a+b; /* "return" causes the function to exit with the value that follows it (must be the same type the function is defined as) */
}
Now, I introduced several new things in this so lets review them. First, a function is created by first calling the type of variable the function will "return" followed by its name and a set of "parameters" inside of a set of parenthesis " ( ) ". When you call this function later you simply use the name you provided and fill in the parameters you want (I.E. you could call "Add(1,2);" and it would return "3".
Variables are the places where you store data. When you create a variable you define its type followed by it's name. In our example above we created two variables in the function declaration: int a and int b
int is a variable type that holds a single number (if your on a 64 bit system its range is from –2,147,483,648 to 2,147,483,647). int by default is signed meaning its first bit tells whether it is positive or negative but this reduces its maximum value by about half. If you wanted it to not contain this sign bit, you could call it as unsigned int.
The unsigned keyword changes a variable that is default "signed" to be unsigned. the signed keyword does the opposite.
Other data types are:
void - holds no data at all. (used later)
bool - holds a single bit. (true or false)
char - holds a unsigned number 1 byte long. (0 - 255)
short - holds a signed number half the size of int
long - holds a signed number twice the size of int
float - holds a signed floating decimal point number (1 of 2 only default types that can contain a decimal)
double - holds a signed floating decimal point number twice the size of float (also called double precision)
in C those are the basic types. You may have noticed that they all only contain numbers. "Well how do you store text?" you might ask. The char type can contain a single letter as well by placing single quotes around it. Such as 'a' would contain the number corresponding to "a" (or 61) in ASCII. To store more than one letter you need an "Array" which I'm not going to talk about this time. (next C lesson maybe)
That's enough basics for now. I'll continue with more later.
Until next time. Have fun.
Edit: I forgot the double var type.
Edit2: I forgot the bool and void types as well.
i got half way through this and had a brain meltdown o.o i shall return to finny!
ReplyDeletehey this can be really useful for beginners dude, good post
ReplyDeleteamazingly good tut for teaching basic C, will def check back here later for more programming topics~
ReplyDeletePretty confusing but I think I get it so far. Following.
ReplyDeleteWow I may actually check this out :O
ReplyDeleteoh man its been forever since i did any c/c++ i think i took a few classes in college
ReplyDeletewish i could read that stuff!
ReplyDeletekeep up the good work
good shit furfag
ReplyDelete>=O wut?
ReplyDeleteYay! Love it ! Check out my blog too ! It spreads knowledge too +with funny pics
ReplyDeleteSupport in spreading some knowledge:
tetru.blogspot.com
Know this already but still good information for beginners.
ReplyDeletethis will prove helpful in my near future...
ReplyDeletethx
I'm not sure I understand all of this, will have to check back when fully awake.
ReplyDeleteFollow my blog as well, at:
http://wrestlinginteresting.blogspot.com/