Hello World
Hello World

  • 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
  • preprocessor directive
    #include "filename"
    inserts the content of a file
  • filename in angle brackets ⇒ looked up 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 {}
  • each statement must be terminated with a semi-colon ;
  • this statement writes text to the console
  • std:: is the namespace of the standard library
  • cout (short for "character out") refers to the standard (console) output
  • "Hello World\n" is a string literal – a sequence of characters
  • \n is a special "new line" character
}
  • the program terminates after executing the main function
  • it will automatically return 0, if no return statement is given

  • C++ source code can't be run directly
  • C++ compiler translates source code into binary machine code that is understood by the computer hardware (CPU)
  • program that can be run = binary executable file containing machine code
$ g++ hello.cpp –o sayhello
$ ./sayhello
Hello World!
compile & link 
run executable "sayhello"
program output

Compiler Flags

Recommended compiler flags for your first programs

g++ -std=c++17 -Wall -Wextra -Wpedantic input.cpp -o output

-std=c++17 Sets compiler to the C++17 standard. Recommended.
-std=c++14 Sets compiler to the C++14 standard (also ok).
  • -Wall
  • -Wpedantic
  • -Wextra
Enables compiler warnings. Highly recommended. Doesn'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 file.
  • Compiler Error program not compilable, compiler will stop
  • Compiler Warning program compilable, compiler will continue, but there is a problematic piece of code that might lead to runtime bugs

It's 2020 – set your compiler to C++17 or at least to C++14 if you have to use an older compiler.

Terminology

  • static fixed at compile time (not changeable at runtime)
  • dynamic changeable at runtime (not fixed at compile time)