C++ Basic Program structure

Program Structure

The best way to learn a programming  language is by writing a program. That s why, here is our first program:

// 1st  program in C++

#include <iostream>

using namespace std;

int main ()


cout << “This is my first program!”;

return 0;


The above section shows the source code of our first program. Output of this program after compilation and execution will be as given below:


This is my first program!

The way how to edit and compile a program depends on the compiler you are using. It depends on whether it has a Development Interface or not and on its version.

No doubt it is one of the simplest programs that can be written in C++, but it contains all the fundamental components that are essential by every C++ program.

Let’s explore this code line by line:

// 1st  program in C++

This is a comment line. Comments do not have any effect on the behaviour of the program. There are two types of comments.

  1. Single line comments
  2. Multi line comments

Single line Comments always starts with two slash signs (//). The programmer can add short explanations or descriptions within the source code itself. In this case, the line is a brief description of what our program is.

#include Lines starting with a hash sign (#) square measure preprocessor directives. These square measure indications for the compiler’s preprocessor during this 1st program example the directive #include tells the preprocessor to incorporate the iostream commonplace file. This specific file (iostream) contains the declarations of the essential commonplace input-output library in C++, and it’s enclosed as a result of its practicality goes to be used later within the program.

using namespace std; All the weather of the quality C++ library square measure declared at intervals what’s known as a namespace, the namespace with the name std. thus so as to access its practicality we have a tendency to declare with this expression.

int main () This line is that the starting of the definition of the main function. This can be the entry purpose of C++ programs to begin their execution, severally of its location at intervals the ASCII text file. The directions contained at intervals this function’s definition can forever be the primary ones to be dead in any C++ program. Therefore all C++ programs have a main function.

Main function is followed by a combine of parentheses (()). that’s as a result of it’s a function declaration: In C++, what differentiates a function declaration from alternative sorts of expressions square measure these parentheses that follow its name. Optionally, these parentheses might enclose an inventory of parameters inside them. Right once these parentheses we will realize the body of the most function penned in braces ().

cout << “This is my first program!”; This line

could be a C++ statement. In fact, this statement performs the sole action that generates an apparent result in our initial program. cout represents the quality output stream in C++, and also the that means of the complete statement is to insert a sequence of characters (in this case the This is my first program!! is the sequence of characters) into the quality output stream (which typically is that the screen). cout is asserted within the iostream customary file inside the std namespace, therefore that is why we wanted to incorporate that specific file and to declare that we tend to were attending to use this specific namespace earlier in our code. Notice that the statement ends with a punctuation character (;). This character is employed to mark the top of the statement and if truth be told it should be enclosed at the top of all expression statements altogether C++ programs (one of the foremost common syntax errors is so to forget to incorporate some punctuation once a statement).

return 0; The return statement causes the most perform to end. return could also be followed by a  return code (in our example is followed by the return code 0). A come back code of zero for the most perform is usually taken because the program worked of course with none errors throughout its execution. This is often the foremost usual thanks to finish a C++ console program.

The program has been structured in numerous lines so as to be additional legible, but in C++, we tend to don’t have strict rules on the way to separate directions in numerous lines. for instance, instead of

int main () { cout << ” Hello World!”; return 0; } We could have written: int main () { cout << ” This is my first program!”; return 0; }

PDF books and articles are here.

C++ Compilers

C++ Compilers

The fundamental instruments expected to take after these instructional exercises are a PC and a compiler toolchain ready to assemble C++ code and construct the projects to keep running on it.

C++ is a dialect that has advanced significantly finished the years, and these instructional exercises clarify numerous highlights added as of late to the dialect. Along these lines, with a specific end goal to appropriately take after the instructional exercises, an ongoing compiler is required. It will bolster (regardless of whether just mostly) the highlights presented by the 2011 standard.

Numerous compiler merchants bolster the new highlights at various degrees. See the base of this page for a few compilers that are known to help the highlights required. Some of them are free!

On the off chance that for reasons unknown, you have to utilize some more seasoned compiler, you can get to a more established adaptation of these instructional exercises here (never again refreshed).

How to declare variables?

How to declare variables?

C++ could be a strongly-typed language, and needs each variable to be declared with its sort before its 1st use. This informs the compiler the scale to order in memory for the variable and the way to interpret its price. The syntax to declare a brand new variable in C++ is straightforward: we have a tendency to merely write the kind followed by the variable name (i.e., its identifier). For example:

int a;
float mynumber;

These are two valid declarations of variables. The first one declares a variable of type int with the identifier a. The second one declares a variable of type float with the identifier my number. Once declared, the variables a and my number can be used within the rest of their scope in the program.
If declaring more than one variable of the same type, they can all be declared in a single statement by separating their identifiers with commas. For example:

int a, s, c;

This declares three variables a, s and c, all of them of type int, and has exactly the same meaning as:

int a;
int s;
int c;

To see what variable declarations look like in action within a program, let’s have a look at the entire C++ code of the example about your mental memory proposed at the beginning of this

#include <iostream>
using namespace std;
int main ()
// declaring following integer variables:
int a, s;
int result;

// initialize variables:
a = 5;
s = 2;
a = a + 1;

// processing to compute result
result = a - s;

// displaying results
cout << result;

// As the return type of main function is int i have to return some integer to terminate this program:
return 0;

The C++ language defines many headers, that contain info that’s either necessary or helpful to your program. For this program, the header <iostream> is required.

The line using namespace std; tells the compiler to use the std namespace. Namespaces area unit a comparatively recent addition to C++.

The next line ‘// main() is wherever program execution begins.’ may be a single-line comment out there in C++. Single-line comments begin with // and stop at the top of the road.

The line int main() is that the main function start wherever program execution begins.

The next line cout << “Hello World”; causes the message “Hello World” to be displayed on the screen.

The next line return zero; terminates main( )function and causes it to return 0 to the line method.

Don’t be worried if something else than the variable declarations themselves look a bit strange to you. Most of it will be explained in more detail in coming articles.


A Simple C Program: Printing a Line of Text

A Simple C Program: Printing a Line of Text

C uses some notations that may appear strange to people who have not programmed computers.
I begin by considering a simple C program. First example prints a line of text. The program and its screen output are shown

1 // Fig. 2.1: fig02_01.c
2 // A first program in C.
3 #include <stdio.h>
5 // function main begins program execution
6 int main( void )
7 {
8 printf( "Welcome to Programming World!\n" );
9 } // end function main


Welcome to Programming World!

Even though this program is simple, it illustrates several important features of the C language. Lines 1 and 2

#include Preprocessor Directive Line 3 is a directive to the C preprocessor. Lines beginning with # are processed by the preprocessor before compilation. Line 3 tells the preprocessor to include the contents of the standard input/output header () in the program.

Computers Data, Control of sequences and Softwares

Computers Data, Control of sequences and Softwares

C is a concise yet powerful computer programming language that’s appropriate for technically oriented people with little or no programming experience and for experienced programmers to use in building substantial software systems.

you give the command to compile the program. The compiler translates the
C program into machine language-code (also referred to as object code). In a C system, preprocessor program executes automatically before the compiler’s translation phase begins.The C preprocessor obeys special commands called preprocessor directives, which indicate that certain manipulations are to be performed on the program before compilation. These manipulations usually consist of including other files in the file to be compiled and performing various text replacements.


The instructions you write to command computers to perform actions and make decisions which controls computers (often referred to as hardware). Some examples of Computer Systems i.e

Loader puts program in memory.
CPU takes each
instruction and
executes it, possibly
storing new data
values as the program

Electronic health
These might include a patient’s medical history, prescriptions, immunizations,
lab results, allergies, insurance information and more. Making
this information available to health care providers across a secure network
improves patient care, reduces the probability of error and
increases overall efficiency of the health care system.

Human Genome
The Human Genome Project was founded to identify and analyze the
20,000+ genes in human DNA. The project used computer programs
to analyze complex genetic data, determine the sequences of the billions
of chemical base pairs that make up human DNA and store the
information in databases which have been made available over the
Internet to researchers in many fields.

A billion general-purpose computers, and billions more embedded
computers are used in cell phones, smartphones, tablet computers, home appliances,
automobiles and more. Computers can perform computations and make logical decisions phenomenally faster than human beings can. Many of today’s personal computers can perform billions of calculations in one second.

Computers process data under the control of sequences of instructions called computer programs. These programs guide the computer through ordered actions specified by people called computer programmers. The programs that run on a computer are referred to as software.