Вы находитесь на странице: 1из 126

InfoSoft Computer Education

Building Successful Careers.

PREFACE

The computer is a versatile and powerful tool for you and


your organisation. It enables you to work more effectively and
to communicate with people around the world.
The challenges of the 21st century will require new ways
of thinking about and understanding the complex,
interconnected and rapidly changing world in which we live
and work. And the IT field of complexity science is providing
the insights and we need to push our thinking in new
directions."
In a rapidly changing technological world, we need
experts that make it simple for individuals, businesses and
companies to get the job done without having to wonder how
it is done.
This module is prepared with an intention to provide best
theory knowledge and practical skill persisting these
Computer Fundamentals. InfoSoft believes in providing up to
date knowledge and thus the book has been designed by C.A.
& MCA’s keeping in mind an individual’s future and
commercial requirement of the Industry.

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 1~
InfoSoft Computer Education
Building Successful Careers.
InfoSoft is proud of its R & D Team for developing such a
Book that enhances the in-built quality of individuals aspiring
future in the field of computer sector.

Regards:-

Research & Development Team – InfoSoft

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 2~
InfoSoft Computer Education
Building Successful Careers.
Chapter 1: Introduction to Programming
What Is a Program?
• To Program Is An Art.
• The Art cannot be learnt without really making art pieces.
• Similarly, Programming cannot be learnt without just by knowing or reading syntax
of a particular programming language.
• To learn a programming, one must write programs on computer.
• The later part of chapter discusses the two common problem solving methods:
(1) Algorithm
(2) Flowchart
• Types Of Programming language:
(1) Machine Language Programming
(2) Assembly Language Programming
(3) Higher Level Language Programming

What Is Machine Language Programming?

Machine Language Programming is done using binary codes of underlying Processor. As a


binary codes (instruction set) varies from Machine to Machine (processor to processor),
programs Written using binary codes of one machine language are not portable

Another problem is that machine language is to remember binary codes for each
instruction.
For Example, addition of to numbers may be done by using binary codes 010110101. It is
very difficult for human being to remember such codes for 100 to 200 different operations.
The solution is to use short name called mnemonics for each binary codes .

For Example :

Addition is represented by “ADD”.

What is Assembly Language Programming?

The solution is to use short name called mnemonics for each binary codes .
For Example:- Addition is represented by “ADD”. The program written by using such
mnemonics is known as assembly language program. Assembly language programming
thus solved problem of remembering binary codes.

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 3~
InfoSoft Computer Education
Building Successful Careers.
After Writing Assembly languages program, A Software tool called Assembler is convert
into machine language program because computer understand only binary languages.
The advantages of machine and assembly is that programs written using them are faster.

What is higher level languages?

The higher level languages are like natural languages as they use phrases of natural
languages. It make them easy to understand the program development also becomes
simpler and faster.
A single statement in a higher level language denotes several operation in a machine or
assembly languages. There number of higher level languages are available.

For Example,

BASIC ,FORTON , COBOL, C, C++, JAVA All are higher level languages.
A Software tool called compiler is used to translate higher level languages into machine
languages.
A separate compiler is required for each language .the BC (Boarland-C) is very popular
compiler available for PC machine.

Characteristics Of Higher Level languages:


(1) Interactive
(2) Variety of data type
(3) Rich set of operators
(4) Flexible control Structure
(5) Readability
(6) Modularity
(7) File Handling
(8) Memory Management
(9) Procedural Vs. Object-Oriented
(10) Procedure or event-driven
(11) Availability of Library Support
(12) Interface to other Language

Interactive:

The major advantage of higher level languages is that they are interactive. They Allow
user to interface with program through sophisticated input-output statements while
program is being executed on a system

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 4~
InfoSoft Computer Education
Building Successful Careers.

Variety of data type :


Data types are broadly categories in to two ways:
(1) Basic
(2) User Defined
The Basic type include data types for characters ,integers, and real
values.
The user defined types include data types for more complex information.
Example of the user defined data types are array, structure or records,
unions etc.
C supports four basic types :
(1) char
(2) int
(3) float
(4) double
Rich set of operators:
An operator defines operation to be performed on data. For example ,+ defined
arithmetic Addition of two numbers.
Commonly There are Five types of operators:
(1) Arithmetic Operators(+,-,*,/)
(2) Logical Operators(AND,OR,NOT)
(3) Relational Operators(>,<,==,!=,>=,<=)
(4) Conditional Operators(Exp1?Exp2:Exp3)
(5) Increment/decrement Operators (++/- -)
Flexible control Structure:
The control structures are Providing necessary mechanism for implementing
Algorithm in language format. The structure programming languages mainly three types
of control structure:
(1) Sequence
(2) Selection or decision
(3) Repetition or looping
(1) Sequence:
The sequence is inherit to any programming language as control Always goes
to next statement in sequence unless otherwise, it is intentionally send to
some other part.
For Example,
X = 5;
Y=X+10;
(2) Selection or decision :

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 5~
InfoSoft Computer Education
Building Successful Careers.
The selection statement or decision Statement are very important and used
for deciding further executing path based on logical condition .
A decision Making programs Skip a some part of program.
The C Programming Languages Support Some selection statement or decision
Statement like if , if-else , Multiple if-else , Switch etc…
For Example,
If(x>y)
Printf (“%d” ,x);
Else
Printf (“%d” ,y);
(3) Repetition or looping:
Many times, it is necessary that same part of process if the program is to be
repeated .for this purpose , Languages support three different types of
looping statement:
(1) For loop
(2) While loop
(3) Do-While loop
For Example,
For(int i=0;i<=10;i++)
{
Printf(“%d”,);
}
Readability:
Once a Program is Written, it is read many times in feature by same or other
person. A program if read it after long time by some person looks new and easily read and
understand.
In C comment is Written in /*……*/. Any thing between /* and */ ,is considered as
comment and removed during compilation process as comment are not part of program
which are written just for improving readability of a program.

Modularity:

Programming language support concept known as modularity in order to handle size and
complexity effectively.
Modularity is nothing but dividing large program into collection of small subroutines
.Each subroutines is itself a small program Performed well defined task.

File Handling :

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 6~
InfoSoft Computer Education
Building Successful Careers.
In many Application, input to program is taken form another file which is already
available
Or output of program is stored in file for feature use.
In a C programming ,file management is supported through rich set of the
file handling functions which allows user to open a file, read from file , write into
file etc…

Memory Management:
The memory management can be supported either through library calls or directly
using system calls. The C programming Support Memory Management Function in library
known as <malloc.h>. it provides function like malloc() to allocate memory dynamically
and free() to release the memory.
In language like c++ and java, memory management is done through operators
New And Delete.
Procedural Vs. Object-Oriented:
Procedural language emphasize on Procedures, while object oriented languages
emphasize on object oriented principles like object ,classes ,encapsulation ,inheritance
etc….
The C is procedural language , While C++ Is Object oriented language Another
Example of Object oriented languages is that java.
Procedural Vs. Event driven :
In procedure driven programming, Execution is strictly done by calling procedures
i.e. one procedure call another which may be some procedure.
For Example, execution of C program begins by calling function main() which acts
as entry function and calls other function in a program.
Modern soft wares are GUI(graphical user interface) soft wares which are developed
using visual languages like Microsoft visual basic, VC++,sun JAVA etc… All GUI programs
are event driven.
Availability of Library Support:
The C Compilers are available with rich Supported library function. A very common
library is standard I/O Which provides functions related to Input/output. It is supported
through header file #include<stdio.h> the other common C libraries are string Maths ,
Ctype ,etc…
Interface to other Language:
Many programming languages supported interface to other language i.e. they
provide extension to other languages. Such interface or extension is very useful for many
reasons.

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 7~
InfoSoft Computer Education
Building Successful Careers.
For Example, c support interface to assembly languages statement within C
program itself. It then facilitates to access operating system services directly or some
critical portion can be written in assembly for efficiency purpose.
Problem Solving Methods:
There Are two types of Problem Solving Methods:
(1) Algorithm
(2) Flowchart
(1)Algorithm: An algorithm is a step wise solution of program. Algorithm gives a
sequence of steps which makes complete solution of a programs in hend.
For, Example: Write a program to find ares of circle.
So, We find out:
A=PIE*R*R
Algorithm:
Input R
Compute A=3.14 *R*R
Print A
STOP
(2)Flowchart:
A flow Chart is a Pictorial representation of Algorithm. Flowchart consider
Some Symbol are as follows:
Symbol Use
Start/stop

Input/output

Process or Computation

Flow Direction

For Example:- Draw the flowchart to find a area of circle:

start
Start

Input
RAOPURA: - 112 -113,
R Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 8~
InfoSoft Computer Education
Building Successful Careers.
Input

A=Pi*R
A=Pi*R*
R
*R

Print
A

Stop

Introduction To C
C is a relatively small language, but one which it higher level language.
C's small ,it has some advantage: there's less to learn; there isn't excess baggage in the
way when you don't need it.(you can give a comment if some part is not used in
program).
It has also be a disadvantage: since it doesn't do everything for you, there's a lot you have
to do yourself.
(Actually, this is viewed by many as an additional advantage: anything the language
doesn't do for you, it doesn't dictate to you, either, so you're free to do that something
however you want.)
C is referred to a ``high-level assembly language.''
Some people think that's an insult, but it's actually a deliberate and significant aspect of
the language.
If you have programmed in assembly language, you'll probably find C very natural and
comfortable (although if you continue to focus too heavily on machine-level details, you'll
probably end up with unnecessarily non portable programs).
If you haven't programmed in assembly language, you may be frustrated by C's lack of
certain higher-level features. In either case, you should understand why C was designed
this way: so that seemingly-simple constructions expressed in C would not expand to
arbitrarily expensive (in time or space) machine language constructions when compiled.
If you write a C program simply, it is likely to result in a efficient machine language
executable. If you find that the executable program resulting from a C program is not
efficient, it's probably because of something silly you did not because of something the
compiler did behind your back which you have no control over.
Feature of C:
(1) C is portable
(2) C does not certain input/output statements.
(3) C is modular language.

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 9~
InfoSoft Computer Education
Building Successful Careers.

Example:
A programming language is a tool. If you're building a house, and I'm teaching you how to
use a hammer, and you ask how to assemble rafters and trusses into gables, that's a
legitimate question, but the answer has fallen that ``How do I use a hammer?'' and into
``How do I build a house?''. In the same way, we'll see that C does not have built-in
features to perform every function that we might ever need to do while programming.
As mentioned above, C imposes relatively few built-in ways of doing things on the
programmer. Some common tasks, such as manipulating strings, allocating memory, and
doing input/output (I/O), are performed by calling on library functions.
Other tasks which you might want to do, such as creating or listing directories, or
interacting with a mouse, or displaying windows or other user-interface elements, or doing
color graphics, are not defined by the C language at all.
You can do these things from a C program, of course, but you will be calling on services
which are peculiar to your programming environment (compiler, processor, and operating
system) and which are not defined by the C standard. Since this course is about portable C
programming, it will also be steering clear of facilities not provided in all C environments.
This aspect of the language which it's fairly pointless to complain about. If you take care
and pay attention, you can avoid many of the pitfalls. These notes will point out many of
the obvious (and not so obvious) trouble spots.
Example:
The first example program in C language is:-
print or display a simple string, and Exit ``hello, world'' program:
#include <stdio.h>
#include <conio.h>

main()
{
Clrscr();
printf("Hello, world!\n");
getch ();
}
If you have a C compiler, the first thing to do is figure out how to type this program in and
compile it and run it and see where its output went. (If you don't have a C compiler yet,
the first thing to do is to find one.)
The first two line #include <stdio.h> And #include <conio.h> is practically
boilerplate; it will appear in almost all programs we write. It asks that some definitions
having to do with the ``Standard I/O Library'' be included in our program; these definitions
are needed if we are to call the library function printf correctly.

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 10 ~
InfoSoft Computer Education
Building Successful Careers.
The second line says that we are defining a function named main. Most of the time, we
can name our functions anything we want, but the function name main is special: it is the
function that will be ``called'' first when our program starts running.
The empty pair of parentheses ( And ) indicates that our main function accepts no
arguments, that is, there isn't any information which needs to be passed in when the
function is called.
The braces { and } surround a list of statements in C. Here, they surround the list of
statements making up the function main.
The line
printf("Hello, world!\n");
is the first statement in the program. It asks that the function printf be called; printf is a
library function which prints formatted output. The parentheses surround printf's
argument list: the information which is handed to it which it should act on. The semicolon
at the end of the line terminates the statement.
(printf's name reflects the fact that C was first developed when Teletypes and other
printing terminals were still in widespread use. Today, of course, video displays are far
more common. printf's ``prints'' to the standard output, that is, to the default location for
program output to go. Nowadays, that's almost always a video screen or a window on that
screen. If you do have a printer, you'll typically have to do something extra to get a
program to print to it.)
printf's first (and, in this case, only) argument is the string which it should print. The
string, enclosed in double quotes "", consists of the words ``Hello, world!'' followed by a
special sequence: \n. In strings, any two-character sequence beginning with the
backslash \ represents a single special character. The sequence \n represents the ``new
line'' character, which prints a carriage return or line feed or whatever it takes to end one
line of output and move down to the next. (This program only prints one line of output, but
it's still important to terminate it.)
The second line in the main function is
Getch();
In general, a function may return a value to its caller, and main is no exception. When
main returns (that is, reaches its end and stops functioning), the program is at its end, and
the return value from main tells the operating system (or whatever invoked the program
that main is the main function of) whether it succeeded or not. By convention, a return
value of 0 indicates success.
This program may look so absolutely trivial that it seems as if it's not even worth typing it
in and trying to run it, but doing so may be a big (and is certainly a vital) first hurdle. On
an unfamiliar computer, it can be arbitrarily difficult to figure out how to enter a text file
containing program source, or how to compile and link it, or how to invoke it, or what
happened after (if?) it ran. The most experienced C programmers immediately go back to
this one, simple program whenever they're trying out a new system or a new way of

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 11 ~
InfoSoft Computer Education
Building Successful Careers.
entering or building programs or a new way of printing output from within programs. As
Kernighan and Ritchie say, everything else is comparatively easy.
How you compile and run this (or any) program is a function of the compiler and operating
system you're using. The first step is to type it in, exactly as shown; this may involve using
a text editor to create a file containing the program text. You'll have to give the file a
name, and all C compilers (that I've ever heard of) require that files containing C source
end with the extension .c. So you might place the program text in a file called hello.c.
The second step is to compile the program. (Strictly speaking, compilation consists of two
steps, compilation proper followed by linking, but we can overlook this distinction at first,
especially because the compiler often takes care of initiating the linking step
automatically.) On many Unix systems, the command to compile a C program from a
source file hello.c is
cc -o hello hello.c
You would type this command at the Unix shell prompt, and it requests that the cc (C
compiler) program be run, placing its output (i.e. the new executable program it creates)
in the file hello, and taking its input (i.e. the source code to be compiled) from the file
hello.c.
The third step is to run (execute, invoke) the newly-built hello program. Again on a Unix
system, this is done simply by typing the program's name:
hello
Depending on how your system is set up (in particular, on whether the current directory is
searched for executables, based on the PATH variable), you may have to type
./hello
to indicate that the hello program is in the current directory (as opposed to some ``bin''
directory full of executable programs, elsewhere).
You may also have your choice of C compilers. On many Unix machines, the cc command
is an older compiler which does not recognize modern, ANSI Standard C syntax. An old
compiler will accept the simple programs we'll be starting with, but it will not accept most
of our later programs. If you find yourself getting baffling compilation errors on programs
which you've typed in exactly as they're shown, it probably indicates that you're using an
older compiler. On many machines, another compiler called acc or gcc is available, and
you'll want to use it, instead. (Both acc and gcc are typically invoked the same as cc; that
is, the above cc command would instead be typed, say, gcc -o hello hello.c .)

(One final caveat about Unix systems: don't name your test programs test, because
there's already a standard command called test, and you and the command interpreter
will get badly confused if you try to replace the system's test command with your own, not
least because your own almost certainly does something completely different.)

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 12 ~
InfoSoft Computer Education
Building Successful Careers.
Under MS-DOS, the compilation procedure is quite similar. The name of the command you
type will depend on your compiler (e.g. cl for the Microsoft C compiler, tc or bcc for
Borland's Turbo C, etc.). You may have to manually perform the second, linking step,
perhaps with a command named link or tlink. The executable file which the compiler/linker
creates will have a name ending in .exe (or perhaps .com), but you can still invoke it by
typing the base name (e.g. hello). See your compiler documentation for complete details;
one of the manuals should contain a demonstration of how to enter, compile, and run a
small program that prints some simple output, just as we're trying to describe here.

In an integrated or ``visual'' programming environment, such as those on the Macintosh or


under various versions of Microsoft Windows, the steps you take to enter, compile, and run
a program are somewhat different (and, theoretically, simpler). Typically, there is a way to
open a new source window, type source code into it, give it a file name, and add it to the
program (or ``project'') you're building. If necessary, there will be a way to specify what
other source files (or ``modules'') make up the program. Then, there's a button or menu
selection which compiles and runs the program, all from within the programming
environment. (There will also be a way to create a standalone executable file which you
can run from outside the environment.) In a PC-compatible environment, you may have to
choose between creating DOS programs or Windows programs. (If you have troubles
pertaining to the printf function, try specifying a target environment of MS-DOS.
Supposedly, some compilers which are targeted at Windows environments won't let you
call printf, because until you call some fancier functions to request that a window be
created, there's no window for printf to print to.) Again, check the introductory or tutorial
manual that came with the programming package; it should walk you through the steps
necessary to get your first program running.

Data Types:
There are only a few basic data types in C. The first ones we'll be encountering and using
are:
• char (a character)
• int (an integer, in the range -32,767 to 32,767 )
• long int (a larger integer (up to +-2,147,483,647))
• float( a floating-point number )
• double (a floating-point number, with more precision and perhaps greater range
than float )

C Character set And Tokens:


Character :

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 13 ~
InfoSoft Computer Education
Building Successful Careers.
The Words(tokens) and statement used in any language are from their basic character
set. For Example, the word used in English language are formed from its alphabet having
26 different symbols i.e. A,B,C…..,Z or a,b,c…..,z the character in the c are divided into
four categories.
(1) Letters or alphabets
(2) Digits
(3) Special character
(4) White Spaces
The letters include the uppercase(A,B . . . .,Z) And lowercase (a,b,c . . . .,z) Alphabets of
Engilsh Language. The Digits include the 0,1,2,3. . .,9.
The C also use special character like ;(semicolon),’(single quotes),”(double quotes),+,-,*,/,
%,>,= etc. for different purposes.
The white space are used to separate the word or tokens. they are blank, tab or newline.
Tokens :
The character together makes special symbol or word known as token. The Example of
the token are the words used to define data types like int , float ,etc…Similarly any
operator i.e. +,-,*,/,%,> etc. are also tokens. The constants used in program like 12,12.67
are also tokens.
Keywords:
The keywords are the special words used in C programming having specific meaning .the
meaning of the key word are not changed. They are also known as reserve words. The
keyword can not be used as identifiers.
Some Example of Keywords are:

(1) Auto (7) Float (11) Got (14) Do


(2) Break (8) If o (15) Swit
(3) Case (9) For (12) Voi ch
(4) Int (10) Whil d
(5) Struct e (13) Stat
(6) Long ic

Identifiers:
The identifiers are user defined names used in programs for providing name to
variables, arrays
And function . The identifiers are made up of letters(upper case and lower case) ,digits and
underscore .
The C is case sensitive language.
For Example: max And Max are not same.

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 14 ~
InfoSoft Computer Education
Building Successful Careers.
Constants:
The constants are the values Which Are never change.
The C is Use Following types of constants:
(1) Numeric Constants.
a. Integer
b. Real

(2) Non- Numeric Constants.


a. Character
b. String

Integer:
Integer constants represent the Whole Number.it is used 0 to9 and option sign -
and + before number.
For Example,
123
-23
0
+38
Real:
Real constants represent the number with fractional parts i.e. digits after decimal
Part
For Example,
0.0008
-0.158
+581.5
568.
.58
Character:
Character constants represent single character and are always enclosed in single
quotes.
For Example,
‘A’
‘a’
‘?’
‘:’
‘8’

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 15 ~
InfoSoft Computer Education
Building Successful Careers.
Within character and string constants, the backslash character \ is special, and is
used to represent characters not easily typed on the keyboard or for various reasons not
easily typed in constants. The most common of these ``character escapes'' are:
\n a ``newline'' character
\b a backspace
\r a carriage return (without a line feed)
\' a single quote (e.g. in a character constant)
\" a double quote (e.g. in a string constant)
\\ a single backslash
\t a horizontal tab
\0 a Null

For example, "he said \"hi\"" is a string constant which contains two double quotes, and '\''
is a character constant consisting of a (single) single quote. Notice once again that the
character constant 'A' is very different from the string constant "A".

String:
A string is represented in C as a sequence or array of characters. (We'll have more to say
about arrays in general, and strings in particular, later.) A string constant is a sequence of
zero or more characters enclosed in double quotes
For Example:
"1992"
"hello, world"
"this is a test"
“D” (Not that “D” And ‘D’ Are not same)
Variable:
The variables represent the quantities which changes with time. Each variable
identified by a unique identifier in a program called variable name
For Example, length , area , max etc… Are Variable

Variable Declarations
Informally, a variable (also called an object) is a place you can store a value. So that you
can refer to it unambiguously, a variable needs a name. You can think of the variables in
your program as a set of boxes or cubbyholes, each with a label giving its name; you
might imagine that storing a value ``in'' a variable consists of writing the value on a slip of
paper and placing it in the cubbyhole.
A declaration tells the compiler the name and type of a variable you'll be using in your
program. In its simplest form, a declaration consists of the type, the name of the variable,
and a terminating semicolon:

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 16 ~
InfoSoft Computer Education
Building Successful Careers.
char c;
int i;
float f;
You can also declare several variables of the same type in one declaration,
separating them with commas:
int i1, i2;
Later we'll see that declarations may also contain initializers, qualifiers and storage
classes, and that we can declare arrays, functions, pointers, and other kinds of data
structures.
The placement of declarations is significant. You can't place them just anywhere (i.e. they
cannot be interspersed with the other statements in your program). They must either be
placed at the beginning of a function, or at the beginning of a brace-enclosed block of
statements (which we'll learn about in the next chapter), or outside of any function.
Furthermore, the placement of a declaration, as well as its storage class, controls several
things about its visibility and lifetime, as we'll see later.

Operators:
There are five types of Operators:
(1) Arithmetic Operators
(2) Relational Operators
(3) Logical Operators
(4) Assignment Operators
(5) Inc/Dec Operators
(6) Ternary Operators
(7) Bit-Wise Operators
(8) Comma Operator
(9) Sizeof Opearator

Arithmetic Operators:
The C include following types of Arithmetic Operators:
Operator Meaning
s
+ Addition or Unary
- plus
* Subtraction or Unary
/ minus
Multiplication
%
Division
Modulo division

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 17 ~
InfoSoft Computer Education
Building Successful Careers.
Relational Operators:
When There are a comparison of age of two person, comparison of Marks of two Students
etc…
Like (a>b),(3>2).
The C include following types of Relational Operators:
Operator Meaning
s
> Greater then
>= Greater then or equal
< to
<= Less then
Less then or equal to
==
Equal to
!=
Not Equal to

Logical Operators:
The Expressions Having Two or more relational expression is called logical expression.
The C include following types of Logical Operators:
Operator Meaning
s
&& Logical END
|| Logical OR
! Logical NOT

Assignment operator:
The assignment operator = assigns a value to a variable.
For example,
x=1
sets x to 1, and
a=b
sets a to whatever b's value is.
The expression
i=i+1
is, as we've mentioned elsewhere, the standard programming idiom for increasing a
variable's value by 1: this expression takes i's old value, adds 1 to it, and stores it back
into i. (C provides several ``shortcut'' operators for modifying variables in this and similar
ways, which we'll meet later.)
We've called the = sign the ``assignment operator'' and referred to ``assignment
expressions'' because, in fact, = is an operator just like + or -. C does not have

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 18 ~
InfoSoft Computer Education
Building Successful Careers.
``assignment statements''; instead, an assignment like a = b is an expression and can be
used wherever any expression can appear. Since it's an expression, the assignment a = b
has a value, namely, the same value that's assigned to a. This value can then be used in a
larger expression; for example, we might write
c=a=b
which is equivalent to
c = (a = b)
and assigns b's value to both a and c. (The assignment operator, therefore, groups from
right to left.) Later we'll see other circumstances in which it can be useful to use the value
of an assignment expression.
It's usually a matter of style whether you initialize a variable with an initializer in its
declaration or with an assignment expression near where you first use it. That is, there's
no particular difference between
int a = 10;
and
int a;
/* later... */
a = 10;

INCREMENT / DECREMENT OPERATOR:

The ++ (increment) and - - (decrement) operator both are unary operators. The increment
operator increments the integer value by 1, while decrement operator decrements integer
value by 1. They are used only with integer variables. The increment and decrement are of
two types: PREFIX AND POSTFIX. The ++m and - -m are example of prefix increment and
prefix decrement operators. Similarly, m++ and m- - are examples of postfix increment
and postfix decrement.

For Example:
int x;
int n = 5;
x= ++n;
Here, the last statement x=++n; used the prefix ++ operator.
If we replace the third statement in preceding Example by:
X=n++;
Then after the execution of statement n=6 but x=5 as it uses the postfix increment
which means assign first and then increment. We can say from preceding discussion that
X=++n;
Is the short form of

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 19 ~
InfoSoft Computer Education
Building Successful Careers.
n-n+1;
x=n;
and
x=n++;
is the short form of
x=n;
n=n+1;

Ternary Operator:
This is also known as conditional operator. It can be written as follows.
e1?e2:e3 ;
Where e1 is Logical Expression and Evaluated First. If it is true, then
Expression e2 is executed otherwise expression e3 is executed. The following if-else
statement
If(X>Y)
Max=x;
Else
Max=y;
Is written in using ternary operator as follows.
Max=(x<y) ? X:Y;
Here, the maximum of x and y is assigned to Max

Bit-Wise Operator:

Normally, higher level language do not use Bit wise Operator as they denote the lower
level operations which are common in assembly/ machines language but C language is
exception. the c support Bit Wise operators
To perform bit-wise operators like ANDing , ORing ,etc. table gives the all Bit-wise
operators.

Operato Meaning
rs
& Bit-Wise AND
| Bit-Wise OR
^ Bit-wise Exclusive OR
<< Left shift
>> Right shift
~ 1’s Complement

Comma Operator:

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 20 ~
InfoSoft Computer Education
Building Successful Careers.
The comma operator is used to combine the multipal statement which are related to a
single statement. Consider as Following:
X=5;
Y=10;
Z=X+Y;
These Statement Assign the values to X and Y and then stores their sum into Z.they can
be written using comma operator as follows:
Z=(X=5, Y=7, X+Y);

Size Of Operator:
The size Of Opertor is used to find the size of the operand occupied in memory in bytes.
For Example,
m= sizeof(int);
will make m=2 as integer occupies 2 bytes in memory.

Printf:
Printf's name comes from print Formatted. It generates output under the control of a
format string (its first argument) which consists of literal characters to be printed and also
special character sequences--format specifiers--which request that other arguments be
fetched, formatted, and inserted into the string. Our very first program was nothing more
than a call to printf, printing a constant string:
printf("Hello, world!\n");

Our second program also featured a call to printf:


printf("i is %d\n", i);

In that case, whenever printf ``printed'' the string "i is %d", it did not print it verbatim; it
replaced the two characters %d with the value of the variable i.
There are quite a number of format specifies for printf. Here are the basic ones :
%d print an int argument in decimal
%ld print a long int argument in decimal
%c print a character
%s print a string
%f print a float or double argument
%e same as %f, but use exponential notation
%g use %e or %f, whichever is better
%o print an int argument in octal (base 8)
%x print an int argument in hexadecimal (base 16)
%% print a single %

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 21 ~
InfoSoft Computer Education
Building Successful Careers.
It is also possible to specify the width and precision of numbers and strings as they are
inserted (somewhat like FORTRAN format statements); we'll present those details in a
later chapter. (Very briefly, for those who are curious: a notation like %3d means to print
an int in a field at least 3 spaces wide; a notation like %5.2f means to print a float or
double in a field at least 5 spaces wide, with two places to the right of the decimal.)
To illustrate with a few more examples: the call

printf("%c %d %f %e %s %d%%\n", '1', 2, 3.14, 56000000., "eight", 9);


would print
1 2 3.140000 5.600000e+07 eight 9%
The call
printf("%d %o %x\n", 100, 100, 100);
would print
100 144 64
Successive calls to printf just build up the output a piece at a time, so the calls
printf("Hello, ");
printf("world!\n");

Would also print Hello, world! (on one line of output).


Earlier we learned that C represents characters internally as small integers corresponding
to the characters' values in the machine's character set (typically ASCII). This means that
there isn't really much difference between a character and an integer in C; most of the
difference is in whether we choose to interpret an integer as an integer or a character.
printf is one place where we get to make that choice: %d prints an integer value as a
string of digits representing its decimal value, while %c prints the character corresponding
to a character set value. So the lines
char c = 'A';
int i = 97;
printf("c = %c, i = %d\n", c, i);
would print c as the character A and i as the number 97. But if, on the other hand, we
called
printf("c = %d, i = %c\n", c, i);
we'd see the decimal value (printed by %d) of the character 'A', followed by the character
(whatever it is) which happens to have the decimal value 97.
You have to be careful when calling printf. It has no way of knowing how many arguments
you've passed it or what their types are other than by looking for the format specifiers in
the format string. If there are more format specifiers (that is, more % signs) than there are
arguments, or if the arguments have the wrong types for the format specifiers, printf can
misbehave badly, often printing nonsense numbers or (even worse) numbers which
mislead you into thinking that some other part of your program is broken.

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 22 ~
InfoSoft Computer Education
Building Successful Careers.
Because of some automatic conversion rules which we haven't covered yet, you have a
small amount of latitude in the types of the expressions you pass as arguments to printf.
The argument for %c may be of type char or int, and the argument for %d may be of type
char or int. The string argument for %s may be a string constant, an array of characters,
or a pointer to some characters (though we haven't really covered strings or pointers yet).
Finally, the arguments corresponding to %e, %f, and %g may be of types float or double.
But other combinations do not work reliably: %d will not print a long int or a float or a
double; %ld will not print an int; %e, %f, and %g will not print an int.

Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 23 ~
InfoSoft Computer Education
Building Successful Careers.

Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 24 ~
InfoSoft Computer Education
Building Successful Careers.

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 25 ~
InfoSoft Computer Education
Building Successful Careers.
Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 26 ~
InfoSoft Computer Education
Building Successful Careers.
Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 27 ~
InfoSoft Computer Education
Building Successful Careers.
Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 28 ~
InfoSoft Computer Education
Building Successful Careers.
Chapter 2 : Decision Making.

If Else Statement:

Introduction:
The Important and essential part of a programming language is their control structures.
The programming language is their control structure. The important control structure are:

(1) Sequence
(2) Decision
(3) Looping

In This chepter. We will study the decision making statement of c programming


language.
• if-else
• multiple If-else
• switch
• goto

If-else Statement :-

The formet of if-else is given below.

if(condition)
statement1 ;
else
stement2 ;

Here, the condition is represented by relation or logical expression.if condition is true,


then statment1 is executed and if Condition is false, then statement2 is executed.
The statement1 and/or statment2 can be either single statement or computed
statement i.e. group of statements. the else part in if-else statement is optional. The if
statement without else looks like:

if(condition)
statement ;
In this case ,if condition is true, then statement is executes, otherwise control is
transferred to the statement that follows the if structure.

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 29 ~
InfoSoft Computer Education
Building Successful Careers.
Fig. shows the execution the execution of the if else statement.

T F
Condition

Statment1 Statment2

Switch Statement :
The Switch in C is a multi-choice statement.it provides one choice for each value of
variable or expression. The block of statements associated with the choice whose value is
same as the value of variable or expression is exeqted.the format of the switch statement
is given below:-
Switch(variable or expression )
{
case lable1: statement_block1;
break ;
case lable2: statement_block2;
break ;
.
.
.
default : default_block ;
}

Here, label1 , label2 . . .show the possible values of the variable or expression. For
each value of variable or expression, one statement block is provided. For Example
statement block1 is associated with the lable1. The statement block consist of one
or more statement. After every statement block, there is break which send control to the
next statement after switch statement. The last choice is default which is chosen when
value of variable or expression does not match to any of the labels.in this case, the default
statement is executed.

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 30 ~
InfoSoft Computer Education
Building Successful Careers.

Goto Statement:

The goto statement in C programming is used to transfer the control unconditionally from
one part of the program to another part of the program. The c is structure programming
language where the use of the goto statement is very dangerous because it can easily
violate the rules of structured programming.
The format of the goto Statement is as follows:
goto label ;
Here, label is the label to the statement to which goto transfer control. The meaning is
that the execution begins from this labeled or target statement.
Following format shows the two possible use of goto statement :forward and
backward.
In forward, the target statement comes after the goto, while in backward target
statement comes before goto statement. Note that label must be followed by
colon(:) at the target statement.

(a) Goto forward

goto label;
.....
.....
Label :
statement;
(b) Goto backward

Label;
Statement ;
.....
.....
goto label;

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 31 ~
InfoSoft Computer Education
Building Successful Careers.

Programs:-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 32 ~
InfoSoft Computer Education
Building Successful Careers.
Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 33 ~
InfoSoft Computer Education
Building Successful Careers.
Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 34 ~
InfoSoft Computer Education
Building Successful Careers.
Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 35 ~
InfoSoft Computer Education
Building Successful Careers.
Chapter 3 : Looping
Introduction:

A Looping is an important control structure in higher level language Programming. The


objective of looping is to provide repetition of the part of the program i.e. block of
statement in a program , number of times. the looping statement provided by the higher
level language gives necessary control to repeat the block of statements. Consider the
following program segment which makes sum of 1 to 10.

sum = 0;
I = 1;
Sum = sum + I ;
i = i + 1;
Loop
if ( i<=10)
goto start ;
prinf(“%d\n”,sum);

As long as i<=10 . the steps followed by above example are :

1. Initialize sum with 0 and i with 1.


2. Current value of i is added to sum.
3. The i is incremented by 1.
4. If i <=10, then control gose to step 2,otherwise it gose to step 5.
5. The value of sum is printed.

The step 2 to step 5 form the loop as they are repeated 10 times .

Loop structure contain two important elements:-condition and body

There are two types of loop structured support by language as follows:-

(a) Entry control


(b) Exit control

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 36 ~
InfoSoft Computer Education
Building Successful Careers.

condition body

condition

body
(a) Entry Control (b) Exit Control

The C language support Following types of looping structure :

(1) While
(2) Do-while
(3) For

While Loop:
We have already used the while loop in many programs. The structure of while ts as
follows.

While(condition )
{
body ;
}

Here , the condition is evaluated first and if it is true, then the statement in the body of
the loop executed . after executing body, the condition is evaluated first and if it true body
is executed again this process is repeated as long as the condition is true . the control
moves out once the condition is false.it is not necessary to enclose body of loop in pair of
braces { } if body contain only one statement Fig. Shows The Flowchart of While-loop :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 37 ~
InfoSoft Computer Education
Building Successful Careers.

F
Condition

body

For Example, The While loop can be Written as a follows to print 1 to 10.
.....
.....
I=1;
While (I <=10)
{
Printf(“%d\n”,I);
I=I+1;
}
.....
.....

Do-While Loop:-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 38 ~
InfoSoft Computer Education
Building Successful Careers.

The format of the do - While is given as follows :-


do
{
Body;
}
While(condition);

In do-while ,first the body is executed and then the condition is checked. If the Condition is
true then body is true body is executed again. The body executed again and again as long
as condition is true. The control out of the loop, once the condition become false. The Do-
while loop is an Exit control loop as the condition is checked after executing the body. This
ensures that body is executed at least once even if the condition is false first time..Fig
Shows the flow chart of the do-While Statement.

body

T
condition
F

The Example of theDo while loop Are:


.....
.....
Do
{
Printf(“Enter the number”);
Scanf(“%d”,&n);
}
While(num<=0);
.....
.....

For Loop:

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 39 ~
InfoSoft Computer Education
Building Successful Careers.
The format of for Loop is as follows:
for(<e1>;<e2>;<e3>)
{
body ;
}

Here ,<e1> is the initialization of variables by initial values. the <e2>is executes only
once when we enter the loop ,if <e1> contains more than variables for initialization, they
must be separated by commas. The <e2> is the condition and if it is true, then control
enter into the body of the loop and if it is true, then control enters into the body of the
loop and executes the statements in the body. After the executing the body of the
loop ,<e3> is executed which contains statement which modifies the variable.

The <e2> is then evaluated with the modified values and if it is true, then body is again
executed. This process is repeated as long as condition represented by <e2> is true.
Once it becomes false, control comes out of the loop and executing is resumed from next
statement after for loop, if body of for loop. If the body of for loop contains only one
statement, then pair of brace { } is not required. Fig shows the flow chart of for loop.

e1

e2?

e3

body

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 40 ~
InfoSoft Computer Education
Building Successful Careers.
Nesting of Loop:

Loop inside loop is called nested loop For Example:-


......
.... ..
For( )
{
.....
.....
For( )
{
..... Outer loop
Inner loop
.....
.....
}
.....
.....
}
.....
.....

For Example:
#include<stdio.h>
#include<conio.h>

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 41 ~
InfoSoft Computer Education
Building Successful Careers.
void main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",i); Inner loop Outer loop
}
printf("\n");
}
getch();
}

Break And Continue

Sometimes, due to an exceptional condition, you need to jump out of a loop early, that is,
before the main controlling expression of the loop causes it to terminate normally. Other
times, in an elaborate loop, you may want to jump back to the top of the loop (to test the
controlling expression again, and perhaps begin a new trip through the loop) without
playing out all the steps of the current loop. The break and continue statements allow you
to do these two things. (They are, in fact, essentially restricted forms of goto.)
To put everything we've seen in this chapter together, as well as demonstrate the use of
the break statement, here is a program for printing prime numbers between 1 and 100:

#include <stdio.h>
#include <math.h>

main()
{
int i, j;

printf("%d\n", 2);

for(i = 3; i <= 100; i = i + 1)


{
for(j = 2; j < i; j = j + 1)

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 42 ~
InfoSoft Computer Education
Building Successful Careers.
{
if(i % j == 0)
break;
if(j > sqrt(i))
{
printf("%d\n", i);
break;
}
}
}

getch();
}

The outer loop steps the variable i through the numbers from 3 to 100; the code tests to
see if each number has any divisors other than 1 and itself. The trial divisor j loops from 2
up to i. j is a divisor of i if the remainder of i divided by j is 0, so the code uses C's
``remainder'' or ``modulus'' operator % to make this test. (Remember that i % j gives the
remainder when i is divided by j.)

Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 43 ~
InfoSoft Computer Education
Building Successful Careers.

Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 44 ~
InfoSoft Computer Education
Building Successful Careers.

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 45 ~
InfoSoft Computer Education
Building Successful Careers.
Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 46 ~
InfoSoft Computer Education
Building Successful Careers.
Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 47 ~
InfoSoft Computer Education
Building Successful Careers.

Chapter 4 : Arrays And String


Array is an set of element.

Arrays is useful concept for representing a group of similar values of under a single name.
thus, array can be defined simply as the group of values of same type. Whenever a
particular value from a group is needed,we can access it by name of group i. e.Arrays
name and the position of the value in the group . For Example, group of Markes of ten
student. If name of groyp is “marks”,then the mark of 6th student is accessed by
mentioning the position 6 in the group “ marks”

There three types of arrays:


(1) One dimensional arrays
(2) Two dimensional arrays
(3) Multi dimensional arrays

One dimensional Arrays:

The array represents a group of similar values under a single name. The format of the one-
dimensional array is as follows:

type arrary_name
[size];
For Example,
int a[10];
Where a is the name of array of 10 integers.

For Example:

So far, we've been declaring simple variables: the declaration


int i;
declares a single variable, named i, of type int. It is also possible to declare an array of
several elements. The declaration
int a[10];
declares an array, named a, consisting of ten elements, each of type int. Simply speaking,
an array is a variable that can hold more than one value. You specify which of the several
values you're referring to at any given time by using a numeric subscript. (Arrays in

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 48 ~
InfoSoft Computer Education
Building Successful Careers.
programming are similar to vectors or matrices in mathematics.) We can represent the
array a above with a picture like this:

In C, arrays are zero-based: the ten elements of a 10-element array are numbered from 0
to 9. The subscript which specifies a single element of an array is simply an integer
expression in square brackets. The first element of the array is a[0], the second element
is a[1], etc. You can use these ``array subscript expressions'' anywhere you can use the
name of a simple variable, for example:

a[0] = 10;
a[1] = 20;
a[2] = a[0] + a[1];

Notice that the subscripted array references (i.e. expressions such as a[0] and a[1]) can
appear on either side of the assignment operator.
The subscript does not have to be a constant like 0 or 1; it can be any integral expression.
For example, it's common to loop over all elements of an array:
int i;

for(i = 0; i < 10; i = i + 1)

a[i] = 0;

This loop sets all ten elements of the array.


Arrays are a real convenience for many problems, but there is not a lot that C will do with
them for you automatically. In particular, you can neither set all elements of an array at
once nor assign one array to another; both of the assignments
a = 0; /* WRONG */
and
int b[10];
b = a; /* WRONG */
are illegal.
To set all of the elements of an array to some value, you must do so one by one, as in the
loop example above. To copy the contents of one array to another, you must again do so
one by one:

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 49 ~
InfoSoft Computer Education
Building Successful Careers.
int b[10];
for(i = 0; i < 10; i = i + 1)
b[i] = a[i];
Remember that for an array declared
int a[10];
there is no element a[10]; the topmost element is a[9]. This is one reason that zero-based
loops are also common in C. Note that the for loop
for(i = 0; i < 10; i = i + 1)
...
Does just what you want in this case: it starts at 0, the number 10 suggests (correctly)
that it goes through 10 iterations, but the less-than comparison means that the last trip
through the loop has i set to 9. (The comparison i <= 9 would also work, but it would be
less clear and therefore poorer style.)

In the little examples so far, we've always looped over all 10 elements of the sample array
a. It's common, however, to use an array that's bigger than necessarily needed, and to use
a second variable to keep track of how many elements of the array are currently in use.
For example, we might have an integer variable
int na; /* number of elements of a[] in use */
Then, when we wanted to do something with a (such as print it out), the loop would run
from 0 to na, not 10 (or whatever a's size was):
for(i = 0; i < na; i = i + 1)
printf("%d\n", a[i]);

Naturally, we would have to ensure ensure that na's value was always less than or equal
to the number of elements actually declared in a.

Arrays are not limited to type int; you can have arrays of char or double or any other type.

Two –dimensional Array:

Two- dimensional array are used to represent the two –dimensional structure like
matrix,tables etc. The two-dimensional array is declared as :

Type array_name[row_size]
[column_size];

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 50 ~
InfoSoft Computer Education
Building Successful Careers.

Multi-dimensional array:

The C programming also allows the use of the three or more dimensional array which are
known as multi-dimensional arrays. The general format for declaration of the multi-
dimensional arrays is as follows.

Types array_name[d1][d2][d3]

Where d1 is the 1th dimension. for Example,


int sales[3][4][12];

What is a String?

A string is a sequence of characters. string is useful to store the nin-numerical quantities


such as name,address etc. The C stores the characters ASCII values in memory.Each of the
spical character called null character and denoted as ‘\0’ NULL For Example,
String “Hello” is stored as :

‘h’ ‘e’ ‘l’ ‘l’


‘o’ ‘\0’

String Function:

The c does not support operation on string directly except read and print.to perform such
operations such operations either user should write the required code or uses the library
functions
Provided by #include<string.h>.Some function of the string are:

Function Meaning
Strlen(s) Computes length of string s
Strrev(s) Reverse the string s
Strcpy(s1,s2) Copies string s2 into s1
Strcat(s1,s2) Concate s2 at the end of s1
Strcmp(s1,s2) Compares s1 and s2. Returns 0 if
s1=s2,returns positive
If s1>s2 and returns negative if s1<s2

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 51 ~
InfoSoft Computer Education
Building Successful Careers.

For Example,

Since C never lets us assign entire arrays, we use the strcpy function to copy one string to
another:

#include <string.h>

char string1[] = "Hello, world!";


char string2[20];

strcpy(string2, string1);

The destination string is strcpy's first argument, so that a call to strcpy mimics an
assignment expression (with the destination on the left-hand side). Notice that we had to
allocate string2 big enough to hold the string that would be copied to it. Also, at the top of
any source file where we're using the standard library's string-handling functions (such as
strcpy) we must include the line

#include <string.h>

Which contains external declarations for these functions.

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 52 ~
InfoSoft Computer Education
Building Successful Careers.

Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 53 ~
InfoSoft Computer Education
Building Successful Careers.
Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 54 ~
InfoSoft Computer Education
Building Successful Careers.
Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 55 ~
InfoSoft Computer Education
Building Successful Careers.
Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 56 ~
InfoSoft Computer Education
Building Successful Careers.

Chapter 5 : Function
Function:

• A Function is one form of the sub=program or subroutine.


• Function itself is selfcontained program which implements a small but independent
task.
• It is not an independent program,but it works as a part of program.

Defining function:

The format of function is as follows :


Return_type name(argument)
{
Body;
}
Here return_type shows the type of value function returns. If function does not
return any value,but returns only control, then the return_type is void. the name
is the name of the function.

Return statement:

The format of the return statement is as follows.


Return;
Or
Return(expression);

The first form of the return does not return any value, but returns only control to the
caller. Whene return type of the function is void and we want to return control from the
middle of the function, this from is very useful.
Void fun()
{
.....
.....
If( . . . )
Return;
....

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 57 ~
InfoSoft Computer Education
Building Successful Careers.
....
}

FOR EXAMPLE :

#include<stdio.h>
#include<conio.h>
Int main(int ,int);
Void main()
{
Int x ,y,z;
Printf (“enter values of x and y”);

Z=max(x ,y)

Scanf(“%d /n ” z);
}

Int max(int a, int b)


{
Return(a > b ? a : b);
}
Whene function returns some vaiue , we use the seconed form of the return. In this
example the function max() returns integer
Return(a>b?a:b);
Is written , where a>b?a:b finds the maximua of a and b which is returned by return
statement.

Call by value

When arguments passed by a value , the calls fuctions creates a new variables of the
same type as argument and copies the actual argument passed from the main value into
it, which is called dummy argument. So by using call by value,duplicate copy of original
variable is created, which will take space in the memory,so it will waste the space.

For example , call by value method

Main()
{
Int a,b,c,sum;

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 58 ~
InfoSoft Computer Education
Building Successful Careers.
!
!
Sum=calsum(a,b,c);
!
!
}
Int calsum(intx,inty,intz)
{
!
!
!
}

Call by reference

Call by reference method,from the main function , instead of passing a value ,its adderss
is mpassed and at calls function receive the address by using pointer varisble which will
gives the value at that address,so no duplication occurs. Soc save the space in the
memory will using Call by reference method.

For example , Call by reference method:

Main()
{
Int a,b,c;
!
!
Sum=calsum(&A,&b,&c);
!
!
}
Int calsum(int*x ,int*y ,int*z)
{
!
!
!
}

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 59 ~
InfoSoft Computer Education
Building Successful Careers.
Function and Array :

The array can be passed to function as argument as like individual variables like a char ,
an integer , a double etc. The array can be passed to function by writing oniy array name
in function call.
Following function call shows how an array is passed as function argument.

Z=max_n(a,n);

In this function call , a is an array of n integer. The array a is passed as first argument
while the size of the array is passed as second argument to the function max_n0. The
function max_n0 is written as follows.
Int max_n(int v[], int size)
{
Int I,max;
Max=v[0];
for(i=0;i<size;i++)
{

}
}

Preprocessor:-
We have already used #inclued and #define in the programs. This are Preprocessor
Directives. The Preprocessor is one of the phases in compilation process which
performs its tasks just before compiles the program. The Preprocessor directive
starts always with # character in first column of line in program.
The Preprocessor performs mainly three types of expansions as mentioned below.

1. File inclusion
2. Macro substitution
3. Conditional inclusion

File inclusion : The file inclusion directive starts from #include. The format of the
#include is as follows.
#include<file_name>
Or
#include”file_name”

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 60 ~
InfoSoft Computer Education
Building Successful Careers.
Where file_name is the name of file to be included.

Macro substitution: Macro substitution means to replace the macro by the macro string.
Macro start with the #defines. The simple macro is defined as follows.
#define macro_name string

Conditional inclusion: The conditional inclusion is used to avoid the inclusion the
inclusion of the same file more than once in a program.

For example , the header file mts.h is written as follows.


#include MTS
#define MTS
....
....
/* content of the mts.h */
....
....
#endif

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 61 ~
InfoSoft Computer Education
Building Successful Careers.
Programs :-

Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 62 ~
InfoSoft Computer Education
Building Successful Careers.

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 63 ~
InfoSoft Computer Education
Building Successful Careers.
Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 64 ~
InfoSoft Computer Education
Building Successful Careers.
Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 65 ~
InfoSoft Computer Education
Building Successful Careers.
Chapter 6 : Poniter
Introduction:

Poniter are very useful in accessing memory outside the function, passing function
parameters to allow two-way communication between calling and called functions,
dynamic allocation of memory for various simple to compex data structures, hardware
device programming etc…

Every variable declare in C program is given space in computer memory according to its
size.For Example, An integer variable occupies two bytes as it is 16-bit in size.

What is pointer ?

A pointer is also a variable Which stores address of another variable. Once you get the
address of variable in pointer, it can be used for different purpose like to access the
variable value at that address , to increment it two that the address of next element if
original variable is of array type etc… the meaning is that ,once pointer is initialize by
address of some variable , pointer can be used for our advantage. Before a pointer is
used , it must be first declared and then initialized. A pointer variable is declared as
follows:-

Ptr_type

In above , following are important

(1) Ptr_type denotes type of pointer and is data type of the variable to which
pointer can point.
(2) * denotes that it is pointer variable .
(3) Pointer_name is name of pointer variable which can be any valid identifier .

As an Example,
int *ptr;
declares pointer variable with name ptr of type integer. Type of pointer means
variable type to which pointer pointes. Here,ptr is of type integer means it can
point to integer variable only.
Following are some more Examples of pinter declaration:-
float *f; // f is pointer to float.
long int * l; //l is pointer to long integer.

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 66 ~
InfoSoft Computer Education
Building Successful Careers.
char *c; // C is pointer to character.

Command Line Argments:


Program can also accept input values through command line arguments when it is started.
for Example,Whwn turbo C started as :
C:\tc>tc test.c
On MS-DOS,tc is a program which takes test.c as command line argument and when tc
window opens ,it opens file test .c immediately. another example of command line
argument is DOS copy command written as follows:
C:\>copy file.c file2.c
Where copy command takes two files names, source file name (file1.c) and
destination file name(file2.c) as arguments from command line. In order to pass input to C
program through command line argument s,we have to use arguments or parameters to
main ( ) is the first function call when C program start it has two arguments shown below:
Void Main (int argc,char *argv[ ] )
The first argument is args knows as arguments count which tells how many
arguments are passed.the second argument argv which is array of character pointer
s.pointers pointing to argument string.in case of Example of DOS copy command ,argc=3
and argv is as follows:

Orgv: Copy’\0’
File1.c’\0’

File2’\0’

For example,

Write a program which simply displays(echoes) its arguments.


\* write this program in file echo.c */
#include<stdio.h>
#include<conio.h>
Void main(int argc, char *argv[] )
{
Int I;
For( i=1; i<argc; i++)

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 67 ~
InfoSoft Computer Education
Building Successful Careers.
Printf(“%s”,argv[i]);
}

Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 68 ~
InfoSoft Computer Education
Building Successful Careers.
Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 69 ~
InfoSoft Computer Education
Building Successful Careers.
Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 70 ~
InfoSoft Computer Education
Building Successful Careers.
Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 71 ~
InfoSoft Computer Education
Building Successful Careers.
Chapter 7 : Structures
Introduction:

• Structure is a collection of different types of variables under a single name which


is used to represent logically related data pertaining to same entity or object.
• The structure is a very powerful and important tool which fulfills requirements of
complex problems.
• A simple example of a structure is information about employees in payrool or
information about students.
• Structure allocates separate memory for each of its variables, while union
allocates memory for only one and shared by al of its members.Hence,union is
not a group of variables, but it is a variable,which can take different type.

Defining structures and structure variables :

A structureis a group of variables of different types.The general format to


define a structure is given as:

struct struct_tag
{
data_type member_variable1;
data_type member_variable2;
.
.
};
In above , world struct is a reserve word and used to defined a structure template.

Struct-tag is an optional tag which provides type name to the structure and usefulin
creation of variables of the structure type. The member of structures and enclosed in pair
of { } .Each member consist of a type and variable name followed by semicolon (;). the
definition is ended with semicolon (;) . Note that such structure definition just declares a
template or prototype of a structure varicble,but does not occupy any
memory.Hence,Word declaration is more appropriate.

Consider following Example of a structure ,defining a student record:-

Struct student

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 72 ~
InfoSoft Computer Education
Building Successful Careers.
{
Int roll_no;
Char name[20];
Int age;
} ;

In above example, word student is representing a tag. There are three members as :
(1) Roll number,integer type
(2) Name of student, string type
(3) Age in years,integer type

Accessing structure members:

Member of a structure can be accessed and used individually in a problem. Following


syntax is used to access thse members of a structure.

Structure_name.membe
r_name
Structure_name is the name of structure variable and used member_name is the name of
variable which is member of structure member operator”.”(dot) conntects member name
to structure name.

For example,

S1.age
Denotes value of variable age of a structure of variable s1 which is of type student.
Same way s2.age denotes age value associated with student s2. The s1.age ors2.sge
are just like any other integer variable and can be used freely in expression. The following
statement assigns 20 to s1.age
S1.age =20;
The roll-number of student s1 can be read from keyword as
Scanf(“%d”,&s1.age);

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 73 ~
InfoSoft Computer Education
Building Successful Careers.

Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 74 ~
InfoSoft Computer Education
Building Successful Careers.
Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 75 ~
InfoSoft Computer Education
Building Successful Careers.
Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 76 ~
InfoSoft Computer Education
Building Successful Careers.
Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 77 ~
InfoSoft Computer Education
Building Successful Careers.
Chapter 8 : File Management

File Management function :

The file management function are defined in the header file <stdio.h>. They are available
in the program by including the header file <stdio.h>. Table gives the list of the file
management function which are common in use. The list includes functions for performing
the following operation on files.

• Opening a file.
• Closing a file .
• Reading from a file(both ASCII and binary).
• Writing to a (both ASCII and binary).
• Error handing functions.
• Functions to implement random files.

Function Operation
fopen() Open a file
Fclose() Close afile
Getc() Rrad a character from file
Putc() Write a character into file
Fscanf() Read from a file
Fprintf() Write into file
Getw() Read an integer from file
Putw() Write object from a file
Fread() Read object from file
Fwrite() Write object in file
Feof() Indicates end of file
Ferror() Indicates error in file
Fseek() processing
Ftell() Move to the desired
Rewined() position in file
Tells the current position
in file
Move to the beginning of
file

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 78 ~
InfoSoft Computer Education
Building Successful Careers.

Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 79 ~
InfoSoft Computer Education
Building Successful Careers.
Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 80 ~
InfoSoft Computer Education
Building Successful Careers.
Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 81 ~
InfoSoft Computer Education
Building Successful Careers.
Programs :-

Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 82 ~
InfoSoft Computer Education
Building Successful Careers.

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 83 ~
InfoSoft Computer Education
Building Successful Careers.
Chapter 9:
History of C++

In 1970, Ken Thompson, immersed in the development of UNIX at Bell Labs, created the B
language. It was a port of BCPL for a specific machine and system (DEC PDP-7 and UNIX),
and was adapted to his particular taste and necessities.

In 1973, Dennis Ritchie, had developed the basis of C.

The inclusion of types, its handling, as well as the improvement of arrays and pointers,
along with the later demonstrated capacity of portability without becoming a high-level
language, contributed to the expansion of the C language.

It was established with the book "The C Programming Language" by Brian Kernighan and
Dennis Ritchie, known as the White Book.

About C++ Language

• What is C++?

C++ is a programming language. It literally means "increased C", reflecting its


nature as an evolution of the C language.

• Is it necessary to already know another programming language before learning C+


+?

Not necessarily.

C++ is a simple and clear language in its expressions.

• What is OOP: Object-oriented programming?

It is a programming model that treats programming from a perspective where each


component is considered an object, with its own properties and methods, replacing
or complementing structured programming paradigm, where the focus was on
procedures and parameters.

• Is C++ a proprietary language?

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 84 ~
InfoSoft Computer Education
Building Successful Careers.
No. No one owns the C++ language. Anyone can use the language royalty-free.

Programming languages
A programming language is a set of instructions and a series of lexical conventions
specifically designed to order computers what to do.

Why C++?
C++ has certain characteristics over other programming languages. The most
remarkable are:

• Object-oriented programming
• Modular programming
• C Compatibility
• C++ is backwards compatible with the C language. Any code written in C can
easily be included in a C++ program without making any change.
• Speed

Basics of C++

Structure of C++ program :

Basic input/output: cout , cin.


Comments
Operator : unary, binary
Conditional: if , else ? .
Loops : for ,while
Array
Functions , call by value, prototypes ,header files.

Program in C++.

#include <iostream.h>
#include<conio.h>
void main ()
{
cout << "Hello World!";
return 0;
}

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 85 ~
InfoSoft Computer Education
Building Successful Careers.

Output: Hello World!

1) #include <iostream>

In this case the directive #include <iostream> tells the preprocessor to include the
iostream standard file.
This specific file (iostream) includes the declarations of the basic standard input-
output library in C++, and it is included because its functionality is going to be used
later in the program.

2) int main ()

This line corresponds to the beginning of the definition of the main function.
The main function is the point by where all C++ programs start their execution,
independently of its location within the source code.
For that same reason, it is essential that all C++ programs have a main function.
The word main is followed in the code by a pair of parentheses (()).

3) cout << "Hello World!";

Cout is a pre-defind instance of the stream ostream-much more later.


<< is the insertion operator : it inserts what follows into the stream
Cout.
“Hello world” is the string we want to output.
endl does 2 things :
i. it writes a <CR>.
ii. It flushes the output buffer.
all statement in C++ must end with ‘;’.

4) return 0;

The return statement causes the main function to finish.


Since main isof type int , it must return an int to the program
That called it.
a return value of ‘0’ signifies successful completion.

5) }

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 86 ~
InfoSoft Computer Education
Building Successful Careers.
Finally,we mark the end of the main code block.

Comment :

Comments are parts of the source code disregarded by the compiler. They simply do
nothing.
Their purpose is only to allow the programmer to insert notes or descriptions embedded
within the source code.

C++ supports two ways to insert comments:


//line comment
/*blockcomment*/

The first of them, known as line comment, discards everything from where the pair of
slash signs (//) is found up to the end of that same line. The second one, known as block
comment, discards everything between the /* characters and the first appearance of the */
characters, with the possibility of including more than one line.

We are going to add comments program:


/* my second program in C++
with more comments */

#include <iostream>
using namespace std;

int main ()
{
cout << "Hello World! "; // prints Hello World!
cout << "I'm a C++ program"; // prints I'm a C++ program
return 0;
}

Output :

Hello World! I'm a C++ program

If you include comments within the source code of your programs without using the
comment characters combinations //, /* or */, the compiler will take them as if they were
C++ expressions, most likely causing one or several error messages when you compile it.
Operator : unary, binary

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 87 ~
InfoSoft Computer Education
Building Successful Careers.
Conditional: if , else ? .
Loops : for ,while
Array
Functions , call by value, prototypes ,header files.
You Can See In Programming in c.

Standard Output (cout)

The standard output of a program is the screen, and the C++ stream object defined to
access it is cout.
cout is used in conjunction with the insertion operator, which is written as << (two "less
than" signs).
For Example:

cout << "Hello"; // prints Hello


cout << Hello; // prints the content of Hello variable

The endl manipulator produces a newline character, exactly as the insertion of '\n' does,
but it also has an additional behavior when it is used with buffered streams: the buffer is
flushed

Standard Input (cin).

The standard input device is usually the keyboard. Handling the standard input in C++ is
done by applying the overloaded operator of extraction (>>) on the cin stream. The
operator must be followed by the variable that will store the data that is going to be
extracted from the stream.
For example:
int age;
cin >> age;
The first statement declares a variable of type int called age, and the second one waits for
an input from cin (the keyboard) in order to store it in this integer variable.

Another Example:
cin >> a >> b;
is equivalent to:
cin >> a;
cin >> b;

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 88 ~
InfoSoft Computer Education
Building Successful Careers.
In both cases the user must give two data, one for variable a and another one for variable
b that may be separated by any valid blank separator: a space, a tab character or a
newline.

Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 89 ~
InfoSoft Computer Education
Building Successful Careers.

Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 90 ~
InfoSoft Computer Education
Building Successful Careers.
Programs :-

Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 91 ~
InfoSoft Computer Education
Building Successful Careers.

Chapter 10:

Classes And Objects:

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 92 ~
InfoSoft Computer Education
Building Successful Careers.
A class is an expanded concept of a data structure: instead of holding only data, it can
hold both data and functions.
An object is an instantiation of a class. In terms of variables, a class would be the type,
and an object would be the variable.

Classes are generally declared using the keyword class, with the following format:
class class_name
{
access_specifier_1:
member1;
access_specifier_2:
member2;
...
}
object_names;

Where class_name is a valid identifier for the class, object_names is an optional list of names
for objects of this class. The body of the declaration can contain members, that can be
either data or function declarations, and optionally access specifiers.

All is very similar to the declaration on data structures, except that we can now include
also functions and members, but also this new thing called access specifier.
An access specifier is one of the following three keywords: private, public or protected.
These specifiers modify the access rights that the members following them acquire:

Private members of a class are accessible only from within other members of the same
class or from their friends.

Protected members are accessible from members of their same class and from their
friends, but also from members of their derived classes.

Finally, public members are accessible from anywhere where the object is visible.
By default, all members of a class declared with the class keyword have private access for
all its members. Therefore, any member that is declared before one other class specifier
automatically has private access.
For example:
class CRectangle
{
int x, y; //data member
public:

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 93 ~
InfoSoft Computer Education
Building Successful Careers.
void getdate (10,20);
{
int area (void);
}
}
Void main()
{
CRectangle rect;
}

Declares a class (i.e., a type) called CRectangle and an object (i.e., a variable) of this
class called rect.
This class contains four members: two data members of type int (member x and member
y) with private access (because private is the default access level) and two member
functions with public access: getdate() and area(), of which for now we have only included
their declaration, not their definition.

Notice the difference between the class name and the object name: In the previous
example, CRectangle was the class name , where as rect was an object of type CRectangle.
It is the same relationship int and a have in the following declaration:
int a;

where int is the type name (the class) and a is the variable name (the object).

After the previous declarations of CRectangle and rect, we can refer within the body of the
program to any of the public members of the object rect as if they were normal functions
or normal variables, just by putting the object's name followed by a dot (.) and then the
name of the member. All very similar to what we did with plain data structures before. For
example:
rect.getdate (3,4);
myarea = rect.area();

Here is the complete example of class ss:


#include<iostream.h>
#include<conio.h>
Class ss
{
Private:
int a,b;
public:
void getdate(int x,int y)

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 94 ~
InfoSoft Computer Education
Building Successful Careers.
{
a=x;
b=y;
}
Void putdata()
{
Cout<<”The value of a is:”<<a<<endl;
Cout<<”The value of b is:”<<b<<endl;

}
Void main()
{
Class ss s1;
Int p,q;
Clrscr();
Cout<<”enter the value of p:”;
Cin>>p;
Cout<<”enter the vaule of q”;
Cin>>q;
S1.getdata(p,q);
S1.putdata();
Getch();
}

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 95 ~
InfoSoft Computer Education
Building Successful Careers.
Programs :-

Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 96 ~
InfoSoft Computer Education
Building Successful Careers.

Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 97 ~
InfoSoft Computer Education
Building Successful Careers.

Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 98 ~
InfoSoft Computer Education
Building Successful Careers.

Chapter 11:
Constructors and destructors
Constructors

A class can include a special function called constructor, which is automatically called
whenever a new object of this class is created. This constructor function must have the
same name as the class, and cannot have any return type; not even void.
We are going to implement data including a constructor:
#include <iostream.h>

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 99 ~
InfoSoft Computer Education
Building Successful Careers.
#include <conio.h>
Class data
{
Private:
Int a,b;
Public:
Data()
{
A=10;
B=20;
}
Data(int x)
{
A=x;
B=y;
}
Data(int x,int y)
{
A=x;
B=y;
}
Void putdata()
{
cout<<”the value of a is:”<<a<<endl;
cout<<”the value of b is:”<<b<<endl;
}
Void main()
{
Int p,q;
Clrscr();
Cout<<”Enter the value of p &q”;
Cin>>p>>q;
Data d1=datas(p);
Data d2=datas(p,q);
Data d3=datas();
d1.putdata();
d2.putdata();
d3.putdata();
getch();
}

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 100 ~
InfoSoft Computer Education
Building Successful Careers.

You can also see how neither the constructor prototype declaration (within the class) nor
the latter constructor definition include a return value; not even void.

Destructors

The destructor fulfills the opposite functionality. It is automatically called when an object is
destroyed, either because its scope of existence has finished (for example, if it was
defined as a local object within a function and the function ends) or because it is an object
dynamically assigned and it is released using the operator delete.

The destructor must have the same name as the class, but preceded with a tilde sign (~)
and it must also return no value.

The use of destructors is especially suitable when an object assigns dynamic memory
during its lifetime and at the moment of being destroyed we want to release the memory
that the object was allocated.
// example on constructors and destructors
#include <iostream.h>
#include <conio.h>
class CRectangle
{
int *width, *height;
public:
CRectangle (int,int);
~CRectangle ();
int area () {return (*width * *height);
}
};

CRectangle::CRectangle (int a, int b)


{
width = new int;
height = new int;
*width = a;
*height = b;
}

CRectangle::~CRectangle () {
delete width;
delete height;

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 101 ~
InfoSoft Computer Education
Building Successful Careers.
}

int main () {
CRectangle rect (3,4), rectb (5,6);
cout << "rect area: " << rect.area() << endl;
cout << "rectb area: " << rectb.area() << endl;
return 0;
}
Output:
rect area: 12
rectb area: 30

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 102 ~
InfoSoft Computer Education
Building Successful Careers.
Programs :-

Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 103 ~
InfoSoft Computer Education
Building Successful Careers.

Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 104 ~
InfoSoft Computer Education
Building Successful Careers.

Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 105 ~
InfoSoft Computer Education
Building Successful Careers.

Chapter 12:

The keyword this

The keyword this represents a pointer to the object whose member function is being
executed. It is a pointer to the object itself.

One of its uses can be to check if a parameter passed to a member function is the object
itself. For example,
#include<conio.h>

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 106 ~
InfoSoft Computer Education
Building Successful Careers.
#include<iostream.h>
Class test
{
Private:
Int x;
Public:
Void putdata();
{
This->x=50;
Cout<<”the value of x is:”<<this->x;
}};
Void main()
{
Clrscr();
TEST P;
p.putdata();
Getch();
}
Output:- The value of x is:50

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 107 ~
InfoSoft Computer Education
Building Successful Careers.
Programs :-

Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 108 ~
InfoSoft Computer Education
Building Successful Careers.

Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 109 ~
InfoSoft Computer Education
Building Successful Careers.

Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 110 ~
InfoSoft Computer Education
Building Successful Careers.

Chapter 13:

Friend functions

In principle, private and protected members of a class cannot be accessed from outside
the same class in which they are declared. However, this rule does not affect friends.

Friends are functions or classes declared with the friend keyword.

If we want to declare an external function as friend of a class, thus allowing this function

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 111 ~
InfoSoft Computer Education
Building Successful Careers.
to have access to the private and protected members of this class, we do it by declaring a
prototype of this external function within the class, and preceding it with the keyword
friend:
// friend functions
#include <iostream.h>
using namespace std;

class CRectangle
{
int width, height;
public:
void set_values (int, int);
int area ()
{
return (width * height);
}
friend CRectangle duplicate (CRectangle);
};

void CRectangle::set_values (int a, int b)


{
width = a;
height = b;
}

CRectangle duplicate (CRectangle rectparam)


{
CRectangle rectres;
rectres.width = rectparam.width*2;
rectres.height = rectparam.height*2;
return (rectres);
}

int main ()
{
CRectangle rect, rectb;
rect.set_values (2,3);
rectb = duplicate (rect);
cout << rectb.area();
return 0;
}

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 112 ~
InfoSoft Computer Education
Building Successful Careers.

Output:

24

The duplicate function is a friend of CRectangle. From within that function we have been
able to access the members width and height of different objects of type CRectangle, which
are private members. Notice that neither in the declaration of duplicate() nor in its later use
in main() have we considered duplicate a member of class CRectangle. It isn't! It simply has
access to its private and protected members without being a member.

The friend functions can serve, for example, to conduct operations between two different
classes. Generally, the use of friend functions is out of an object-oriented programming
methodology, so whenever possible it is better to use members of the same class to
perform operations with them. Such as in the previous example, it would have been
shorter to integrate duplicate() within the class CRectangle.

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 113 ~
InfoSoft Computer Education
Building Successful Careers.
Chapter 14:

Inheritance

A key feature of C++ classes is inheritance.

Inheritance allows to create classes which are derived from other classes, so that they
automatically include some of its "parent's" members, plus its own.

For example, we are going to suppose that we want to declare a series of classes that
describe polygons like our CRectangle, or like CTriangle. They have certain common
properties, such as both can be described by means of only two sides: height and base.

This could be represented in the world of classes with a class CPolygon from which we
would derive the two other ones: CRectangle and CTriangle.

The class CPolygon would contain members that are common for both types of polygon.
In our case: width and height.
And CRectangle and CTriangle would be its derived classes, with specific features that are
different from one type of polygon to the other.

Classes that are derived from others inherit all the accessible members of the base class.
That means that if a base class includes a member A and we derive it to another class with
another member called B, the derived class will contain both members A and B.

In order to derive a class from another, we use a colon (:) in the declaration of the derived
class using the following format:

class derived_class_name: public base_class_name

{ /*...*/ };

Where derived_class_name is the name of the derived class and base_class_name is the name
of the class on which it is based. The public access specifier may be replaced by any one of
the other access specifiers protected and private. This access specifier limits the most
accessible level for the members inherited from the base class: The members with a more
accessible level are inherited with this level instead, while the members with an equal or
more restrictive access level keep their restrictive level in the derived class.

// derived classes

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 114 ~
InfoSoft Computer Education
Building Successful Careers.
#include <iostream>
using namespace std;

class CPolygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b;}
};

class CRectangle: public CPolygon {


public:
int area ()
{ return (width * height); }
};

class CTriangle: public CPolygon {


public:
int area ()
{ return (width * height / 2); }
};

int main () {
CRectangle rect;
CTriangle trgl;
rect.set_values (4,5);
trgl.set_values (4,5);
cout << rect.area() << endl;
cout << trgl.area() << endl;
return 0;
}
Output:
20
10

We can summarize the different access types according to who can


access them in the following way:

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 115 ~
InfoSoft Computer Education
Building Successful Careers.
Access public protected private
members of the same yes yes yes
class
members of derived yes yes no
classes
not members yes no no

Where "not members" represent any access from outside the class, such as from main(),
from another class or from a function.

This is because we have used the public keyword to define the inheritance relationship on
each of the derived classes:
class CRectangle: public CPolygon { ... }
This public keyword after the colon (:) denotes the most accessible level the members
inherited from the class that follows it (in this case CPolygon) will have. Since public is the
most accessible level, by specifying this keyword the derived class will inherit all the
members with the same levels they had in the base class.
If we specify a more restrictive access level like protected, all public members of the base
class are inherited as protected in the derived class. Whereas if we specify the most
restricting of all access levels: private, all the base class members are inherited as private.

For example, if daughter was a class derived from mother that we defined as:

class daughter: protected


mother;

This would set protected as the maximum access level for the members of daughter that it
inherited from mother. That is, all members that were public in mother would become
protected in daughter. Of course, this would not restrict daughter to declare its own public
members. That maximum access level is only set for the members inherited from mother.

If we do not explicitly specify any access level for the inheritance, the compiler assumes
private for classes declared with class keyword and public for those declared with struct.

What is inherited from the base class?


In principle, a derived class inherits every member of a base class except:
its constructor and its destructor
its operator=() members
its friends

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 116 ~
InfoSoft Computer Education
Building Successful Careers.
Although the constructors and destructors of the base class are not inherited themselves,
its default constructor (i.e., its constructor with no parameters) and its destructor are
always called when a new object of a derived class is created or destroyed.

If the base class has no default constructor or you want that an overloaded constructor is
called when a new derived object is created, you can specify it in each constructor
definition of the derived class:

derived_constructor_name (parameters) : base_constructor_name (parameters) {...}

Multiple inheritance

In C++ it is perfectly possible that a class inherits members from more than one class.
This is done by simply separating the different base classes with commas in the derived
class declaration. For example, if we had a specific class to print on screen ( COutput) and
we wanted our classes CRectangle and CTriangle to also inherit its members in addition to
those of CPolygon we could write:
class CRectangle: public CPolygon, public COutput;
class CTriangle: public CPolygon, public COutput;

Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 117 ~
InfoSoft Computer Education
Building Successful Careers.

Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 118 ~
InfoSoft Computer Education
Building Successful Careers.

Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 119 ~
InfoSoft Computer Education
Building Successful Careers.

Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 120 ~
InfoSoft Computer Education
Building Successful Careers.

Chapter 15:

Polymorphism

Before getting into this section, it is recommended that you have a proper understanding
of pointers and class inheritance.
If any of the following statements seem strange to you, you should review the indicated
sections:

Statement: Explained in:


int a::b(int c) { Classes
}

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 121 ~
InfoSoft Computer Education
Building Successful Careers.
a->b Data Structures
class a: public Friendship and
b { }; inheritance
Virtual members

A member of a class that can be redefined in its derived classes is known as a virtual
member.
In order to declare a member of a class as virtual, we must precede its declaration with
the keyword virtual:

Abstract base classes

Abstract base classes are something very similar to our CPolygon class of our previous
example. The only difference is that in our previous example we have defined a valid
area() function with a minimal functionality for objects that were of class CPolygon (like the
object poly), whereas in an abstract base classes we could leave that area() member
function without implementation at all. This is done by appending =0 (equal to zero) to the
function declaration.

An abstract base CPolygon class could look like this:


// abstract class CPolygon
class CPolygon
{
protected:
int width, height;
public:
void set_values (int a, int b)
{
width=a; height=b;
}
virtual int area () =0;
};
Notice how we appended =0 to virtual int area () instead of specifying an implementation
for the function. This type of function is called a pure virtual function, and all classes that
contain at least one pure virtual function are abstract base classes.

The main difference between an abstract base class and a regular polymorphic class is
that because in abstract base classes at least one of its members lacks implementation we

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 122 ~
InfoSoft Computer Education
Building Successful Careers.
cannot create instances (objects) of it.

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 123 ~
InfoSoft Computer Education
Building Successful Careers.
Chapter 16:

Templates

Function templates:
Function templates are special functions that can operate with generic types. This allows
us to create a function template whose functionality can be adapted to more than one
type or class without repeating the entire code for each type.

In C++ this can be achieved using template parameters. A template parameter is a


special kind of parameter that can be used to pass a type as argument: just like regular
function parameters can be used to pass values to a function, template parameters allow
to pass also types to a function. These function templates can use these parameters as if
they were any other regular type.

Exceptions
Exceptions provide a way to react to exceptional circumstances (like runtime errors) in our
program by transferring control to special functions called handlers.

To catch exceptions we must place a portion of code under exception inspection. This is
done by enclosing that portion of code in a try block. When an exceptional circumstance
arises within that block, an exception is thrown that transfers the control to the exception
handler. If no exception is thrown, the code continues normally and all handlers are
ignored.

A exception is thrown by using the throw keyword from inside the try block.

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 124 ~
InfoSoft Computer Education
Building Successful Careers.
Programs :-

Programs :-

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 125 ~
InfoSoft Computer Education
Building Successful Careers.

RAOPURA: - 112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.
SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.
ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.
 0265 – 6507526 / 9227160174 / 9227853100
~ 126 ~

Вам также может понравиться