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

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 …
Read More

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 …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More