marp | theme | footer |
---|---|---|
true |
custom-theme |
Hello world program written in hello.cpp
file just prints Hello World!
to the terminal:
#include <stdio.h>
int main() {
// We are trying to be friendly here!
puts("Hello World!");
return 0;
}
If we compile and run it we get the following:
λ › c++ -std=c++17 -o hello hello.cpp
λ › ./hello
Hello World!
- Some keywords like
return
orint
(and many more!) - Constants like
"Hello World!"
and0
- Lots of whitespaces!
- Different brackets:
{}
,()
,<>
- Signs with special meaning:
#
,;
,//
- Includes like
#include <stdio.h>
- Functions like
puts
andmain
📺 Watch the related YouTube video!
- In the code above, some words and symbols are highlighted
- This is done by parsing the language into meaningful parts
- Whitespaces and symbols separate these parts
- Some highlighted words are built-in and always the same:
return
always marks an exit point from a functionint
always represents an "integer number" type- See all C++ keywords at
cppreference.com
- Things like
"Hello World!"
and0
are literal constants - Other change their meaning depending on context, e.g.
puts
can appear elsewhere in the code and mean a different thing - We will see many more of all of these later
If not inside of string constants like "Hello World!"
And not splitting key words or names of entities 🙌
#include <stdio.h>
int main() {
puts( "Hello 🌍!" ) ;
return 0;
}
#include <stdio.h>
int main(){puts("Hello 🌍!");
return 0;}
#include <stdio.h>
int main()
{puts(
"Hello 🌍!"
);
return 0;
}
#include <stdio.h>
int main() { puts
(
"Hello 🌍!");
return
0
;}
- 🤬 People tend to argue (a lot!) about how to style their code
- 💡 Avoid long discussions by using a tool
clang-format
!
λ › clang-format -i hello.cpp
This rewrites the original hello.cpp
file with the styled version of itself
Just put a .clang-format
file into a directory up the tree from your source files, here are the possible settings
- 🤔 Different people will have different opinions here
- ✅ This is the
.clang-format
file I use for my projects:
---
Language: Cpp
BasedOnStyle: Google
AllowShortBlocksOnASingleLine: true
AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: All
AllowShortLoopsOnASingleLine: true
AllowShortIfStatementsOnASingleLine: true
BinPackArguments: false
BinPackParameters: false
...
- 🤝 The most important thing is to have a format file in the project, regardless of which style it enforces
{}
are mostly used to:- Initialize variables
- Define "scopes"
[stay tuned!]
()
are mostly used for:- Arithmetic operations
- For writing and calling functions
- Sometimes for initializing variables
[older style]
<>
are mostly used in:- Comparison operations
- Includes
[stay tuned!]
- Templates
[later in the course]
- In the C++ world, a line that starts with a
#
marks a preprocessor directive, e.g.#include
- Preprocessor is just a text replacement tool
- It is the first step of the full compilation process:
%%{init: {'theme':'neutral', 'themeVariables': { 'fontSize': '30pt'}}}%% graph LR Preprocessor --> Compilation --> Assembly --> Linker
- We'll talk about the other steps later
- An
#include
is just a text substitution - Let's compile
hello.cpp
with a flag--save-temps
:λ › c++ -std=c++17 -o hello --save-temps hello.cpp
- This keeps a bunch of files, we care about
hello.ii
:λ › tail -n 4 hello.ii int main() { puts("Hello 🌍!"); return 0; }
- This file ends with our program
- Above our code there is the contents of the
stdio.h
file
- There are two ways to use
#include
(just a convention):#include "file"
- searches for thefile
first in the folder where the current source file lives#include <file>
- searches for thefile
first in the include path[stay tuned]
#include <file>
and#include "file"
simply replace themselves with the content of thefile
file
can be any file with any extension, but usually has extension.h
or.hpp
- There are multiple types of statements in C++
- We clearly can't rely on whitespaces to separate them
- So most (but not all!) statements require a
;
at the end - Helps separate one statements from another
- We will learn which statements need a
;
in the future - For now just prepare to type
;
a lot! 😉
- Comments are statements that are relevant only to us
- Compiler completely ignores them
- Symbol
//
comments out the whole line - Symbols
/*
and*/
comment out everything between them - Let's add comments to our example from before:
int main() { // This is some important comment! return /* another comment here */ 0; }
- Compiling with
--save-temps
yieldshello.ii
:int main() { return 0; }
- Functions help encapsulate and decouple functionality
- In the example above,
puts
is a function that prints a stringHello World!
to screen - A function like
puts
can easily be 100s of lines long main
is a function too! And a special one at that 🌈🦄- Exists in every C++ program - it's where the program starts!
- We can (and will!) write lots of our own functions
- Let's write one right now!
- This is not the most useful function in the world!
- We also won't talk about a strict definition of what is a function here, we will cover it later
- We aim at building intuition here!
#include <stdio.h>
void PrintSmth() {
puts("Smth");
}
int main() {
PrintSmth(); // This is how to call it!
return 0;
}
- The function just calls
puts
with the text"Smth"
- But it is our own function!
- And we can call it from the
main
function - We have a new keyword here:
void
void
stands for "nothing"- In our case it means that the function returns nothing