Basics Structure of C++ Technical Elite
Basics structure of C++ program
The formats of writing program in C++ is called its structure. the basics structure of C++ program is very flexible .A program in C++ consists of the following parts
- Preprocessor directive
- Main() function
- Program body
Preprocessor directive
preprocessor directive are instruction given to the preprocessor. The preprocessor directives are processed by a program known as preprocessor. It is part of C++ compiler .It modifies C++ source program before compilation. The semicolon is not used at the end of preprocessor directives .The preprocessor directives start with hash symbol # These directives are written at the start of program
Header file
Because this line starts with a #, it is called a preprocessor directive.
The preprocessor reads your program before it is compiled and only executes those lines beginning with a # symbol. Think of the preprocessor as a program that “sets up” your source code for the compiler. The #include directive causes the preprocessor to include the contents of another file in the program. The word inside the brackets, iostream, is the name of the file that is to be included. The iostream file contains code that allows a C++ program to display output on the screen and read input from the keyboard. Because this program uses cout to display screen output, the iostream file must be included. The contents of the iostream file are included in the program at the point the #include statement appears. The iostream file is called a header file, so it should be included at the head, or top, of the program
Syntax
The syntax of using the header file is as follows
#include<header_file_name>
The name of header file can also be include in double quotes as follows;
#include"header_file_name"
Example
#include<iostream>
using namespace std;
Programs usually contain several items with unique names. In this chapter you will learn
to create variables. Variables, functions, and objects are examples of program entities
that must have names. C++ uses namespaces to organize the names of program entities.
The statement using namespace std; declares that the program will be accessing entities
whose names are part of the namespace called std. (Yes, even namespaces have names.)
The reason the program needs access to the std namespace is because every name created
by the iostream file is part of that namespace. In order for a program to use the entities
in iostream, it must have access to the std namespace.
int main()
This marks the beginning of a function. A function can be thought of as a group of one or
more programming statements that collectively has a name. The name of this function is
main, and the set of parentheses that follows the name indicate that it is a function. The word int stands for “integer.” It indicates that the function sends an integer value back to
the operating system when it is finished executing.
Although most C++ programs have more than one function, every C++ program must have a
function called main. It is the starting point of the program. If you are ever reading someone
else’s C++ program and want to find where it starts, just look for the function named main.
return 0;
This sends the integer value 0 back to the operating system upon the program’s completion.
The value 0 usually indicates that a program executed successfully.
Program
// A simple C++ program
#include <iostream>
using namespace std;
int main()
{
cout << "Programming is great fun!";
return 0;
}
Out put
Programming is great fun!
Comments
Post a Comment