Jump to content

"Hello world!" in C++

- - - - -

  • Please log in to reply
8 replies to this topic

#1
Csabi

Csabi

    Learning Programmer

  • Members
  • PipPipPip
  • 62 posts
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;

   system("pause");

}
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
or
/*Comment line 1

Comment line 2 */
Here you can find the original lesson: Hello world in C++

#2
VakhoQ

VakhoQ

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
if we wont to remove text "preas any key to continye" we should write:
 system("pause>0"); 


#3
sk8rjess

sk8rjess

    Newbie

  • Members
  • Pip
  • 4 posts
good start, thanks!

#4
0x000223

0x000223

    Newbie

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

cin.get();

Much cleaner, and takes way more less resources.

#5
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200

0x000223 said:

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.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#6
Marcoso

Marcoso

    Newbie

  • Members
  • PipPip
  • 12 posts
Easily written and everything is explained. :) Thank you!

#7
kimmy123

kimmy123

    Newbie

  • Members
  • Pip
  • 3 posts
I think its a good.

#8
Rishi Sharma

Rishi Sharma

    Newbie

  • Members
  • Pip
  • 2 posts
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 ??

#9
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 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
It's sometimes needed by old compilers.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users