← Back to Programming

C++ basics: types and compilation

C++ is a fast, powerful language used for games, browsers, operating systems, and embedded devices. It gives programmers close control over the machine, along with high-level features.

About C++

Bjarne Stroustrup began creating C++ at Bell Labs in 1979 as 'C with Classes', extending the C language with object-oriented features. It was first commercially released in 1985. C++ is standardised by ISO, with major versions such as C++11, C++17, C++20, and C++23, which added many modern features.

Compilation

C++ is compiled directly to machine code for a specific platform. Building a program involves the preprocessor, which handles #include and #define lines; the compiler, which turns each source file into an object file; and the linker, which combines object files and libraries into an executable. Common compilers are GCC, Clang, and Microsoft's MSVC.

A first program

#include <iostream> int main() { std::cout << "Hello\n"; return 0; }. #include brings in the input-output library; main is the entry point; std::cout writes to the console using the << operator; returning 0 signals success. The std:: prefix names the standard library's namespace.

Fundamental types

C++ has built-in types such as int, double, char, and bool. Their sizes can vary by platform; int is commonly 32 bits. Fixed-width types such as std::int32_t and std::int64_t from <cstdint> guarantee sizes. Unsigned types cannot hold negatives and wrap around on overflow, whereas signed integer overflow is undefined behaviour.

Variables and initialisation

Variables must be declared with types: int count = 0;. Brace initialisation, int count{0};, is safer because it rejects narrowing conversions such as storing 3.7 in an int. auto lets the compiler deduce types: auto x = 3.5; makes x a double. const marks values that must not change.

Undefined behaviour

Some mistakes have no defined result in C++: reading an uninitialised variable, accessing an array out of bounds, or overflowing a signed integer. The program might crash, produce wrong answers, or seem to work until it suddenly does not. Compiler warnings, such as -Wall, and sanitiser tools help catch these bugs.

Functions and headers

Functions are declared with return and parameter types: int square(int x) { return x * x; }. Large programs put declarations in header files (.h or .hpp) and definitions in source files (.cpp), so other files can #include the header and call the functions. Functions can be overloaded by parameter types, like Java methods.

Worked example

int a = 7, b = 2; std::cout << a / b << "\n"; prints 3 because integer division truncates. std::cout << static_cast<double>(a) / b; prints 3.5. static_cast is C++'s explicit, searchable way to convert types, preferred over older C-style casts such as (double)a.

Key takeaways

C++ (Stroustrup, from 1979) extends C with classes and much more, standardised as C++11 through C++23. It compiles to native machine code via preprocessor, compiler, and linker. main is the entry point; std::cout prints. Built-in types vary in size; fixed-width types guarantee it. Brace initialisation prevents narrowing; auto deduces types; const prevents changes. Undefined behaviour, such as out-of-bounds access, is dangerous. Headers hold declarations; static_cast performs explicit conversions.

Flashcard

C++

Language extending C with classes

Stroustrup, 1979 onwards.

Check your understanding

A C++ program reads past the end of an array. What does the language guarantee will happen?

← Previous lesson Next lesson →