Chapter 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 43

CS-308:Programming Fundamentals

Dr. Sumaira Nishat


Chapter 2
Basic Structure of C++ Program
• The format of writing program in C++ is called its structure.
• It consists of the following parts:
• Preprocessor directive
• Main () Function
• Program body (C++ statements)
Preprocessor Directive
• The preprocessor is a unique feature of C++.
• In C++, we have steps like compilation, linking and execution for a
typical program.
• In reality, we have a lot of other features in a C++ program that needs
to be processed before passing the program for compilation.
• For this purpose, a special step called preprocessing is carried out.
• Preprocessing is carried out before the compilation process and the
special features are preprocessed.
• As a result, an expanded C++ program is obtained and then it is
passed to the compiler.
Preprocessor Directive
• The special features for preprocessing are identified using an entity called
“Preprocessor directive”.
• These preprocessor directives tell the compiler that certain information in
the C++ program marked with preprocessor directives need to be
preprocessed before compilation.
• Note that in C++ all preprocessor directives begin with a “#” symbol.
• The moment the preprocessor encounters the # symbol, the information
following the # symbol is preprocessed before passing the program to the
compiler.
• Unlike the other C++ statements, preprocessor directives do not end with a
semicolon.
Preprocessor Directive – include Preprocessor
• Include preprocessor is used to include header files in the program.
• The syntax of using this directive is as follows:
#include <filename>
• Example:
#include <iostream>
• This statement tells the compiler to include the file iostream.h in
source program before compiling it.
• The header iostream contains the functions required for input/output
data streaming like cout, cin, etc.
Header Files
• Header files are collection of standard library functions to perform
different functions.
• There are many header files for different purposes.
• Each header file contain different types of predefined functions.
• Many header files can be included in one program.
• The header file must be included in the program before calling any of
its function in the program.
• We can include header files in our program using the following syntax.
#include <filename.h>
Example:#include <math.h>
• The extension of header file is .h.
Main () Function
• The main function is the starting point of a C++ program.
• When the program is run, the control enters main() Function and
starts executing its statement.
• Each program must contain main() Function.
• If a program does not contain main function, it can be compiled but
cannot be executed.
• Any number of statements can be written in the body of the main()
function.
• The syntax of the main() function is as follows:
Void main()
{
Body of main function
{
C++ Statements
• A statement in C++ language is an instruction for the computer to
perform a task.
• Computer performs these instructions one by one in the same
sequence in which these instructions are written.
• The statements of the program are written in curly braces {}.
{ Opening Brace.

} Closing Brace.
• The braces are also known as delimiters.
• These statements are collectively called as body of the program.
• Each statement in C++ is terminated by semicolon.
Example .cpp File Extension

Preprocessor Directive

Main Function

C++ instruction
C++ Output - Using cout
• The cout object is used to print a message on the screen.
• The message may consist of strings and values.
• The cout object is used with insertion operator <<.
• Anything written after the insertion operator is displayed on the
screen.
• Example:
cout<<"Hello World!";
cout<<3+5;
C++ Program Development and Execution
• Editing: The process of writing C++ program is known as editing.
• It includes writing , modifying and deleting program statements.
• Saving: The process of storing the program on disk is known as saving.
• A program should be saved on disk to be used repeatedly.
• C++ Programs are savedwith .cpp extension. E.g. hello.cpp
• Compiling: Process of converting source program to object program.
• Compiler do this conversion.
• Linking: Process of linking library files with object program.
• Linker combines the object program with the library files.
• Executing: Process of running an executable file.
• The program is loaded into the memory to execute it.
• The program that places the executable file in the memory is known as loader.
Steps for C++ Program Development and Execution
Debugging
• An error in a computer program is known as bug.
• The process of finding and removing bugs is known as debugging.
• Type of errors
• Syntax Errors
• Logical Errors
• Run-time errors
Syntax Errors
• Syntax : a collection of rules for writing programs in a programming
language.
• All statements in a program are written according to these rules.
• Syntax error is a type of error that occurs when an invalid statement is
written in program.
• The compiler detects syntax errors and display error message to describe
the cause of error.
• A program containing syntax errors cannot be compiled successfully.
• There can be many causes of syntax errors. E.g.
• The statement terminator is missing at the end of statement.
• A misspelled keyword.
• Any of the delimiters is missing.
Logical Errors
• A typical error that occurs due to poor logic of the programmer is
called logical error.
• A statement with logical error may produce unexpected and wrong
results in the program.
• Logical errors are difficult to find because translator cannot detect
these errors.
• These errros can be deected by examing the program thoroughly.
• Different causes of logical errors are as follows:
• Wrong conditions in program such as writing a <5 instead of >5.
• Wrong formula e.g. Aerage = Total * 5 instead of total/5.
Run-Time Errors
• A runtime error in a program is an error that occurs while the
program is running/executing after being successfully compiled.
• The run-time error are detected and displayed by the computer
during execution.
• Caused due to wrong input by the user. E.g. dividing a number by
zero.
• Example
• The user may ask the program to open a file that does not
exist.
• The user may enter wrong type of data.
Example
• #include <iostream>
using namespace std;

int main() {
cout << "Hello World!";
cout << "I am learning C++";
return 0;
}
Example
• #include <iostream>
using namespace std;

int main() {
cout << "Hello World!";
cout << "I am learning C++";
return 0;
}
New Lines
• To insert a new line, you can use the \n character:
New Lines
• To insert a new line, you can use the \n character:
New Lines
• Two \n characters after each other will create a blank line:
New Lines
• Another way to insert a new line, is with the endl manipulator:
Escape Sequence
• Both \n and endl are used to break lines. However, \n is most used.
• The newline character (\n) is called an escape sequence, and it forces
the cursor to change its position to the beginning of the next line on
the screen. This results in a new line.
• Examples of other valid escape sequences are:
Escape Sequence \t
• Creates a horizontal tab
Escape Sequence \\
• Inserts a backslash character (\)
Escape Sequence \"
• Inserts a double quote character
C++ Comments
• Comments can be used to explain C++ code, and to make it more
readable.
• It can also be used to prevent execution when testing alternative
code.
• Comments can be singled-lined or multi-lined.
Single-line Comments
• Single-line comments start with two forward slashes (//).
• Any text between // and the end of the line is ignored by the compiler
(will not be executed).
• This example uses a single-line comment before a line of code:
C++ Multi-line Comments
• Multi-line comments start with /* and ends with */.
• Any text between /* and */ will be ignored by the compiler:

• we use // for short comments, and /* */ for longer.


Practice
What would be the output of the following program?
Practice
What would be the output of the following program?
Practice
What would be the output of the following program?
Practice
What would be the output of the following program?
Practice
What would be the output of the following program?
Practice
What would be the output of the following program?
Reading Assignment
• History of C++.
• Features of C++.

You might also like