Using GNU, the compilation command is g++
followed by the file name. Here, the name of the source file is hello.cpp.
g++ hello.cpp
The execution command is ./
followed by the file name. Here, the name of the executable file is a.out.
./a.out
Single-line comments are created using two consecutive forward slashes. The compiler ignores any text after //
on the same line.
// This line will denote a comment in C++
Multi-line comments are created using /*
to begin the comment, and */
to end the comment. The compiler ignores any text in between.
/*This is all commented out.None of it is going to run!*/
The program runs line by line, from top to bottom:
iostream
. This library contains code that allows for input and output.main()
function houses all the instructions for the program.#include <iostream>int main() {std::cout << "1\n";std::cout << "2\n";std::cout << "3\n";}
std::cout
is the “character output stream” and it is used to write to the standard output. It is followed by the symbols <<
and the value to be displayed.
std::cout << "Hello World!\n";
The escape sequence \n
(backward slash and the letter n) generates a new line in a text string.
std::cout << "Hello\n";std::cout << "Hello again\n";