C++ program structure
Preprocessor Directive
#include<iostream>
Header Files
Return type using namespace std;
int main()
main function
{
delimiters
cout<<"Hello C” <<endl;
return 0; Statement terminator
}
Output statement
Preprocess Directive
• The preprocessor directives are
commands that give instructions to
the C/C++ preprocessor , whose
job is to modify the text of a C/C++
program before it is compiled.
• A preprocessor directive begins
with a symbol # ( known as number
or channel or hash sign)
• preprocessor is a system program
that modifies a C/C++ program
prior to its compilation
Preprocess Directive
• Preprocessor directives are not
C++ statements, so they do not
end in a statement terminator(;)
• C++ has various preprocessor
directive like #include, #define ,
#undef, #ifdef, #ifndef, #endif, #if
etc. but we will focus only two of
them
#include
#define
The #include Preprocessor Directive
The preprocessor directive
#include tells the compiler
to insert header file into
your source file
SYNTAX
#include <filename>
Searches standard library for
file
Use for standard library files
#include “filename”
Searches current directory,
then standard library
Use for user-defined files
EXAMPLES:
#include <iostream>
#include <cmath>
#include directives tell the preprocessor where to
find the meanings of standard identifiers (built-in
function, objects and classes etc) used in the
program. These meanings are collected in files
called standard header files. The header file
iostream contains information about standard
input and output objects such as cin and cout.
Descriptions of common mathematical functions
are found in the header file cmath
The #define Preprocessor Directive
• #define
Preprocessor directive used to create symbolic constants
also know as macros
– Symbolic constants
• When program compiled, all occurrences of symbolic
constant replaced with replacement text
– SYNTAX
#define identifier replacement-text
– Example:
#define PI 3.14159
– Everything to right of identifier replaces text
#define PI = 3.14159
• Replaces “PI” with "= 3.14159"
– Cannot redefine symbolic constants once they have
been created
Header File
A header file is a file with extension .h which contains declarations of
C++ functions, objects and classes. But newer Standard C++ header files
don’t have a file extension like iostream
A text file containing all the information about a standard library needed
by the compiler when compiling a program that uses the facilities defined
in the standard library
User can create its own header file which may include his own defined
functions and classes but it will discussed later on…
The using directive
using keyword allow us to import an entire namespace into your program
namespace is an new feature in c++ which is not available in c language
C++ program can be divided in different namespace
namespace provide a way to group different entities like class, objects and
function under a name
namespace are used to resolve conflicts that we get when two or more
things has same name
cout statement is belong to std namespace
main ( ) function
• Functions are one of the fundamental building block of c++ and
our first program has only a main function
• In c++ function can be a part of class and it can be write stand
alone as a part of class function is called member function we
will discuss about function and classes later.
• Program execution start from the main function
• Program execution end also at main function
• For c++ program execution main function must be required
main Function Definition
cout and return Statement
The program statement is fundamental unit of c++ program
cout and return are c++ statements or instruction
cout statement tell the computer to display quoted phrase
A semicolon signals the end of the statement.
return statement tells main() to return the value 0 to whoever called it,
in this case the operating system or compiler
Delimiters
A delimiter is a unique character or series of characters that indicates the
beginning or end of a specific statement, string or function body set.
define the scope
Delimiter examples include:
Round brackets or parentheses: ( )
Curly brackets: { }
Escape sequence or comments: /*
Double quotes for defining string literals: " "
Predefined C++ Macros
C++ provides a number of predefined macros men oned below −
Sr.No Macro & Description
1 __LINE__
This contains the current line number of the program when it is being compiled.
2 __FILE__
This contains the current file name of the program when it is being compiled.
3 __DATE__
This contains a string of the form month/day/year that is the date of the translation of the
source file into object code.
4 __TIME__
This contains a string of the form hour:minute:second that is the time at which the program
was compiled.
Example of Predefined C++ Macros
#include <iostream>
using namespace std;
int main ()
{
cout << "Value of __LINE__ : " << __LINE__ << endl;
cout << "Value of __FILE__ : " << __FILE__ << endl;
cout << "Value of __DATE__ : " << __DATE__ << endl;
cout << "Value of __TIME__ : " << __TIME__ << endl;
return 0;
}