HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to C++VariablesData TypesOperatorsStringsVectors (vector)Conditions (if / switch)Loops (for / while)FunctionsClasses (OOP)
REFERENCE
📖 Overview
🖥️ Output & Input
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array & Vector
🔁 Condition & Loop
🧩 Function & Class
C++›Introduction to C++

Introduction to C++

First step into C++, the language of power and performance: program structure, printing with cout, and variables.

Simple enough for a 5-year-old to understand

Structure of a C++ program

Libraries are included with #include, main() runs, and output to the screen is done with cout <<.

Your first program
1
#include <iostream>
Input/output library.
ImportInput/output library.
2
using namespace std;
To use cout without writing std::.
ImportTo use cout without writing std::.
3
int main() {
Program entry point.
FunctionProgram entry point.
4
cout << "Hello!" << endl;
Prints; endl moves to the next line.
Output / InputPrints; endl moves to the next line.
5
return 0;
6
}
ImportFunctionOutput / Input
💡 Hint: You can chain the << operator: cout << a << " " << b << endl;

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Print exactly Hello World to the screen.

🎯 Expected output: Hello World

Loading editor…

About this topic

C++ was started by Bjarne Stroustrup in the 1980s by adding classes to C. The goal was to make organizing large programs easier without sacrificing C's speed. Today, C++ is the language of game engines, browsers, databases, and financial transaction systems.

C++'s defining principle is this: you don't pay for features you don't use. Object orientation, templates, and exceptions exist, but none of them are mandatory. That is why C++ can be used in both multi-layered enterprise software and microcontrollers.

The price of this flexibility is the size of the language. C++ is probably the largest widely used programming language, and no one knows the whole thing. In practice, everyone learns the subset they need; for beginners, this subset is quite small.

What you will learn

  • include and using namespace declarations
  • printing to the screen with cout
  • the structure of the main function
  • the main differences between C++ and C
  • Compilation and execution steps

Who is this for?

  • Those who want game development or graphics programming
  • Those who will work in areas where performance is critical
  • Those who know C and want to move to object orientation

When to use?

C++ is used where you want both closeness to hardware and abstraction: game engines, audio processing, image processing, high-frequency trading systems.

If you are writing a new web service or script, C++ is usually the wrong tool; development speed and memory safety are more important.

Common mistakes

Wrong

#include <iostream>

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

cout is inside the std namespace. Using it without qualification gives the error "'cout' was not declared in this scope". You either need to write the std qualification or add a using declaration. The error message in C++ can be very long; the first line is usually the most informative.

Correct

#include <iostream>

int main() {
    std::cout << "Hello" << std::endl;
    return 0;
}

Wrong

#include <iostream>
using namespace std;

int main() {
    cout << "Sum: " << 5 + 3 << endl
    return 0;
}

There is no semicolon at the end of the cout line, and the compiler reports the error on the next line: "expected ';' before 'return'". For long lines written with stream operators, forgetting the semicolon is very common because the line is already full of operators.

Correct

#include <iostream>
using namespace std;

int main() {
    cout << "Sum: " << 5 + 3 << endl;
    return 0;
}

Wrong

#include <iostream>
using namespace std;

int main() {
    cout << "Hello" << endl;
}

This code actually works: the C++ standard makes the return 0 line optional for main and the compiler adds it automatically. However, this does not apply to any function other than main, and writing it explicitly makes the intent clear. Its absence is not an error, but a stylistic issue.

Correct

#include <iostream>
using namespace std;

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

Frequently asked questions

Is writing using namespace std bad?

It is fine in small files, not recommended in large projects. The std namespace contains a large number of common names and can conflict with your own names; conflict error messages are also among the hardest to understand. And it should definitely not be written in header files.

Is there a difference between endl and the newline character?

endl adds a newline and also flushes the buffer; this second operation is slow. If you are printing many lines inside a loop, using the newline character makes a noticeable speed difference. Unless the output needs to be seen immediately, there is no need to use endl.

Can I compile C code with a C++ compiler?

Most of the time yes, but not always. C++ is stricter in some places: it does not allow implicit conversion from a void pointer and adds new keywords. If you have a variable named new or class in C, the C++ compiler will give an error.

Summary

cout is in the std namespace; you need to qualify it or write a using declaration.

Every statement ends with a semicolon; the error is reported on the next line.

endl flushes the buffer, so it is slow inside a loop.

C++ is close to a superset of C, but not 100% compatible.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code