Home C++ examples and tutorials

Introduction

C++ is a programming language. A multiparadigm, object-oriented, strongly typed programming language. The "Hello World" C++ example is:

#include <iostream>
int main(){
	std::cout << "Hello World!";
}

Explanation

The #include directive on the first line:

#include <iostream>

is the preprocessor directive that includes the iostream header needed for the output. The main function is the main program entry point. It is of type int and the scope of the function is determined by the {} braces.

int main(){
	// code goes here
}

The cout command outputs the string literal using the << operator and is part of the std namespace hence the std::cout syntax. :: is the scope operator.

std::cout << "Hello World!";

Each statement in C++ ends with a semicolon ;

Examples

We can output multiple string literals by separating them with the << operator:

#include <iostream>
int main(){
	std::cout << "Hello World!" << " C++ rocks!";
}

We can print a new line character using the std::endl:

#include <iostream>
int main(){
	std::cout << "Hello World!" << std::endl;
}

Use multiple statements:

#include <iostream>
int main(){
	std::cout << "Hello" << std::endl;
	std::cout << "World" << std::endl;
	std::cout << "C++" << std::endl;
}