As I have the fortune to learn C#/C++ at school I thought it is a good idea to share what I learned and what I’m learning. Additionally it helps me to repeat what I learned and I can make sure that I really understood it.
Here we go:
Hello World
At first we need a program which allows us to write and compile C#/C++ code. We are using the Borland C++ Builder but as I’m a fan of open-source programs I’m training my knowledge using the Bloodshed Dev-C++ program unless I’m forced to use Borland.
Now let’s have a look at our first program.
The Code
#pragma hdrstop
#include <conio.h>
#include <iostream.h>
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#pragma argsused
int main(int argc, char* argv[])
{
cout << "Hello world!";
getch();
return 0;
}
Copy and paste the code into your C++ program, compile and execute it. A console window should open displaying „Hello world!“. Press any key to close the window again.
Now as your program is working, let’s have a look at the code itself.
Detailed Explanation
#pragma hdrstop
[...]
#pragma argsused
These two lines hide possible error warnings in Borland. The program will even work if you leave them out.
#include <conio.h>
#include <iostream.h>
„conio.h“ is a library required to create a program which runs in a console window. This „abbreviation“ means CONsoleInputOutput.
„iostream.h“ is a library required to allow the program to output streamed content, also it allows streamed input. This „abbreviation“ means InputOutputSTREAM.
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
The „//“ allows you to add a single line comment to the code. We use the line „- – – -“ to separate the includes from the actual code.
int main(int argc, char* argv[])
{
[...]
}
This function is the main function. Inside it you have every code you need to run the program. Later you will learn to create your own functions, so I stop here only stating that this function is always the last one of the whole program. Meaning: It has to stay at the bottom of the whole code.
As Slevi outlined in his comment, the „int argc, char* argv[])“ is used to receive the arguments in case you start the exe via console e.g. „example.exe foo bar“. As this is already advanced coding I skip a more detailed description. I’ll come back to it in a later tutorial.
cout << "Hello world!";
getch();
return 0;
Now we look at the actual code which allows us to display „Hello world!“ in the console.
„cout“ allows you to output any kind of data. In this case it is „Hello world!“, later you will also learn about other types of data..
„<<„ is a shift operator which moves the output to the console screen. You could say said „cout“ is the screen and „Hello world!“ is moved to the screen by the „<<„.
„Hello world!“ is a so-called string which is displayed in the console. That’s why it is required to wrap it with the quotation marks.
„getch()“ is a function which waits for any kind of user input, basically it waits that the user is pressing any key to continue. We use it here to prevent the console from closing again right after displaying our „Hello world“. For the record, this is a solution I was looking for when I tried to learn C++ on my on a couple of years ago. All other tutorials on the Internet did not have this function in the code, leading to an instantly closed console window. Hopefully this tutorial will help other beginners to avoid my problem back then.
„return 0“ informs the program that the code has been processed and the console window can be closed, after the mentioned user input. It is not possible to avoid using „getch()“ and use „return 1“ instead, as this one will also exit the program.
Wrap Up
Now we are finished and got our first program working, if you encounter any kind of problem, leave me a message and I’ll try to help you.
P.S. Feel free to play around and try different messages just for the fun of it.
Update:
I just had to add this note about getch():
The getch() function is a C function, but since I’m writing a C#/C++ tutorial it would be more appropriate to use cin.get() instead. It does the same but is the C++ variant of the command. Either method should work and I will get back to cin.get() in one of my next tutorials to make sure that any questions about cin.get() get answered.