Hello World Hello World Hello World
Source File hello.cpp
…
:
File hello.cpp
…
:
hello.cpp
hello.cpp
hello.cpp
- this line will be replaced with the content of the file
iostream
iostream
is a header file in the compiler directory that provides input and output functionalities- #include "path/to/filename" ⇒ inserts the content of a file
- #include <filename> ⇒ same, but searches for file in all include directories
- happens before compilation ⇒ compiler
does only
see
the already preprocessed file
Comments are ignored by the compiler.
// single line comment
/* C-style
multi-line comment */
- defines a function called "main"
- every program starts by executing the main function
int
is the only allowed return type for the main function (int
refers to an integer (whole) number)()
is an empty parameter list
{
- blocks of statements are enclosed in curly braces
{ … }
- statements are terminated by a semi-colon
;
- this statement writes text to the console
std
is the namespace of the standard librarycout
(short for "character out") refers to the standard (console) output"Hello World\n"
is a string literal – a sequence of characters\n
is a specialnew line/line break
character
}
- the program terminates after executing the main function
- it will automatically return
0
(indicating success) if noreturn
statement is given - return codes other than
0
are interpreted as an error by the operating system executing the program
C++ is a compiled language
- source code can't be run directly
- code is written to an abstract machine model (more on that later)
- compiler translates source code into binary machine code understood by the CPU
- program that can be run = binary executable file containing machine code
$ g++ hello.cpp -o sayhello
$ ./sayhello
Hello World!
Terminology
- static = fixed at compile time (baked into the executable file, not changeable at runtime)
- dynamic = changeable at runtime (possibly by user input)
Compiler Flags
Recommended compiler flags for your first programs
g++ -std=c++20 -Wall -Wextra -Wpedantic -Wshadow input.cpp -o output
-std=c++20 | Sets compiler to the C++20 standard. Highly Recommended. |
|
Enable compiler warnings. Highly recommended. These don't really activate all warnings, but rather the most important ones that don't produce too much (false positive) noise. |
-o <filename> | Sets the name of the output (executable) file. |
It's 2023 – set your compiler to C++20 (or at least to C++17 if you have to use an older compiler).
Don't Use using namespace std;
!
using namespace std;
Don't!
Many code examples / tutorials show something like this:
using namespace std;
int main () {
cout << "bla\n";
}
… to avoid qualifying cout
with std::
.
But:
using
a namespace drags all symbols from that namespace
into the global namespace.
This can lead to name conflicts and ambiguities and in some cases
even bugs that will only manifest at runtime and are very hard to detect.
Polluting the global namespace with all symbols from other namespaces is a serious liability in any production code base and you should avoid this anti-pattern from the very start.