pãgës

Tuesday, September 14, 2010

C++ Hello World Tutorial

This C++ Hello World tutorial is written for beginning C++ students without previous C++ programming experience. It is traditional writing "Hello World" Program when start learning any new programming languages.


#include
using namespace std;
int main()
{
cout << "Hello World!" << endl;
cout << "Welcome to C++" << endl;
}


iostream is a header file which is used for input/output in the C++ programming language. It is part of the C++ standard library.

using namespace std; keeps you from having to type std::cout each time that output is needed.
The cout object is of type ostream, it is available in std namespace.

int main() is the main function which executes first, before any other code.

cout << is the output statement and strings need quotation marks. cout overloads the left bit-shift operator to make it perform an operation completely unrelated to bitwise operations.

C++ statements and edded with semicolon. So end the cout line with a ; (semicolon)

The << endl means that the next cout will have a new output line. endl is an output manipulator that writes a newline and flushes the buffer, ensuring that the data is output immediately. An alternative to the newline character is `\n`.

save the above file as HelloWorld.cpp
compile the above program using GNU g++ compiler (Linux).
g++ HelloWorld.cpp -o HelloWorld
The output file name will be HelloWorld . Run the file using ./HelloWorld
You can see the output in two Lines

Hello World!
Welcome to C++


Happy Coding :)

No comments: