C
C
Todays Menu
History of C++.
Writing C++ Program.
Introduction
C plus plus is a powerful computer language.
Advance version of C language.
C is a procedural language (e.g. get some input, add these numbers,
Brief History
In 1967 BCPL was developed by Martin Richards.
In 1969 B language was developed by Ken Thomson.
Both B and BCPL were type-less languages.
In 1972 C language was developed by Dennis Ritche. (data type)
C++ Statements
main()
{
program statements
}
Key Words
The words that are used by the language for special purposes are called
Tokens
Example:
#include <iostream.h>
main()
{ int a, b, c;
}
Variables
A quantity whose value may change during the execution of program is
called as variable.
variable name.
Reserved words cant be used as variable names.
A variable name used for one data type cant be used for another data
type
C++ is case sensitive language so Pay and pay are two different
variables.
Some examples of the valid and invalid variable names are given as:
Variable Name
Valid/Invalid
Hassan
valid
perform
valid
double
invalid
foxpro
valid
switch
invalid
Tania
valid
int
invalid
3taq
invalid
unsigned
invalid
x-y
invalid
Taq Ahd
invalid
Remarks
10
11
Declaration of variables
Assigning the name and data type that a variable can hold is called
Initialization of Variables
Assigning a known value to a variable at the time of its declaration is
12
Example:
#include <iostream.h>
#include <conio.h>
main ()
{
int a=2,b=3;
float p=3.14;
char name[8]=tariq;
cout<<a<<endl;
cout<<b<<endl;
cout<<p<<endl;
cout<<name<<endl;
getch();
}
Lecturer: Engr. Tariq Sadiq
13
Constants
A quantity that cant change its value during execution of program.
Integer constants
14
Example:
a , / , +
String constants
A sequence of characters consisting of alphabets, digits and/or special
characters enclosed in double quotation marks is called string constant
Example:
Pakistan , Lahore-54500
Const Qualifier
The data item that follows the keyword const cant change its valude
during execution of the program.
Example:
15
#include <iostream.h>
#include <conio.h>
main ()
{
int r=2;
Const float p=3.14;
float peri;
peri=2*p*r;
cout<<Result is=<<peri;
getch();
}
16
17
Arithmetic Operators
Operator
Meaning
Addition
Subtraction
Multiplication
Division
For remaninder
18
#include <iostream.h>
main ()
{
int sum,subtract,multiplication,division,remainder;
sum=5+2;
Subtract=5-2;
Multiplication=5*2;
Division=5/2;
Remainder=5%2;
Cout<<Addition of 5&2 is=<<sum<<endl;
Cout<<Subtraction of 5&2 is=<<subtraction<<endl;
Cout<<Multiplication of 5&2 is=<<multiplication<<endl;
Cout<<Divison of 5&2 is=<<divison<<endl;
Cout<<Remainder of 5/2 is=<<remainder<<endl;
getch();
}
Lecturer: Engr. Tariq Sadiq
19
Arithmetic Expression
If m=10, x=5
res=m*x+100
20