About  Contact


Let us write first C++ Program

Start up
Techinically a program is a collection of programing statement written in proper sequence to do a specific task. Actually in lay man's word it nothing but fitting the steps involved in doing a task into the programing language wrapper. e.g. suppose I want to write a letter in a language which is not my mother longue, therefore first I will think the content in my mother tongue then by following the grammatical rules I will write the letter in the other language. Same way while writing a computer program for a task, we have to think how we can do it without a program then the steps to be expressed in the computer language form follwing all the syntax, and that is a program.
Before we write first program we must know few basic commands.
The two most important basic commands
(1) cin
(2) cout
The first one cin (pronounced as c-in)is called input command and the other one cout (pronounced as c-out) is called out put command. Both of them are defined in iostream.h header file. Simply if we want that computer to receive data from us then "cin" is used and when we want to display something on the screen the "cout" is used.

Syntax: cin>>variable; or cin>>var1>>var2; (use first form initially)
Syntax: cout<<variable; or cout<<variable1<<variable2;
Note that in case of "cin" two greater than sign then the variable. In case of "cout" two less than sign then the variable and the terminator semicolon.

One last thing we must know before we write out first program and that is, if we want to display any message then it must be included in opening and closing quotes("Message"). The message will display exactly it is given in the program, I mean upper case, lower case, mis-spelt etc.
Now so far the first program is concerned it is always displays the message "Hello C++" let us say hello to C++ and write a program to display the message on the screen.

Requirement to write the program
We have to use "cout" command so we have to include "iostream.h", since we are displaying a fixed message we do not need any variable
#include <iostream.h>
void main(){
	cout<<"Hello C++ !!";    
}
In Dev C++ the program would be as below. Note how iostream is included
#include<iostream>
using namespace std;
int main(){
	cout<<"Hello C++ !!";
}         
 
Now compile the program. While compiling compiler may point out some errors like missing semicolon or brackets ect., correct them and recompile the program. When the program is completely compiled then run it you will get the output in Dev C++ as below. See also in Dev C++ the header file <iostream> it does not have the extension "h"
hello cpp

Compiler
As we know that computer is an electronic device, so it understands only voltage and current. Therefore the program we write must be translated in the form that computer understands and it is done by compiler, it converts the high level language program into low lavel also called machine level( 0's and 1's form ).
So we have developed our first program. In the next topics we would write few more programs.

Use of C
There are many institutions use C instead of C++. Practically there is nothing in C what can not be done in C++. In this site C++ is discussed, still those want to write/ practice the programs discussed here in C, can do that by flowing the steps given below-
  • Incluse header file stdio.h. In this header file the famous two C statements printf and scanf are defined.
  • "printf" is output command and is used to display information on the screen/ monitor which is same as "cout" in C++
  • "scanf" is input command and is used to give data to the computer, which is same as "cin" in C++.
  • Syntax of printf - printf("message")- to display the message; printf("sum=%d", s)- here the output is "sum=10" assuming value of the variable "s" is 10.
  • Syntax of scanf - scanf("%d",&a) - to the user entered value in the variable "a"
  • Use the conversion specifiers - "%d"- for integer; "%f" - for float; "%e" - for scientific notation of a floating point number (used mainly with printf ); "%c" - for char; "%s" - for string (word) in both "printf" and "scanf" statement
The first program in C
#include<stdio.h>
void main(){
	printf("Hello C++ !!");
}         
 



Disclaimer: We have taken otmost care to keep the information error free. The site is not responsible for any kind of damage of whatsoever nature.
©Copy right reserved