Tutorials
-=||Code||=-
#include
using namespace std;
int main()
{
/*This is a comment*/
//This is another comment
cout<<"Hello world";
return 0;
}
-=||Explanation||=-
What does this code do? It prints Hello world to the screen. How does it work?
Well, this code is easy to dissect. Let us take a look at each line and see what role it plays in the final product.
#include
using namespace std;
A namespace is nothing more than a group of variables. We are specifying that we want the namespace "std" . That namespace contains cout. If you were to remove it, you would get a compiler error on whatever line cout was on. Now, here is something interesting, replace "cout<<" with "std::cout<<". Notice that you get no compiler errors because std is mentioned in the iostream header and you are calling cout with the :: operator to tell that std owns cout.
int main(int argc, char **argv)
For general knowledge, you may come across the following formats:
int main()
{
}
int main(void)
{
}
int main(int argc,char **argv)
{
}
int main(int argc, char *argv[])
{
}
int _tmain(int argc, _TCHAR *argv[])
{
}
Generally, these mean the same thing and will all compile correctly with the exception of the last one that will only work if you are using a Microsoft Visual C++ environment. For now, know that int means that the program will return an integer (number) and that you must have a main program.
The main program is what is first called once other stuff is taken care of. More specifically, in C++, you must have an int main program. Why? Because if your code runs right, by convention you are supposed to return 0 if everything goes right.
cout<<"Hello World";
cout is pronounced as "see out" and not "kowt" or "sout" and thus it stands for "c out". It is the variable that data is shifted to when you want to write some text that you would SEE printed OUT on the screen.
return 0;
As mentioned earlier, it means that you wish to exit the program with 0 to tell that everything went right.
Finally, some more that you may have noticed particularly the "/**/" and "//" used in the code, the semicolons, the braces and the semicolons. "/**/" and "//" are known as comments and allow you to write comments (hence the name) to help you remember what that particular line or group of code does. Experts recommend commenting your code to save time brushing up on its purpose. Note that /**/ are used for multi-line comments while // are used for quick simple one-liner comments.
The braces are used to group together a block of related code. They serve the same purpose as quotation marks that group together related statements.
Semicolons serve the same purpose as periods in the English language and others related to it (i.e., Spanish, French, etc.). They are used to help the compiler separate lines of code. You may ask why comments do not have semicolons in the example. The answer is what purpose does it serve? Everything on the same line as the // style comment is ignored. You know when a /**/ style comment is ended because both /* and */ have to be in the code. Everything inside them is ignored while everything outside is looked at as valid code.
-=||Summary||=-
#include -means to get whatever is specified by the user.
using namespace -means that we want to get whatever group of variables is specified
int main -means the code will have a main function that will return an integer (number) when finished executing code
cout<<"example text" -means that we want to see example text printed out on the screen
return 0 -the function should return 0 if everything went well.
{} -used to group together related lines of code
;- has the same function in C++ as periods do in English. They are used to tell the compiler when you are dealing with a new instruction
//-used to comment out a single line and everything on the same line as it is ignored
/**/-this is used to comment out multiple lines of code and everything inside them is ignored
-=||Learning Resources||=-
http://cprogramming.com
http://cplusplus.com
http://youtube.com/antiRTFM
http://www.youtube.com/watch?v=EwvfBtC1nL4&feature=related