Topic:   C/C++ tutorials   (Read 45272 times)


0 Members and 1 Guest are viewing this topic.

GMG Kurt


  • GMG-er

  • **


  • Posts: 682

  • Sorry for being such a noob
C/C++ tutorials
« on: August 04, 2011, 02:25:08 PM »
during my extended leave due to me moving states.
I learned C and C++
yay! So I'm doing a tutorial seris on it, and I have two tutorials to upload

I'll be posting tutorials every day, if I can

we'll start with C, and move into C++

tut1: Maths

C/C++ math is similar in every way to GM/SC math, except a few added features
we'll review the features of GM/SC/ math
•the basic math operators +-*/
•the inability of the complier to square, or root (there is a way to do this in C, but we'll discuss this later)
•allows variable to be used in it
the additional features are:
•you don't need spaces in between characters
•three additional operators
•different use of the equal sign

modulus:
[spoiler]the modulus operator is a simple, yet weird addition that is essential.
it looks like this '%' and finds the remainder of a division.
ie.  5 % 2 = x
this would set x to 1, because 5 divided by two has a remainder of 1
22 % 10 = x
x = 2

you use this for verifying whole numbers, and for isolating didgits[/spoiler]

increment operator:
[spoiler]I really shouldn't tell you guys about this, because I can't fully elaborate on it's uses, without losing you.
treat this as part one of increment operators
increment opperators syntax examples are as follow
i++ ++i i-- --i
these are all the different uses of it. they add/subtract by one.
i being of type
int
float
double
short
unsigned
char

these will be more explained tomorrow when I enumerate on types. so the above, in laymen terms, is anynumber (or a char, which would increase it's ASCII value, but is a bad programing practice)
currently treat
++i, and i++ (and the minus versions)
as the same, but there's a small difference that we'll explain latter[/spoiler]

assignment opperator/ use of the equal sign:
[spoiler]two concepts in one.
The equal sign written as '=' is the assignment operator. as in 3+4=x
assignes the value of 6 to x, and when used as '==' it checks for equality.
where it either evaluates to 0 or 1, whether it's false or true(in that order)
this is used with function such as if()
again this is something we'll explain later with functions

back to assignment operators
the statements
x = x + 1
and
x += 1
are equivalent.
you can do this with al of the other operators

+=
-=
/=
*=
%=

and some other operators called the bitwise operators, that we won't cover.

here's some more syntax examples
variable_with_a_stupid_long_name = variable_with_a_stupid_long_name / 8
and
variable_with_a_stupid_long_name /= 8

now you can see it's use^
cake -= 5
[/spoiler]

P.S. typing these lines straight into a C compiler won't work.
good luck feel free to ask questions  


Just your average Weekend Warrior.
Yes I know I have bad spelling, it's what makes me such a good programmer!

"Old art, weather magnificent or wretched, is always the raw material of new art. The artist's job, though, is to

GMG Kurt


  • GMG-er

  • **


  • Posts: 682

  • Sorry for being such a noob
Re: C/C++ tutorials
« Reply #1 on: August 04, 2011, 02:27:17 PM »
tut2: defining your variables

this is a simple, yet important concept:
variables must be defined before use, and it attributes the amount of data they'll use, and hoe they'll be treated by the compiler.
this is a quick run-down of all of them,  and will still explain them as they're used in future tutorials

int - as in integer, a number that ranges from -32768 to 32767 these numbers should be familiar to any GM user.
thats because both int, and the standard numerical variable on GM use two bytes of information to stare the data. int stands for integer.

unsigned - the same as integer, but it can only be positive (or zero) to the range is farther 0 - 65535 is also a modifyer

char - a character value that is stored in the computers information as ASCII, and therefore we can use mathematical expressions on it.
it only hold one character

float - as in floating point integer. can also be expressed as double which gives it "double accuracy" and long(explained below)

short/long - these are modifiers, so you would write short or long before int, unsigned, or float
for int it'd be -21474836 to 21474835 it only allocates two more bytes of data(4 total) and the increase is so large because it's expressed in binary which is in base 2, and exponentially based (<<you shouldn't understand that cocept yet. I'll explain it at the bottom of this) exponential
for short is the default

signed - also a default it's for int, float, and double

part2:
assigning variables:
you assign them with the assignment operator '='
one of two ways

Code: [Select]
int x;  //allocated
x = 5;//initiallized
x was allocated, and then initialized.  two things I'd like to mention
•those semicolons mark the end of the line, I'll talk more about them in the next tutorial
•'//' is the character for comment, everything after that isn't read by the compiler. exactly like the REM command in GM
also know as the comment command or, "that little apostrophe thingy"

second way is all in one line
Code: [Select]
int x = 5;
does the same thing, I think you can work it out

Code: [Select]
char c = 'k';
Code: [Select]
char c = 107;
these do the same thing since k is read as 107 (it's ASCII value)

Code: [Select]
char c1 = 'k';
char c2 = k;

the first statement works and the second doesn't
Code: [Select]
char c = 'klm';

this is technically sorest, but is a very bad programming practice. it will set c to 'k' then to 'l' and then to 'm'
only one at a time so therefore c would equal m (if your reading the book I mentioned along with my tutorial you will notice that this isn't mentioned in the book.
my work is both consistent, and inconsistent with his work)


Code: [Select]
signed short int x1;
int x2;
these would be of the same data type, and allocate the same memory.
just an example to reiterate the default

Code: [Select]
int x, c, v, b;
x = c = v = b = 0;

did I just blow your mind? two new concepts
•you can declare multiple variable via the comma operator
•you can initialize multiple variables, it is important the zero is on the right side since the assignment operator (=)
evaluates right to left.
this sets b to zero v to b (which is 0) and soforth
Code: [Select]
int x = 0, c = 0, v = 4, b;
this sets x to zero c to zero and v to 4.
b is set the the nearest data address, which may or may not be zero. it's just left over data in the computers memory. if it's a spot in the memory never been use then yes, it could be zero,
but you have no way of knowing it. don't gamble on using uninitilized variables.

part3:
different bases
ADVANCED TOPIC ALERT

65
this number was expressed in base 10, decimal form
0xAE4
this is also a number, but expressed in base 6, hexadecimal form
0177
base 8 octal form

these can all be written to the integer variable type, if you haven't studied exponents then leave now (I don't mean to be rude, but
if you don't already know that this'll demoralize you from learning C/C++)

hexadecimal numbers always start with 0x  (thats a zero)
so 0x54 is hexadecimal. To find the number you take the digits to powers of 16 (8 for octal, and 10 for decimal)
example:
0x54
5 X 16^1 + 4 X 16^0

remember anything to the power of zero is one
each digit of hex form can be 0 - f, or 0 - F (a number has to have all the "letters" in the same case)
numbers being themselves.
A - 10
B - 11
C - 12
D - 13
E - 14
F - 15

octal is the same followed by a zero, and numbers 0-7
and although we don't thing about it decimal form is the same

123
1 X 10^2 + 2 X 10^1 + 3 X 10^0

I sometime use hex form for writing large numbers, but rarely.

P.S. if you haven't already noticed I'll be making GM comparisons since the languages are similar to help GM programmers get a leg up on C programming.  :D
good luck

P.P.S it said it was too long so I had to do it in two posts
« Last Edit: September 24, 2011, 09:01:27 PM by KurtManion »
Just your average Weekend Warrior.
Yes I know I have bad spelling, it's what makes me such a good programmer!

"Old art, weather magnificent or wretched, is always the raw material of new art. The artist's job, though, is to

GMG Kurt


  • GMG-er

  • **


  • Posts: 682

  • Sorry for being such a noob
Re: C/C++ tutorials
« Reply #2 on: August 04, 2011, 02:59:59 PM »
just finished the third installment

tut3
writing an application.

now here's the good stuff
Code: [Select]
int main()
{
printf("hello world\\n");
}
the first thing a C (notice I didn't say C++)program executes is it's main function. it's execute what's in-between the
curly braces. our program also incorporates a printf statement which is just like the print statement in GM. except with different syntax
this will print hello world to the consol, and then make a newline. That is what the character \\n is. it's the newline character. the compiler treats
this as a single character much like a, or b. but when encountered instead of writing it, it makes a newline. the backslash starts all special characters such at this
to write a real backslash write //
also considered one character
 printf statements can also take variable as arguments and print them
Code: [Select]
int x = 1;
main()
{
printf("%d", x);
}
this prints x in decimal form (hence the d)
there are many other character converters, and even modifiers to them.

one more thing I'd like to say on this. if you actually typed this into the compiler and ran it, you should see some annoying warning telling you main() epaulets to int.
now you should be mildly confused because I didn't tell you about returning values. main will ALMOST always return an int.
0 for: every line of code has been finished so I'm going to quit incorrectly now!
1 for: got it I just received the quit command
so the program should look like this
Code: [Select]
int x = 1;
int main()
{
printf("%d", x);
        return(1);
}
the parenthesis aren't necessary, but a convention.

part 2:
scanf:

scanf has similar syntax to printf, but it read for info from the user
Code: [Select]
int d;
int main()
{
scanf("%d", &d);
printf("%d", d);
}
scanf looks for an integer the user writes, and then writes it again(on a new line)
simple, and versatile concept.

now I have a challenge for all of you.

if temperature can be converted from fahrenheit  to celsius by the equation C=(F - 32)/1.8
then program a program to ask the user for the temp in fahrenheit and then writes it in Celsius.
post it here, and I'll comment on all the good and bad.
even if your program doesn't work still upload it.
P.S. make sure to keep it, because we'll be doing something else with it latter.
Just your average Weekend Warrior.
Yes I know I have bad spelling, it's what makes me such a good programmer!

"Old art, weather magnificent or wretched, is always the raw material of new art. The artist's job, though, is to

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: C/C++ tutorials
« Reply #3 on: August 04, 2011, 08:13:45 PM »
Code: [Select]
#include <stdio.h>
#include <stdbool.h>

main()
{
 Â  bool running = true;
 Â  while (running==true) {
 Â        float f = 0.0;
 Â        printf("Please type in the degrees in Fahrenheit: ");
 Â        scanf("%f", &f);
 Â        printf("The degrees in Celsius: %f\\n\\n", (f - 32.0)/1.8);
 Â        printf("Would you like to go again? [Y/N] ");
 Â        char c;
 Â        while (getchar() != '\\n');
 Â        scanf("%c", &c);
 Â        if (c != 'Y' && c != 'y') {
 Â           running=false;
 Â           printf("Thank you!\\n");
 Â        }
 Â  }
}

Nice tutorial.
« Last Edit: August 04, 2011, 08:15:10 PM by Gandolf »

Connors


  • ^ This guy is amazing.

  • ****


  • Posts: 2374

  • It's a secret to everyone...
Re: C/C++ tutorials
« Reply #4 on: August 14, 2011, 11:50:49 AM »
I could probably help proof-read some of this. ;)
Warning: The above post may have been modified multiple times.

"In a great game, the character must never perfectly obey the user's command"
 - Tim Rogers

http://connorspuzzles.tumblr.com/

GMG Kurt


  • GMG-er

  • **


  • Posts: 682

  • Sorry for being such a noob
Re: C/C++ tutorials
« Reply #5 on: August 15, 2011, 01:39:49 PM »
Quote
I could probably help proof-read some of this. ;)
;) thanks, I try to proof-read it, but I kinda suck at it, I'd really appreciate if you could help proofread.  :D
« Last Edit: August 16, 2011, 08:51:13 AM by KurtManion »
Just your average Weekend Warrior.
Yes I know I have bad spelling, it's what makes me such a good programmer!

"Old art, weather magnificent or wretched, is always the raw material of new art. The artist's job, though, is to

GMG Kurt


  • GMG-er

  • **


  • Posts: 682

  • Sorry for being such a noob
Re: C/C++ tutorials
« Reply #6 on: August 16, 2011, 06:11:35 PM »
I made a video to summarize all of the above basic material.
it's 13min, but used to be 22min  ::)

http://www.youtube.com/watch?v=NZUVXIGsMtQ

I really need feed back, on my seris before I start part two, so please comment on how part one went (including the video)
Just your average Weekend Warrior.
Yes I know I have bad spelling, it's what makes me such a good programmer!

"Old art, weather magnificent or wretched, is always the raw material of new art. The artist's job, though, is to

GMG Kurt


  • GMG-er

  • **


  • Posts: 682

  • Sorry for being such a noob
Re: C/C++ tutorials
« Reply #7 on: August 17, 2011, 12:16:06 PM »
Quote
Nice tutorial.

thanks Gan it's nice to know someone thinks so  ;D

I'll be leaving tomorrow until Wednesday or Thursday, and resume the tutorials after that, Everyone thats onboard please finnish part 1, including the video.  ;)
« Last Edit: August 17, 2011, 12:16:19 PM by KurtManion »
Just your average Weekend Warrior.
Yes I know I have bad spelling, it's what makes me such a good programmer!

"Old art, weather magnificent or wretched, is always the raw material of new art. The artist's job, though, is to

GMG Kurt


  • GMG-er

  • **


  • Posts: 682

  • Sorry for being such a noob
Re: C/C++ tutorials
« Reply #8 on: August 27, 2011, 06:02:06 PM »
part two of my C/C++ tutorial is now underway. I’ve developed a foolproof way to execute it, but it requires an intro briefly touching on each of the subjects


1Compiling (UNIX) How to compile your program when you can’t use Xcode.
2Functions/preprocessor You finally fully learn what makes up C/C++, how procedural programming works, and some of the neat features of the preprocessor.
3 Making decisions You learn the key concepts of C/C++ programming. Such as:
for, while, do, goto, break, and continue.
4a Arrays Topic of arrays.
4b Character strings Using and manipulating  an array of characters to use strings.
5 Structures A data type composed of different data types (of your choice), including pointers.
6 Pointers The hardest, most complex, and important type you’ll find out you’ve worked with all along!


you mearly need to know the names: structure, array and pointer. For it too work, but I though a table of contents would be a nice addition. Eache of these will be posted per day for the next week, and as I said before I’d like feedback to know how to make part 3 better.

P.S. part three is the last part on procedural programming, after that we get into object-oriented programming, and I reveal why it’s called the C/C++ tutorial, and whats with the weird name (C++).
« Last Edit: August 27, 2011, 06:08:00 PM by KurtManion »
Just your average Weekend Warrior.
Yes I know I have bad spelling, it's what makes me such a good programmer!

"Old art, weather magnificent or wretched, is always the raw material of new art. The artist's job, though, is to

x


  • GMG-er

  • **


  • Posts: 247
Re: C/C++ tutorials
« Reply #9 on: August 31, 2011, 02:40:53 AM »
You picked a good language to write a tutorial on. Lots of people want to know it, and its small and easy to explain. You're doing a good job too, keep on going I say!

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: C/C++ tutorials
« Reply #10 on: August 31, 2011, 08:32:16 AM »
That actually looks really interesting. I'm kinda excited now.

x


  • GMG-er

  • **


  • Posts: 247
Re: C/C++ tutorials
« Reply #11 on: September 02, 2011, 04:36:24 AM »
Why is this called a "C/C++" tutorial when theres no C++ at all by the way?

Connors


  • ^ This guy is amazing.

  • ****


  • Posts: 2374

  • It's a secret to everyone...
Re: C/C++ tutorials
« Reply #12 on: September 02, 2011, 05:13:50 PM »
Hey guess what! I'm now in a programming class. We're learning C now and moving on to Java in the spring.

Oh and Kurt said he will make C++ tutorials later.
« Last Edit: September 02, 2011, 05:17:48 PM by Connors »
Warning: The above post may have been modified multiple times.

"In a great game, the character must never perfectly obey the user's command"
 - Tim Rogers

http://connorspuzzles.tumblr.com/

x


  • GMG-er

  • **


  • Posts: 247
Re: C/C++ tutorials
« Reply #13 on: September 02, 2011, 11:43:59 PM »
Ok that makes sense. Most people tend to skip C and just learn C++. However I personally found learning C before C++ was a huge benefit. So thats good in my opinion.

In addition, Java is a nice language (when put in the right context) and I have found it pretty useful when it comes to getting a job.
« Last Edit: September 02, 2011, 11:44:14 PM by x »

GMG Kurt


  • GMG-er

  • **


  • Posts: 682

  • Sorry for being such a noob
Re: C/C++ tutorials
« Reply #14 on: September 04, 2011, 03:59:21 PM »
so sorry for me absence hurricane Irene knocked out my power, and I just got it back, and in my opinion it shows how ineffective one singular electrical company can be >:(

and Connors is right I am teaching C++ later in this, since C leads up into C++, especially because of all the underlying concepts.
Hopefully I'll have tut1 done today, and maybe even tut2!
Thanks for all the support you guyes  ;D
Just your average Weekend Warrior.
Yes I know I have bad spelling, it's what makes me such a good programmer!

"Old art, weather magnificent or wretched, is always the raw material of new art. The artist's job, though, is to