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.py and simply run it with python hello.py
  • However for C++, we’ve 1 more step: compiling!
    • Create the file hello.cpp
    • Compile: g++ hello.cpp -o hello
    • Run: ./hello

Getting started with “Hello World”

You would notice it’s less intuitive compared to Python, but nonetheless similar.

6 things to note here.

  1. We use // for comments
  2. 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
  3. 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
  4. :: is the scope resolution operator
    • It tells the compiler to look for the identifier we want in the namespace (directory of identifiers)
  5. cout <<: is the syntax for outputting text
    • Intuitively, anything after <<: flows to cout that prints!
    • On the other hand, we can get user’s input with cin >>:, see how the data flows in the opposite direction?
  6. 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

Getting input

Tags: cpp