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.


C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …