Basic Syntax:

helloworld.cpp
#include <iostream>
using namespace std;
int main(){
    cout<<"Hello World!";
    return 0;
}

Note: This is the base of C++ code. We have to write this code everytime we are writing C++ program.


Now we will understand the example from the previous chapter.

helloworld.cpp
#include <iostream>
using namespace std;
int main(){
    cout<<"Hello World!";
    return 0;
}


Description of Code:

Line 1 : iostream is a library in C++ that provides input and output functionality to interact with the console or other input/output devices.
Line 2 : using namespace std; means that we can use names for variables and objects from the standard library.
Line 3 : int main() this is the starting point for the execution of your program. Any code written inside the curly brackets will be executed.
Line 4 : cout < < "Hello World!" - it will print Hello World!
Line 5 : return 0; execution of main function stops here. This is the last line of the main function which is executed. Everything written after it is not executed.

Components:

Basically, there are three components of C code there:
  1. Header Files provides functionality for many functions.
  2. Main Function is the entry poiny of the code.
  3. Return Statement is the exit point of the code.

Note : This partitioning has no technical basis. This is done just for the purpose for understanding the code easily for new developers.


What does return 0 mean:

Whenever we write a function in C++, it returns some value. The type of value is the return type of the function (can be a number, text etc.). In our case, return type of our function is int as you can see. That is why it is returning 0. If the function has return type void, it will not return anything.

Some important points regarding syntax of C:

  • You need to put a semicolon (;) after every line of code or more specifically after every instruction.
  • C++ compiler ignores white spaces. You can write all the code in one line but multiple lines make the code readable.
  • C++ is case sensitive language. E.g. John is not same as john.
  • If you have any confusion, you can read this tutorial again and do not worry about the header files. If you do not understand them, just ignore and move ahead. You will understand them later on.