Influencing policy (training slides from Fast Track Impact)
[ITP - Lecture 03] Introduction to C/C++
1. Intro to C/C++ Intro to Programming
MUHAMMAD HAMMAD WASEEM 1
INTRODUCTION
C++, pronounced as “See Plus Plus”, is a powerful computer programming language. It is the
advance version of C language. The C is a procedural language while the C++ is an object oriented
programming language.
STRUCTURE OF C++ PROGRAMS
A C/C++ program consists of three main parts. These are:
Preprocessor Directives
The main function
C/C++ statements
Preprocessors Directives
The instructions that are given to the compiler before the beginning of the actual program are
called Preprocessor Directives. These are also known as Compiler Directives. The compiler adds special
instructions or code from these directives into the program at the time of compilation.
These preprocessor directives normally stat with a number sign (#) and the keyword “include”
or “define”.
A program example is given below. The first statement of the program is preprocessor directive.
This preprocessor has been written to include the iostream.h header file in C++ and stdio.h in C.
C Language C++ Language
#include <stdio.h>
main()
{
printf(”this is my first
program”);
}
#include <iostream.h>
main()
{
cout<<”this is my first program”;
}
Header File
Header file in C/C++ source file that contains definitions of library functions /objects. Header files
are added into the program at the compilation of the program.
The preprocessor directive “include” is used to add a header file into the program. The name of
the file is written in angle brackets (< >) after “#include” directives. It can also be written in double
quotes. The header file stdio.h / iostream.h has definitions of different built-in input and output objects
and functions. The syntax of the header file is:
#include <name of the header file>
The main () Function
The main function indicates the beginning of a C/C++ program. The main () must be included in
every C/C++ program. When a C/C++ program is executed, the control goes directly to the main ()
function. The statements within this function are the main body of the C/C++ program. If main () function
is not included, the program is not compiled and an error message is generated.
The syntax of the main function is:
main ()
{ Programs statements…. }
2. Intro to C/C++ Intro to Programming
MUHAMMAD HAMMAD WASEEM 2
C++ Statements
The statements of the program are written under the main () function between the curly braces
{}. These statements are the body of the program. Each statement in C/C++ ends with a semicolon (;).
C/C++ is a case sensitive language. The C/C++ statements are normally written in lowercase
letters but in some exceptional cases, these can also be written in uppercase.
Keywords
The words that are used by the language for special purposes are called keywords. These are also
called reserved words. For example, in a C/C++ program, the word main is used to indicate the starting
of program, int to declare an integer type variable etc.
These keywords cannot be used as variable names in a program.