This is a basic introduction where we learn how to output Hello World! and see the differences between Python and C++
Main difference in running C++ and Python
- So if you are familiar with python, you know we can easily create a file called
hello.pyand simply run it withpython hello.py - However for C++, we’ve 1 more step: compiling!
- Create the file
hello.cpp - Compile:
g++ hello.cpp -o hello - Run:
./hello
- Create the file
Getting started with “Hello World”
You would notice it’s less intuitive compared to Python, but nonetheless similar.
6 things to note here.
- We use
//for comments - Lines beginning with
#are preprocessor commands#include: tells preprocessor to dump contents in another file<iostream>: file we’re dumping into that defines our IO
int main()defines the code when the program starts{}: multiple commands in a block- Think of this as similar to defining a function in Python
::is the scope resolution operator- It tells the compiler to look for the identifier we want in the namespace (directory of identifiers)
cout <<:is the syntax for outputting text- Intuitively, anything after
<<:flows tocoutthat prints! - On the other hand, we can get user’s input with
cin >>:, see how the data flows in the opposite direction?
- Intuitively, anything after
- In the
{}block, you must always use a;after every line.- This is a common mistake I made when I first started out.
Variables
We can test the file by running where variable.cpp is the file we saved our above code into:
g++ variable.cpp -o variable
./variable