Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

"Hello world!" in C++

c++ hello world

  • Please log in to reply
8 replies to this topic

#1 Csabi


    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 60 posts

Posted 01 December 2010 - 01:39 AM

In this lesson I will show you how to create a simple program, called Hello world, which will simply just write out "Hello world!". A Hello world program is the best way to start in programming language, because this gives a basic understand of the syntax of the code.

First set up an empty Win32 Console Application project and add a C++ file (main.cpp) (click here to see how)

Write the following code and press F5 to run it:
#include <iostream>

using namespace std;

int main(){
   cout << "Hello world!" << endl;
The first line includes the iostream library which provides the input and output functions. Without this function we can`t write out the result of the program. Notice that wen a library is included #include is used.

The second line tell the compiler to use the std namespace, namespace allows to create groups of functions or objects under a name. Don`t worry, I will write more about namespace later. For now it`s good to know that if you leave this line from the code you will need to include std:: before each cout or cin from your code. Notice that the line is ended by a semicolon (";");

The third line starts a function called main, which will return and int (integer). The main function starts first in any program. The curly braces { and } shows the beginning and end of the function.

The next line writes out the "Hello world!" text. Notice that the line is ended by a semicolon (";").

The next line is telling the compiler to pause the program and wait for the user to press Enter. Without system("pause") the program will run so fast that you will not see the result.

Let`s see the program in action:Posted ImageNote: Don`t worry if you haven`t understand everything. This lesson is written to have an idea about how a program looks and to see the basic syntax of C++.

Another useful and simple thing to learn are comments: In C++ you can add comments (notes) after the code lines. The comments are skipped by the compiler and they don`t affect the program. Comment are helpful to note or explain things. The comment syntax is:
//Comment goes here
/*Comment line 1
Comment line 2 */
Here you can find the original lesson: Hello world in C++
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#2 VakhoQ


    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 126 posts

Posted 18 December 2010 - 07:32 AM

if we wont to remove text "preas any key to continye" we should write:

  • 0

#3 sk8rjess


    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 07 February 2011 - 12:30 AM

good start, thanks!
  • 0

#4 0x000223


    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 01 August 2011 - 04:51 AM

Instead of using system("pause") which is a terrible thing to do, you should use:


Much cleaner, and takes way more less resources.
  • 0

#5 Alexander



  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh!
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Delphi/Object Pascal, Visual Basic .NET, Assembly, Fortran, Bash, Others

Posted 01 August 2011 - 05:21 AM

Much cleaner, and takes way more less resources.

Resource usage would be negligible for this portion of the application, however it would be better to employ a curses like library if you were to wish to manipulate/pause the console more than once.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.

#6 Marcoso


    CC Newcomer

  • Just Joined
  • PipPip
  • 10 posts

Posted 11 December 2011 - 03:29 AM

Easily written and everything is explained. :) Thank you!
  • 0

#7 kimmy123


    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 29 January 2012 - 11:09 PM

I think its a good.
  • 0

#8 Rishi Sharma

Rishi Sharma

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 22 February 2012 - 01:53 PM

Why do we sometimes use #include<iostream.h> rather than #include<iostream> .

Why do we put "< .h>" in addition to "<iostream>" ?? What is the pourpose of Extra .h ??
  • 0

#9 WingedPanther73


    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 22 February 2012 - 03:58 PM

It's sometimes needed by old compilers.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download