Upload
ashfaqmb
View
228
Download
0
Embed Size (px)
Citation preview
8/12/2019 cpp-study notes
1/30
8/12/2019 cpp-study notes
2/30
2
Ashfaq Ahmed Waraich
0300 755 3280
LanguagesA programming language is a means for you, to generate those
instructions so that the computer can manipulate the data in the
way you want it manipulated.
Computer is such a device that manipulates numbers. Some of the
numbers are interpreted as instructions by the computer while
some as data.
Types Of Languages.
(1). Low Level Languages.
(2). High Level Languages.
(1)-Low Level Languages.
The languages, which are close to machine language,computer's own language, are called low level languages and these
languages are far from human languages like English.
Advantages.
Programmes are highly efficient. Programmes require minimum time to run. Minimum instructions are required.Disadvantages Programmes are difficult to read and write. Programmes are not portable, i.e. a programme written on a
computer will not run on another computer. Everytime a
programme will be written on every new computer.
Languages are difficult to learn and use.Examples:
Assembly language, Machine language etc.
High Level Languages.
High level languages are those which are closer to human
languages like English and far from machine language.
Advantages
Programmes are easily readable and easy to write as well. Languages are easily learnt and used. Programmes are portable i.e. programmes are hardwareindependant.Disadvantages.
mailto:[email protected]:[email protected]8/12/2019 cpp-study notes
3/30
3
Ashfaq Ahmed Waraich
0300 755 3280
Programmes require more time to run than low languagesprogrammes.
Programmes are not highly efficient.High level languages can be further subdivided into Procedural
languages and Object Oriented Languages.
Examples:
FORTRAN, PASCAL, COBOL
C language is somewhat between high level and low level
languages.
The C Language
C is somewhat between high level languages and low levellanguages.
Authors of c are Brian.W.Kernighan and Denis M.Ritchie. Both the authors wrote c to exploit native capabilities of
implemented computer.
Programmes are much efficient and almost nearer to lowlanguage programmes
Portable programmes can be written as well C falls in block structured, procedural language category of
high level languages.
Source Code
Programme written by a programmer using any language is
called source code. This is a text file only understandable to
human beings. This code is not understandable to computers
because a computer can understand only machine language.
Compiler-Compilation
The source code that is written by a programmer is not
understandable to computer. To make it understandable to
computer, this source code should be translated to such a language
that can be easily understood to computer so that the computer
may know what we want it to be done. The process of translation
into such a form is called Compilation and a programme that
performs this job is called Compiler. Almost every language comes
with its own builtin compiler.
Object Code
When source code is translated into such a code that is
understandable to computer. This resulting code is called Object
mailto:[email protected]:[email protected]8/12/2019 cpp-study notes
4/30
4
Ashfaq Ahmed Waraich
0300 755 3280
code. Through this code computer knows what we want to do via
programme.
Integrated Development Environment (IDE).
IDE of any language is such environment where you can
write,compile,debug and run your programmes.So an environment that offers you all these features is called
Integrated Development Environment as its name implies it
integrates all above features from writing a programme to running a
programme and you dont need to switch different programmes to
do all these tasks.
Some important shortcuts for c language
F1
F2
F3
F4
F5
F6
F7
F8
F9
F10
ALT+0
ALT(1..9)ALT+F1
ALT+F3
ALT+F4
ALT+F5
ALT+F7
ALT+F8
ALT+F9
CTRL+DEL
CTRL+F1CTRL+F2
CTRL+F3
CTRL+F9
Help
File Save
File Open
Run Go To Cursor
Window Zoom
Window Next
Run Trace Into
Run Step Over
Compile(make exe)
None;takes you to menu bar
Window List
None;displays window through 1 to 9Help previous topic
Window close
Debug inspect
Window user screen
Search previous error
Search next error
Compile to obj
Edit clear
Help topic searchRun programme reset
Debug call stack
Compile+Run
mailto:[email protected]:[email protected]8/12/2019 cpp-study notes
5/30
5
Ashfaq Ahmed Waraich
0300 755 3280
Basic Structure Of C Programme
Let us investigate the various elements of c programme.
#include
void main(void)
{
Cout
8/12/2019 cpp-study notes
6/30
6
Ashfaq Ahmed Waraich
0300 755 3280
Variables are usesd so that the same space (memory location)
can hold different values at different times. You can take an
example of a mathematical expression which contains three
variables x,y,z. The value of x is depending on value of y and z i.e.
x=y+z
Whenever this expression is manipulated, for example we putdifferent values for y and z, so their addition put new value in x
everytime, let say we give y=2 and z=3 for first time, so x will hold
5 and again we change y=4 and z=8 then x=12. Same is the case
with memory variables, everytime the programme runs, variables
can hold different data.
Constants and Variables
The power of computer language comes from the ability to
use variable, which can hold many different values in programmes
statement. Let us write our previous programme again to use a
variable instead of a constant
#include
void main(void)
{
int num;
num=2;cout
8/12/2019 cpp-study notes
7/30
7
Ashfaq Ahmed Waraich
0300 755 3280
int rollno,age,clas;
When you define a variable, the compiler set aside an
appropriate amount of memory to store that variable. For an
integer variable, compiler will set aside two bytes of memory.
These two bytes are larger enough to hold numbers from 32768 to
32768. Actually the amount of memory used for a variable isdependent on the particular computer system and compiler being
used. Turbo c++ operates with two bytes. Borland c++ compiler
operates with four bytes i.e. you can make an integer variable to
hold values from 2147483648 to 2147483647. Anyhow, turbo c++
compiler is under our discussion, so we will follow as 2-byte for an
integer variable.
Some Other Data Types or Variable Types
There are, of course, other types of variables besides integer, we
shall sumamrize them here and then give examples of the uses of
the more common types.
Type Name Memory
Space(bytes)
Range Exampl
es
Int/signed int 2 -32768 to 32767 5,10,45
55Char 1 -128 to 128 a,1,
10
Long int 4 -2147483648 to
2147483647
35540
Float 4 1038to 10-38
(precision6 digits)
2.12546
Double 8 10308to 10-308
(pricision 15 digits)
5.15246
8
Long double 10 10-4932to 104932(precision 19 digit)
Short 2 Same as int Same as
int
Unsigned int 2 0 to 65535
Signed char 1 0 to 255
Signed char 1 -128 to 127
How To Initialize Variables
It is possible to combine a variable definition with an
assignment operator(=) so that a variable is assigned a value at the
same time it is defined. For example the following programme:-
mailto:[email protected]:[email protected]8/12/2019 cpp-study notes
8/30
8
Ashfaq Ahmed Waraich
0300 755 3280
#include
void main(void)
{int event=5;
char heat=C;
float time=27.25;
cout
8/12/2019 cpp-study notes
9/30
9
Ashfaq Ahmed Waraich
0300 755 3280
\ Double quote
\\ Backslash
\a Beep
\xdd ASCII code in hexadecimal notation, each d
represents a digit.
\odd ASCII code in octal notation, each d represents a
digit.
The Cin operator
cout is an input operator unlike cout that was output. cin is
an input operator so that it will be used to store data in memory.
Here is a programme that uses cin.
#includevoid main(void)
{
float years,days;
coutyears;
days=years*365;
cout
8/12/2019 cpp-study notes
10/30
10
Ashfaq Ahmed Waraich
0300 755 3280
#include
void main(void)
{
int ftemp,ctemp;
coutftemp;
ctemp=(ftemp-32)*5/9;
cout
8/12/2019 cpp-study notes
11/30
11
Ashfaq Ahmed Waraich
0300 755 3280
Increment Operator
C/C++ uses another operator that is not common in other
languages, the increment operator. Consider the following
programme.
#include
void main (void)
{
int num=0;
cout
8/12/2019 cpp-study notes
12/30
12
Ashfaq Ahmed Waraich
0300 755 3280
#include
void main(void)
{
int age;
age=15;
cout
8/12/2019 cpp-study notes
13/30
13
Ashfaq Ahmed Waraich
0300 755 3280
real life, such as going to the movies and eating a good dinner.
Programming is the same way; we frequently need to perform an
action over and over, often with variations in the details each time.
The mechanism that meets this need is the loop.
There are three major loop structures in c; the For loop, the
While loop and a cousin of the while loop called do while loop.
FOR Loop
It is often the case in programming that you want to do
something a fixed number of times. Perhaps you want to calculate
the paychecks for 120 employees or print the squares of all the
numbers from 1 to 50. The for loop is ideally suited for such cases.
Lets look at an example of a for loop.
#include
void main (void)
{
int count;
for(count=0; count
8/12/2019 cpp-study notes
14/30
14
Ashfaq Ahmed Waraich
0300 755 3280
Multiple Statements in a loop
Two or more statements can also be used in the body of for
loop. This kind of statement, consisting of more than one
statements is called compound statement and a compound
statement is always enclose in braces.Void main(void)
{
int count,total;
for(count=0; total=0;count
8/12/2019 cpp-study notes
15/30
15
Ashfaq Ahmed Waraich
0300 755 3280
Cout
8/12/2019 cpp-study notes
16/30
16
Ashfaq Ahmed Waraich
0300 755 3280
total=
8/12/2019 cpp-study notes
17/30
17
Ashfaq Ahmed Waraich
0300 755 3280
Void main(void)
{
int I;
char ch;
for(I=0; I
8/12/2019 cpp-study notes
18/30
18
Ashfaq Ahmed Waraich
0300 755 3280
The Do While loop
This loop is similar to the while loop. The difference is that in the
do loop the test condition is evaluated after the loop is executed
rather than before.
Void main(void){
int count=0;
int total=0;
do
{
total=total+count;
cout
8/12/2019 cpp-study notes
19/30
19
Ashfaq Ahmed Waraich
0300 755 3280
Decision Making Structures
Computer languages, too, must be able to perform difficult sets of
actions depending on circumstances. C has three major decision-
making structures, the IF statement, the IF-ELSE statement and the
SWITCH statement. A fourth, somewhat less important structure isthe conditional operator.
The IF statement
Like most languages, c uses the keyword if to introduce the basic
decision-making statement.
Void main(void)
{
char ch;
cout
8/12/2019 cpp-study notes
20/30
20
Ashfaq Ahmed Waraich
0300 755 3280
Example Example
Void main(void)
{
char ch;
cout
8/12/2019 cpp-study notes
21/30
21
Ashfaq Ahmed Waraich
0300 755 3280
ELSE-IF construct
Void main(void)
{
int average;
coutaverage;if((average>60)&&(average
8/12/2019 cpp-study notes
22/30
22
Ashfaq Ahmed Waraich
0300 755 3280
SWITCH Statement
The switch statement is similar to the else if construct but has more
flexibility and a clear format.
Vod main(void)Float num1=1.0,num2=1.0;
Char op;
While(!(num1==0.0 && num2==0.0))
{
Coutnum1>>op>>num2;
switch(op)
{
case+:
cout
8/12/2019 cpp-study notes
23/30
23
Ashfaq Ahmed Waraich
0300 755 3280
Max=(num1>num2)? Num1:Num2;
Two values num1 and num2 are being compared. If num1 is greater
than num2 then it will be assigned to the max variable otherwise
num2, which is obviously greater than num1 will be assigned to
max. In any case at the end max would have the larger value of
two given values.
FUNCTIONS
A computer programme cannot handle every task alone.
Instead, it calls on other program-like entities called functions in c-
to carry out specific task.
Simple function
Void line(void);
Void main(void)
{
line();
cout
8/12/2019 cpp-study notes
24/30
24
Ashfaq Ahmed Waraich
0300 755 3280
The line is declarator. The first void means that line could not
return anything and the second void means that it takes no
arguments.
Note that the declarator does not end with a semicolon. It is not a
programme statement whose execution causes something to
happen. Rather it tells the compiler that a function is beingdefined.
The function definition continues with the body of the function: the
program statements that do the work.
2.Calling the Function.As with the c library function we have seen such as cout and
getch(), our user written function line() is called from main(),
simply by using its name, including the parenthesis following the
name. The parenthesis let the compiler know that you are referring
to a function and not a variable or something else. Calling a
function like this is a c statement, so it ends with a semicolon.
line ();
this function call causes control to be transferred to the code in the
definition of line(). This function call causes drawing its rows of
square on the screen, and then return to main(), to the statement
immediately following the function call.
3.Function Prototype.This is the line before the beginning of main():
Void line (void);
This looks very much like the declarator line at the start of the
function definition, except that it ends with a semicolon. What is
purpose?
You have already seen many examples of variables in c programs.
All the variables were defined by name and data type before theywere used. A function is declared in a similar way at the beginning
of a programme before it is called. The function definition (or
prototype-mean same thing) tells the compiler the name of the
function, the data type, the function returns (if any) and the
number and data types of the functions arguments (if any).
Local or AutomaticVariables
The variable j used in the line() function is known only to
line(); it is invisible to the main() function. If we add the following
statement to main () (without declaring a variable j)
Printf(%d,j);
We would get a compiler error because main() would know nothing
about this variable. We could declare another variable also called j
mailto:[email protected]:[email protected]8/12/2019 cpp-study notes
25/30
25
Ashfaq Ahmed Waraich
0300 755 3280
in the main(), it would be a completely separate variable, known to
main() but not to line(). Variables defined in a function are
unknown outside the function. The question of which function
know about a variable and which does not is called visibility of a
variable. A local variable will be visible to the function it is defined
in but not to others.A local variable used in this way in a function is known in c as
an automatic variable, because it is automatically created when a
function is called and destroyed when the function returns. The
length of time a variable lasts is called its lifetime.
Functions that return a value.
A function that uses no arguments but returns a value perform
a similar role as you call 14 through your telephone set and it
returns current time. So you are saying nothing just calling and it
gives you back time because computer is on other side. You call
the function, it gets a certain piece of information and returns it to
you. The function getche() operates in just that way, you call it-
without giving it any information and it returns the value of the first
character typed on the keyboard.
Suppose we want to write a programme that takes upper case
letters from us and returns us lower case letters.
Programme
Char getlc(void);
Void main(void);
{
char chlc;
cout
8/12/2019 cpp-study notes
26/30
26
Ashfaq Ahmed Waraich
0300 755 3280
ch=getche();
if(ch>64 && ch64 && ch
8/12/2019 cpp-study notes
27/30
27
Ashfaq Ahmed Waraich
0300 755 3280
Void bar(int);
Void main(void)
{
Cout
8/12/2019 cpp-study notes
28/30
28
Ashfaq Ahmed Waraich
0300 755 3280
External Variable or Global Variable
It is sometimes desirable to use a variable known to (that is
visible to) all the functions in a programme, rather than just one.
In this case we have external variable sometimes called globalvariable.
Void oddeven(void);
Void negative(void);
Int keynumb;
Void main(void)
{
Coutkeynumb;
oddeven();
negative();
}
void oddeven(void)
{
if(keynumb%2)
cout
8/12/2019 cpp-study notes
29/30
29
Ashfaq Ahmed Waraich
0300 755 3280
Num=44;
You are asking the compiler to translate this code into
machine language instructions that can be executed by the
microprocessor chip in the computer. Thus most of your listing
consists of instructions to the microprocessor. Preprocessor
directives, on the other hand, are instructions to the compileritself. Rather than being translated into machine language, they
are operated on directly by the compiler before the compilation
process even begins; hence the name preprocessor.
The #define Directive.
The simplest use for the #define directive is to assign names
(Days,Pi, for example) to constants such 365 or 3.1415 etc.
#include
#define PI 3.1415
void main(void)
{
int radius,area;
coutradius;
area=PI*radius*radius;
cout
8/12/2019 cpp-study notes
30/30
30
Int func(void);
Void main(void)
{
int j;
for(j=0; j