STRUCTURE OF C++ PROGRAM
#include<header file>
main ()
{
...........
...........
...........
}
A C++ program starts with function called main ( ). The body of the
function is enclosed between curly braces. The program statements are
written within the braces. Each statement must end by a
semicolon;(statement terminator).
A C++ program may contain as many functions as required. However, when
the program is loaded in the memory, the control is handed over to
function main ( ) and it is the first function to be executed.// This is my first program is C++
/* this program will illustrate different components of
a simple program in C++ */
# include <iostream.h>
int main ( )
{
cout <<"Hello World!";
return 0;
}
When the above program is compiled, linked and executed, the following output is displayed on the VDU screen.Hello World!
Various components of this program are discussed below:
Comments
First three lines of the above program are comments and are ignored by the compiler. Comments are included in a program to make it more readable. If a comment is short and can be accommodated in a single line, then it is started with double slash sequence in the first line of the program. However, if there are multiple lines in a comment, it is enclosed between the two symbols /* and */#include <iostream.h>
The line in the above program that start with # symbol are called directives and are instructions to the compiler. The word include with '#' tells the compiler to include the file iostream.h into the file of the above program. File iostream.h is a header file needed for input/ output requirements of the program. Therefore, this file has been included at the top of the program.int main ( )
The word main is a function name. The brackets ( ) with main tells that main ( ) is a function. The word int before main ( ) indicates that integer value is being returned by the function main (). When program is loaded in the memory, the control is handed over to function main ( ) and it is the first function to be executed.Curly bracket and body of the function main ( )
A C++ program starts with function called main(). The body of the function is enclosed between curly braces. The program statements are written within the brackets. Each statement must end by a semicolon, without which an error message in generated.cout<<"Hello World!";
This statement prints our "Hello World!" message on the screen. cout understands that anything sent to it via the << operator should be printed on the screen.return 0;
This is a new type of statement, called a return statement. When a program finishes running, it sends a value to the operating system. This particular return statement returns the value of 0 to the operating system, which means “everything went okay!”./*
This program illustrates how to
declare variable, read data and display data. */
#include <iostream.h>
int main()
{
int rollno; //declare the variable rollno of type int
float marks; //declare the variable marks of type float
cout << "Enter roll number and marks :";
cin >> rollno >> marks; //store data into variable rollno & marks
cout << "Rollno: " << rollno<<"\n";
cout << "Marks: " << marks;
return 0;
}
Sample Run: In this sample run, the user input is shaded.Enter roll number and marks :102 87.5
Rollno: 102
Marks: 87.5
No comments:
Post a Comment