C++ First Program, Hello World Example

Introduction

The following program is the simplest "Hello World" example in C++.

It prints out Hello World. in the console window:

#include <iostream> 

int main() //from   w  w w  .j  a  v  a2 s. c om
{ 
    std::cout << "Hello World."; 
} 

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

#include <iostream> 

int main() // ww w.ja  v a  2s .c om
{ 
    std::cout << "Some string." << " Another string."; 
} 

To output on a new line, we need to output a new-line character \n literal.

The characters are enclosed in single quotes '\n'.

Example:

#include <iostream> 

int main() //w  w  w .j a v a2  s .  c o  m
{ 
    std::cout << "First line" << '\n' << "Second line."; 
} 

The \ represents an escape sequence, a mechanism to output certain special characters such as new-line character '\n', single quote character '\'' or a double quote character '\"'.

Characters can also be part of the single string literal:

#include <iostream> 

int main() /*ww w .ja  va  2 s .c  o m*/
{ 
    std::cout << "First line\nSecond line."; 
} 



Next

Related