C++ Basic Syntax3 min read

This section is important, before diving into the main content of the C++ program, you need to learn the basic syntax of C++. You need to know in which manner C++ code is written. C++ is the collection of  classes and objects that communicate with each other via. method.

C++ Program Structure

Let us start by looking at the simple hello world program in C++.

// Header file for input/output: The line starting with // are the comment lines that are not included during execution but these are for the programmer to comment on the necessary section as required. It is considered a good practice to use comments. For multiple comment lines, you start with /* and end with */ and write in between.

#include<iostream>: This is the header files used for executing the standard input and output function in C++. There are several header files that are very useful for the program. The line starting with # are directives that are processed by the preprocessor.

Some of the header files used in C++:

<iostream>Contains C++ standard input and output functions
<cmath>Contains math library functions
<ctime>Contains function for manipulating the time and date
<fstream>Contains function for functions that perform input from files on disk and output to files on disk

using namespace std;: This line tells the compiler to use a group of functions that are part of the standard library (std). A semi-colon ‘;’ is used to end a statement.

int main(): It is the main function of the program from where the execution of the program begins. int is a type that shows that the function returns integer type. If you don’t want to return anything then simply use the void instead of int.

{ ..... }: Opening and Closing braces shows the beginning and the ending of the main function in a program.

cout<<"Hello World";: This line is responsible for displaying the Hello World on the screen. cout is the standard out function present in <iostream> header file.

return 0; : This statement in a program is used to return a value from the function and also indicating the end of the function. You can usually use it to return some result of the calculation and such.


Compilation and Execution of C++ Program

There are steps to compile and run the C++ program n your computer. You need to save, compile and run, follow the below steps.

  • First, open a text editor(eg notepad) on your computer and write your C++ code.
  • Now save the file as file_name.cpp. Example: hello.cpp.
  • Then open a command prompt and go to the directory where you have saved the file.
  • And on the command prompt type 'g++ hello.cpp‘ and press enter to compile your code. And if no error is found then the command prompt will take you to the next line.
  • Now, type ‘a.out‘ to run your program.
  • Then Hello world will be printed on the screen (considering the above code).

It will look something like this:

Always make sure that you are in the same directory where you have saved the file.


MORE

C++ Structures

In the C/C++ programming language, the structure is user-defined data types that allow us to store the combination of different data types together. All the …
Read More

C++ Static Keyword

Static s a keyword in C++ that is when used with different types gives different meanings. static keyword can be used with variables in a …
Read More

C++ Templates

Templates in C++ are the powerful feature that allows us to write generic programs (generic classes and generic functions). A single class or function is …
Read More

C++ Friend Function

There are private and protected members in a class that is inaccessible from outside of the class, constituting one of the concepts of object-oriented programming …
Read More

this Pointer in C++

In C++, this is a keyword that refers to the current instance of a class and this pointer holds the address or points to the …
Read More

Inheritance in C++

Inheritance is one of the most important concepts of object-oriented programming. In C++, inheritance is the process of deriving the properties and behaviors of one …
Read More