SyntaxPilot
Courses/C++/Lesson 1
Video coming soonThis lesson’s video is being made

Start with the notes below; the video appears here as soon as it is on YouTube.

C++ · Lesson 1 of 40

cout, endl & namespace std

scheduled

These are the notes for C++ episode 1. You will learn what C++ is and where it is used (games, browsers, trading, competitive programming), how g++ compiles your code, your first C++ program line by line, cout and the << insertion operator, what using namespace std and std:: really mean, endl vs "\n", escape sequences, how to read g++ errors, and comments. Every example below runs as-is in the SyntaxPilot compiler (choose C++ as the language).

The problem: you use C++ every day without knowing it

When every millisecond counts, people reach for C++:

In real life Built with C++
Mobile and PC games with smooth graphics and fast action game engines like Unreal Engine
Your web browser opening pages in milliseconds the core of Chrome and Firefox
Applying filters to huge photos performance-critical parts of Photoshop
Coding contests where every millisecond counts competitive programming with C++ and the STL

C++ is also a top choice for DSA and placements, which is why this course starts here.

What is C++?

  • Created by Bjarne Stroustrup, released in 1985.
  • The idea: keep C's speed and add classes (object-oriented programming), plus much more.
  • Like C, it compiles to machine code, so it is very fast.
  • Used in games, browsers, stock trading systems and competitive coding.
  • People call it C with superpowers.

How g++ compiles your code

Think of a book translator:

  1. You write human-readable C++ in main.cpp.
  2. The compiler g++ checks and translates all the code. A mistake gives a compile error, and no program is built.
  3. If all is well, you get a file of machine code (a.out), made for that specific operating system and CPU.
  4. The CPU runs it directly, with no translator in between. That is why C++ is so fast.
main.cpp  --g++-->  a.out (machine code)  --CPU-->  output

In a terminal:

g++ main.cpp
./a.out

On SyntaxPilot, just press Run. (Many people compile with g++ -std=c++17 main.cpp to choose the language version; every example here works with C++17.)

Your first C++ program

#include <iostream>
using namespace std;

int main() {
    cout << "Hello, World!" << endl;
    return 0;
}

Output:

Hello, World!

What each line means

Line Meaning Real life
#include <iostream> Brings in the input/output library; cout and cin come from it borrowing ready-made tools from a library
using namespace std; Lets you write cout instead of std::cout (explained below) calling someone by their first name
int main() { The entry point: every C++ program starts here the building's main door
cout << "Hello, World!" << endl; Puts the text on the output, then a new line a conveyor belt to the screen
return 0; Tells the operating system the program finished successfully "all done"
{ } Mark where main starts and ends the walls of a room
; Ends every statement; forget it and g++ gives an error a full stop

cout and the << insertion operator

Think of cout (character output) as a conveyor belt that carries things to the screen:

  • <<, the insertion operator, puts an item on the belt. Text or numbers, as many as you like, one after another.
  • endl (end line) is like pressing Enter: the next output starts on a new line.
  • Items arrive on the screen in the same order you put them on the belt.
#include <iostream>
using namespace std;

int main() {
    cout << "Age: " << 20 << endl;
    cout << "5 + 3 = " << 5 + 3 << endl;
    cout << "Hello " << "C++" << "!" << endl;
    return 0;
}

Output:

Age: 20
5 + 3 = 8
Hello C++!

C++ printed 8, because the maths 5 + 3 happens before the item goes on the belt. Java's "+ glues text" trap does not happen here, because << separates the items.

Watch the spaces: cout adds nothing between items, so put spaces inside the strings ("Hello "). And numbers next to each other just join: cout << 1 << 2; prints 12.

C's printf vs C++'s cout

The same output in C and C++:

#include <stdio.h>

int main() {
    printf("Age: %d\n", 20);
    return 0;
}
#include <iostream>
using namespace std;

int main() {
    cout << "Age: " << 20 << endl;
    return 0;
}

Both print:

Age: 20
  • In C you must match a format code like %d with the value.
  • In C++ there are no format codes: cout works out by itself whether an item is text or a number.
  • C's printf still works in C++ (include <cstdio>), because C++ is built on top of C.

using namespace std and std::

Say a class has three students named Rahul. If the teacher just says "Rahul", there is confusion. So the teacher uses the full name: Rahul Sharma. At home, though, everyone just says "Rahul".

At school In C++
Three students named Rahul Many libraries: the same name can exist twice
Say "Rahul Sharma": full name, no confusion std::cout: the cout from the standard library
At home, just "Rahul" using namespace std;: now just write cout

:: is the scope resolution operator: std::cout means "the cout inside namespace std". Two ways, same result:

#include <iostream>

int main() {
    std::cout << "Hi" << std::endl;
    return 0;
}
#include <iostream>
using namespace std;

int main() {
    cout << "Hi" << endl;
    return 0;
}

Both print:

Hi
  • Full names (std::cout) are what most large professional projects use, to avoid name clashes.
  • using namespace std; is shorter and perfect for learning and competitive programming, so this course uses it.

endl vs "\n"

cout << "Hi" << endl;
cout << "Hi\n";

Output:

Hi
Hi
endl "\n"
New line yes yes
Sends (flushes) the output to the screen immediately yes, like pressing Enter and Send in a chat no
Speed when printing a lot slower faster

Both look the same on screen. When printing thousands of lines (common in contests), prefer "\n".

Escape sequences

Like \n, there are more special characters, called escape sequences. They work exactly like in C:

Escape Meaning
\n new line
\t tab: lines up columns
\" a double quote inside a string
\\ a backslash itself
#include <iostream>
using namespace std;

int main() {
    cout << "Name:\tAarav" << endl;
    cout << "He said \"Hi\"" << endl;
    cout << "C:\\Users\\Aarav" << endl;
    return 0;
}

Output:

Name:   Aarav
He said "Hi"
C:\Users\Aarav

Reading g++ errors

Read error messages carefully: half the solution is often written right there.

Missing semicolon

#include <iostream>
using namespace std;

int main() {
    cout << "Hi" << endl
    return 0;
}
main.cpp:5:25: error: expected ‘;’ before ‘return’

Read it as file : line : column : problem. The ; belonged at the end of line 5; g++ noticed when it reached return.

Forgetting using namespace std

#include <iostream>

int main() {
    cout << "Hello, World!" << endl;
    return 0;
}
main.cpp:4:5: error: ‘cout’ was not declared in this scope; did you mean ‘std::cout’?

The compiler itself tells you the fix: add using namespace std; or write std::cout (and std::endl, which gets the same error).

A single less-than sign

#include <iostream>
using namespace std;

int main() {
    cout < "Hi";
    return 0;
}
main.cpp:5:10: error: no match for ‘operator<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} and ‘const char [3]’)

g++ does not understand a single < between cout and text, and points to line 5, column 10, the exact spot. This message is followed by many lines of "candidate" notes; you only need the first error line. The insertion operator always has two: <<.

Common mistakes (and the real error messages)

Mistake Code Error
Missing semicolon cout << "Hi" << endl error: expected ‘;’ before ‘return’
No using namespace std cout << "Hi"; error: ‘cout’ was not declared in this scope; did you mean ‘std::cout’?
Single < cout < "Hi"; error: no match for ‘operator<’
Arrows the wrong way cout >> "Hi"; error: no match for ‘operator>>’ (>> is for cin)
No #include <iostream> cout used without the include error: ‘cout’ was not declared in this scope with the note did you forget to ‘#include <iostream>’?
Capital C Cout << "Hi"; error: ‘Cout’ was not declared in this scope (C++ is case sensitive)
No spaces in strings cout << "Hello" << "World"; no error, prints HelloWorld

Comments: sticky notes

Comments work just like in C, and the compiler ignores them:

#include <iostream>
using namespace std;

/* Prints a greeting */
int main() {
    cout << "Hi" << endl;   // greeting
    // cout << "Hidden";
    return 0;
}

Output:

Hi
  • /* ... */ is a multi-line comment.
  • // starts a single-line comment; the commented-out cout never runs.
  • Good comments explain why.

Mini project: a profile card

#include <iostream>
using namespace std;

int main() {
    // Mini project: profile card
    cout << "==============================" << endl;
    cout << "Name:\t" << "Aarav" << endl;
    cout << "City:\t" << "Lucknow" << endl;
    cout << "Age:\t" << 21 << endl;
    cout << "Goal:\t" << "C++ Developer" << endl;
    cout << "==============================" << endl;
    return 0;
}

Output:

==============================
Name:   Aarav
City:   Lucknow
Age:    21
Goal:   C++ Developer
==============================

A chain of insertion operators: text, a tab, then the value. Note that 21 is a number, with no format code needed. Add your own details and build your own card.

Practice

  1. Build your own C++ profile card.
  2. Print a 3-row marks table using \t.
  3. Write the Hello World program using std::cout and std::endl, without the using line.
  4. Print 12 x 12 = 144 with one cout line, letting C++ do the multiplication.
  5. What does cout << 1 << 2 << endl; print? What about cout << (1 << 2) << endl;?
Solutions and hints
  1. Copy the mini project and change the values.
  2. One cout per row: cout << "Subject\tMarks" << endl;, then cout << "Maths\t90" << endl; and so on.
  3. #include <iostream>, then inside main: std::cout << "Hello, World!" << std::endl; (no using namespace std; line).
  4. cout << "12 x 12 = " << 12 * 12 << endl; prints 12 x 12 = 144.
  5. 12: two items, 1 and 2, with nothing between them. With brackets it prints 4, because inside brackets << is the bit-shift operator (1 shifted left by 2 bits = 4). That is a later topic; for now, just know that brackets change the meaning.

Quick quiz

What will this code print?

cout << "A" << "B" << endl << "C";
  • A. A B C
  • B. AB on one line, then C on the next
  • C. ABC
  • D. A, B and C on three lines
Answer

B. A and B go on the belt together with no space, endl starts a new line, and then C.

Interview questions

  • Who created C++, and how is it different from C?
  • Why is C++ faster than languages that run on a virtual machine?
  • What does #include <iostream> do?
  • What is a namespace? What does using namespace std; do, and why do large projects often avoid it?
  • What is the difference between endl and "\n"?
  • What is << called when used with cout? What does >> do with cin?
  • What is the difference between printf and cout?

Summary

  • C++ = C with superpowers: fast, compiled to machine code, used in games, browsers, trading and contests.
  • g++ compiles main.cpp into machine code that the CPU runs directly.
  • #include <iostream> brings in cout; int main() is where the program starts; return 0 means success.
  • cout << puts items on the output belt, in order, with no format codes.
  • using namespace std = calling by first name; std::cout is the full name.
  • endl = new line + flush; "\n" = just a new line, and faster.
  • Read errors as file : line : column : problem; they often suggest the fix.

Next episode: variables and C++ data types (int, double, char, bool and string) and how much memory each uses.