Page 1 of 1 [ 3 posts ] 

MolinaMegaTech
Hummingbird
Hummingbird

User avatar

Joined: 12 Mar 2014
Age: 23
Gender: Male
Posts: 18
Location: In your CPU

26 Mar 2014, 2:05 pm

If you are looking to learn C or if you already know C, It is recommended to learn C++. You don't have to know C to know C++ because C++ is just C but with more libraries and functions added on and has better interfaces than C. For this you will need a compiler for C++ and a recommended one is Code Blocks which you can get here http://www.codeblocks.org/. When you have it installed make a new console application.

Hello World
The hello world program is a widely taught program to beginners and I'm going to go the same way because it is a simple method that begginers will understand.

#include <iostream>

using namespace std();

int main ()
{
cout << "Hello World! \n";
return 0;
getch ();
}
main.cpp

That there is the hello world program. Test it on your compiler to see what it does. Then come back to the post and ill explain it.
-------------------------------------------------

Lets start with the first line of code.

#include <iostream>

This line is required in any program you make. the #include tells the computer that you are going to use a certain library of functions in your program. The <> is where you typed the named library in that you want to include, in this case we used <iostream>.
-------------------------------------------------

using namespace std();

This line of code is really useful to many programmers. It is not required but it lets you write you code in a easier format. If I wouldn't have included that line the rest of my code will have to be written out in words like so in the getch line.

get character none ();
-------------------------------------------------

int main ()

This line names you function. Usually a function named main is required in each program you make. The computer will look for it in an app you make.
-------------------------------------------------

{

}

Curly braces are required and they tell the computer when the function starts, and ends.
-------------------------------------------------

cout << "Hello World! \n";

The cout is a small function that outputs data for you in text. Using << tells the computer that data is going to be outputted. What ever you want to say you put in the parentheses. The \n is not required, but it just makes a new line as if you've pressed enter. (MAKE SURE YOU END EACH LINE OF CODE WITH SEMICOLONS).
-------------------------------------------------

return 0;

This function tells the computer how much extra data will be returned to the program.
-------------------------------------------------

getch ();

If you compile your program and the window that shows the program closes this is the function to use. It makes it so when the program finishes running it[ll wait for you to type a character before closing.

Any questions just ask!



drh1138
Velociraptor
Velociraptor

User avatar

Joined: 2 Dec 2012
Gender: Male
Posts: 498

26 Mar 2014, 5:26 pm

You've got a bit to learn before presuming to teach others if you're explaining (incorrectly) Hello World like this.



Kurgan
Veteran
Veteran

User avatar

Joined: 6 Apr 2012
Age: 36
Gender: Male
Posts: 4,132
Location: Scandinavia

26 Mar 2014, 6:27 pm

Why not use Eclipse instead of Code Blocks? :)

http://www3.ntu.edu.sg/home/ehchua/prog ... howto.html