Introduction to C++ : Basic Syntax

How to write a Program in c++?

Basic Syntax:

Comments:

Pre-processor Directives:

The Main Function:

Statements:

Return statement:

 

 

Basic Syntax

 

Now to start writing a code in c++ you need to familiar with the basic syntax. When we are talking to the computer in certain language, we must be aware of the grammatical rules associated with it. (Don’t Panic if you didn’t understand all of it, read the tutorials further to get complete picture. The best way to learn coding is jump write in and experiment around. You will find each section in detail in separate tutorials)

 A very basic c++ code consists of at least the following components:

Comments: (Not necessary)

Pre-processor Directives:

The Main Function:

Statements:

Return statement:

 

 

// A code to display a welcome message.

/* This code is part of the Basic C++ tutorials Earth Scientists

For more details, please refer to the tutorial on geophysics.in */

 

#include <iostream>

 

int main()

 

{             

    std :: cout<<"Welcome to Geophysics.in";

      

    return 0;

 

} 

 

 

 

 

Comments

//  and  /*…*/  symbols in C++ are used  for  commenting.

Comments are used to give a brief description of the program. It is always handy to use comments so you can remember what this program was for

 when referring to after a long period.

// is a single-line comment

/* is a multiline comment

 ……….

 …………*/

 

// A code to display a welcome message.

/* This code is part of the Basic C++ tutorials Earth Scientists

For more details, please refer to the tutorial on geophysics.in */

 

 

Pre-processor Directives

 

#include <iostream>

 

 

 #include is a pre-processor directive.

 <iostream> refer to standard input output stream library.

In a layman’s term, #include is an instruction for the pre-processor to include the input and output library, which helps us to take input from our keyboard or display our result on the screen.  

 

 

The Main Function

 

 

int main()

 

The main function is the entry point of the program. When you execute the program main function is first to be called.

The int before the function name (main) is the function’s return type. int here is the integer datatype. In simpler terms, the datatype function will return us if the code is successfully executed.

We can also use void if we don’t want any return type.

   

 

{  //start of the function

Statement 1;

Statement 2;

return  returntype ;

}  // end of the program

 

Functions always start and end with curly braces

   

Statements

 

Statements are set of instruction that are given to the computer.

std :: cout<<"Welcome to Geophysics.in";

 

This is a statement that says the compiler to use cout (a printing or displaying function) from the standard library and display "Welcome to Geophysics.in"

The statement always terminates with a semicolon (;).

   

Return statement

 

   It returns the value when the function is called. For e.g., when you are running this program you are actually calling the main function which is integer datatype (see the ‘int’ in front of main () ). So main function is supposed to return an integer value when it is successfully executed.

return 0;

 

Return an integer 0 if the code is compiled successfully.

 

 

 

 

 

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *