C#/C++ – Hello World!

C#/C++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!";
     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!";
     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. 🙂

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.

7 Gedanken zu „C#/C++ – Hello World!

  1. well, good questions, Slevi.

    First of all I’m repeating what I learn at school. The above program was the first we wrote to have a first look at the language. I myself know already about cin.get() and intend to introduce it in my tutorials at a later date. Especially as I have no clue when we will have it at school.

    I assume that getch() is a C/C# function but I’m not sure on it.

    Further, our teacher never told us anything about the variables in the main function, otherwise I would have included a brief description. On this one I’m as clue less as my readers. But I’ll ask my teacher about that as soon as I see him again because the meaning of those variables is also bugging me for some time now.

    I hope I was able to clear things up a bit 🙂

  2. It’s a C function, C# has Console.Read :).

    And haha, if they weren’t gonna tell what it was anyways they could just as well have skipped placing them till they’d explain since they’re not needed to make your program run :P.

    But it’s command line arguments, so if you’d start up your program with something like „test.exe bla whatever“ you’d pick up the bla and whatever arguments.argv[0] is the name of your own application, argc is the count of arguments given. So if you want to display them you could do something like:

    for (int i = 1; i < argc; i ) {
    cout << argv[i] << endl;

    Bit more advanced than the tutorial, but for if you’d already know what a for loop is and what an array is you should be able to understand that part quite well :).

    Slevi’s last blog post..Joost not quite the revolutionary TV?

  3. I guess our teacher will tell us about this later when we are going to need it…

    Anyway, thanks to PHP I have quite an advance compared to my colleagues and…to be honest…I assume that I’m one of the best (if not the best) in the programming class at this moment 🙂

    Long story short, I do know how a for loop works and I know what an array is. Thanks for your detailed explanation on the argv part.
    Still, I will bug my teacher, maybe he will tell my colleagues what these arguments mean 😉

  4. I hate double-commenting, but I prefer it in this case instead of editing my last comment 🙂

    I talked with my teacher and I was right. We will talk about those arguments at a later date. At least I was able to show off my knowledge a little bit, gaining a few more plus points. Big thanks for that information, Slevi 😀

    Although we didn’t have it at school, yet, I will update my tutorial later and add the piece of information about these arguments. Of course you will get full credit for that part of the tutorial 🙂

  5. Haha, so now you’re on the good student list eh? 😉 And always a pleasure to help, I still know when I began teaching myself programming years ago it was a complete pain when running into stuff like that without it being explained.
    These days though it tends to be the complete opposite, there’s so many people which began programming you might end up getting sloppy style when relying on what you find on the net too much.
    A great example in the hello world is rather than using getch() or cin.get() to use a system(„PAUSE“), whereas the first are solutions to the problem and the latter is just a quick hack.
    Funny thing though is that in high school I had a teacher which actually tried to teach me the system pause method :P.

    PS: Second time it happened now, so perhaps you changed something with the commenting form but for some reason it keeps taking out the breaks?

    Slevi’s last blog post..What’s your excuse to blog

  6. I was already on the good student list…even at the top already. I just made sure that I stay at the top 😉

    All tutorials I found so far never used getch(), cin.get() or even system(„Pause“) and believe me, I tried a lot on google… Hopefully this tutorial will help there somehow. Although I wish at school our teacher would do it either C or C# style as these are the languages we are supposed to learn. Using a C function is in my opinion not good…

    About that system(„Pause“), I never heard about it (same with cin.get()) until I did some research during my vacations as I wrote small program to show off again 😉
    I guess I can call me lucky that my teacher doesn’t try to teach us system(„pause“) 😈

    @breaks: I didn’t change much, only added NicEdit…and it is working fine on my end…

Schreibe einen Kommentar

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.