You are on page 1of 334

Lenguaje C

C Introduction
Algorithm:

This is a step-by-step method to solve a particular problem.

Flow Chart:

This is a pictorial representation for flow of information.

Program:

This is a series of meaningful instructions written in a particular order to do a specific task.

Why do we need Programs?

Computer is a machine, it cannot think and act by it's own as the human being does. So we
need to tell it what to do and how to do it. The series of meaningful instructions written in a
particular order to carry out a specific task which is known as a program. Thus the
programs are written in some languages and such languages are known as programming
languages.

Programming Languages:

Basically there are different categories of programming languages.

1. Machine level language


2. Assembly level language
3. Higher level language

1) Machine level Language:

The program written using the binary digits specified for a particular processor is known as
the machine language of the computer. It consists of series of binary digits i.e. 1's and 0's
and this is the language the processor understands without any interfaces.

2) Assembly level language:


This language uses the mnemonics or abbreviations, symbols etc. to represent an instruction
to a specific processor. The programs written in this language is converted into it's machine
level language equivalent by a software (program) known as an Assembler.

3) Higher level language:

These languages are machine independent languages.

These use simple English words to represent each of the instruction.


These are developed to allow programs to be run on a variety of computers.
A single statement of high-level language may be translated into many statements in its
machine level language equivalent.
The programs written in high level language is translated into it's machine level language
equivalent by the translator programs known as Interpreter and Compiler

Assembler:

This is a program which translates the program written in assembly level language into it's
machine level language equivalent.

Interpreter:

This is a program which translates a program written in higher level language into machine
level language equivalent, one statement at a time and executes it immediately.

Compiler:

This is a program which translates the entire program into it's machine level language
equivalent and then executes it.

Historical developments of C

Because of the outgrowth of two earlier languages called BCPL (Basic Combined
Programming Language) and B the language C emerged. In 1970, this language was
developed by Dennis Ritchie at Bell Telephone Laboratory Inc., now known as AT&T Bell
Laboratories Inc. Brian Kernighan and Ritchie published a definite description about C in
1978.

C is often called as miple level language. Since it is designed to have relatively faster
program execution as compared to Assembly and Machine level language which are known
as low level language. It also has relatively faster program development feature as
compared to higher-level language.

Syntaxes and Semantics

Every language has a grammer and meaning. Thus C Programming language also has its
grammer and meaning. The grammer of C is called as syntax and meaning of the language
is called as semantic. So to learn C language we have to learn the grammer of C then using
this language useful programs can be writtern.

C Tokens:

The smallest individual units of a C program are called as C tokens.

Following are the six C tokens

1. Keywords: int, float,for etc.


2. Identifiers: area, radius,main etc.
3. Constants: 75, 55.8 etc.
4. Strings: "Hello" etc.
5. Special Symbols: [ ], _, etc.
6. Operators: %, /, + etc.

The C character Set:

C language has its characters categorized into following groups.

Alphabets:

a to z, A to Z
Digits :

0 to 9

Special Symbols and operators:

~ ' ! @ # % ^ & * ( ) -_ + / = \ | { } [ ] : ; " ' < > , .?

White spaces:

Blank spaces, Horizontal tab, Carriage return, Form Feed, Newlines

Keywords:

These are the words whose meaning is already been explained to C compiler. Keywords
cannot be used as variable names and if we try to do so that means we are trying to assign
new meaning to the key word and it is not allowed.

The following are the 32 Keywords used in C Programming:

auto break case char const continue


default do double else enum extern
float far for goto if int
long near register return short signed
static struct switch typedef union unsigned
void while

Identifiers:
These refer to the names of variables, arrays and functions. These are the user given names.
It can consist of digits, characters and special symbols. Both lowercase and uppercase
characters can be used.

Constant:

This is a quantity that will not change. This quantity can be stored in a memory location
and a name can be given to it.

Strings:

These are the group of characters enclosed within the double quotation marks. The
characters may be letters, digits, special symbols and space.

Special Symbols:

These are symbols used in a C statement. For example underscore symbol can be used to
join two words which make a function name or variable name.

Operators:

These are used in mathematical and logical operations.

Variables:

The alphabets, number and special symbols when properly combined will form variables.
This is a name given to a location in memory where some value can be stored which can be
altered also in the program.

Rules for constructing variable names:

1. A variable name is a combination of alphabets, digits or underscores.


2. The length of the variable name depends on the compiler and operating system.
3. The first character in a variable name must be an alphabet.
4. No commas or blanks are allowed within a variable name
5. No special symbol other than underscore is used in a variable name.
6. The variable names are case sensitive. That is variable name SALARY is not same
as variable name salary. The general convention is that lower case letters are used to
represent a variable name.

Example:

area, radius1, si_int

Data types:

When the value is required to be stored in a variable, we must first of all identify the kind
of data we are going to store in that variable and memory required by that data type.
Following is the table, which gives different basic (premitive) data types, their description,
memory required by them. (Detailed table is given in the Annexure.)

Data type Description Memory requirements Range of values


char Single character 1 byte -128 to 127
int Integer quantity 2 bytes -32768 to 32767
float Real quantity 4 bytes 3.4e-38 to 3.4e+38
double Real quantity 8 bytes 1.7e-308 to 1.7e+308

Basic structure of C program

A typical C program can have six sections. But few of the sections can be omitted if not
required.

1. Documentation Section
2. Link Section
3. Definition Section
4. Global Declaration Section
5. main( ) function Section
6. Sub program Section

1. Documentation Section:
This section is used to give details about the program, name of the program, name of the
author of the program. These are all used as comment lines. The comment lines are not
processed.

2. Link Section:

This section provides instructions to the compiler to include files so as to link functions
from the C library.

3. Definition Section:

All symbolic constants are defined in this section.

4. Global declaration Section:

If a variable is required to be used in different functions of a program then it is declared in


this section so that it is available to all the different functions of an entire program.

5. main( ) function Section:

Every C program must have this function. This function tells the beginning of the program.
main( ) contains two parts one is the declaration part and the other is the executable part. In
the declaration part all the variables used by the statements appearing in the executable part
are declared and the executable part contains all the executable statements. The declaration
part and executable part must appear in between the opening and closing brace. The
program execution begins at the opening brace and ends at the closing brace. All the
statements that appear in declaration part and executable part must end with a semicolon ( ;
).

6. Sub Program Section:

This section contains all the user defined functions that are called by the statements in the
main( ) function or the other user defined functions. Although these user defined functions
can be placed anywhere in the program, they are generally placed immediately after the
main( ) function.
Comment Lines:

The lines beginning with /* and ending with */ are known as comment lines. These lines
can be used any where in the program to enhance the readability and understanding of the
program. Comment lines are not executable statements and therefore anything between /*
and */ is ignored by the compiler.

C Library:

This defines certain functions and the programmer can make use of these to write efficient
programs. These library functions are defined in different header files. These files have the
extension as 'h'. Instead of programmer

writing instructions to do a specific task he can use already existing code written and placed
in the form of functions in the C library.

Now let us write our first C program using a library function printf() to display a message
on the monitor.

/* This is the First C program */


#include <stdio.h>
main()
{
printf(" Welcome to the wonder world of C\n");
}

In the above program the first line is the comment line.

The second line is used to include the header file stdio.h, because the function printf()
which is used in this program is defined in the header file stdio.h.

The third line indicates the beginning of the program with the function name main().

The fourth line is the open brace after which you can have executable statements.

The fifth line has a statement to print the message. Here the built in C library function
printf() is used.

The sixth line, which has a brace, indicates the end of the program.
Procedure to write and execute the program using Turbo C:

1) Invoke TC editor :

There are different methods to invoke TC editor. I will discuss one method. Let us assume
TC shortcut is present on your desktop. Double click on TC shortcut, you will get TC editor
which is shown in the figure 1.

Figure 1

2) Type the program

Type the above given program. To save the program Press Alt key and f key
simultaneously you will get a drop down menu as shown in Figure 2. Then select the save
option and then save it with a name given to it and extension as c,

for example T3.C.

Figure 2
3) Compile the program

The program written is required to be compiled so press Alt and c key simultaneously, you
will get a drop down menu as shown in Figure 3. From the resulting menu press c key. If
there are any errors it is shown in the menu and correct the errors, otherwise you will get a
screen which shows the compilation is successful and press any key

Figure 3
4. Execute the Program:
Now press Alt and r key simultaneously, you will get a drop down menu as shown in
Figure 4. From the resulting menu press r key. Program is executed and to see the result
press Alt and F5 key simultaneously. The result screen is displayed and it is shown in
Figure 5.
Figure 4:
Figure 5
Operators and Expressions:

In the program, Operators are used to manipulate data and variables using which
mathematical and logical expressions can be written. C operators can be classified into
different categories as shown below.

1) Arithmetic operators:

+, -, *, /, % (modulo division).

These operators are used to form arithmetic expressions.


Example: a * b, a % b
2) Relational operators:

> (greater than), < (less than), >= (greater than or equal to), <= (less than or equal to), = =
(equal to), != (Not equal to).
These operators are used to form the relational expressions.
Example: a >= b, c < d

3)Logical operators:

&& (Logical AND), || (Logical OR), ! (Logical NOT)

Example: a && b, c || d
4) Assignment operator:

This operator is used to assign some value to some variable.

It has the following format. Variable= value.


Example: a = 25; a= 65 * 6;
Short hand assignment operators are also available in this category, which has the following
syntax. Variable operator = expression. Example: Assume a value of 25 is assigned to the
variable a.
By using the short hand assignment operator a +=1 which means a= a + 1. So the new value
of a will be 26.
5) Conditional operators:

A ternary operator pair ?: is available in C to construct conditional expression of the form


Expression 1 ? Expression 2 : Expression 3;
The operator pair works as given below:
The entire expression is evaluated from left to right.
Expression 1 is evaluated first, if it is non zero (true) then Expression 2 is evaluated and
becomes the value of the expression. If Expression 1 is false (not true) Expression 3 is
evaluated and its value becomes the value of the expression. So only one of the Expressions
either Expression 2 or Expression 3 is evaluated and not the both during a given evaluation.

Now let us write a program, which will use the ternary operator pair.

/* Program to test ternary operator pair */


#include <stdio.h>
main()
{
int a, b, x;
a=5;
b=10;
x=(a>b) ? a:b;
printf(" Value of x is %d\n",x);
}

In the above program a, b and x are the integer variables because they are declared with the
key word int. Because of this declaration two bytes are reserved for each of the variables in
memory.

Figure showing the allocation of memory to variables


The value 5 is stored in memory location identified as a and value 10 is stored in memory
location identified as b. After the evaluation of the expression value 10 is stored in the
variable x. The expression is evaluated from left to right. If the value of a is greater than
value of b, value of a is stored in x otherwise value of b is stored in x. In our case since
value of a is not greater than b value of b that is 10 is stored in variable x, which is shown
in the figure given below.

Figure showing storage of values in memory locations

6)Bit wise operators:

These operators are used for manipulation of data at bit level. Following is the list of
bitwise manipulation operators.

1. Shift right (>>):


Syntax: operand1 >> operand2;
Shift right operator shifts bits of operand1 right by distance operand2.

2. Shift left (<<):


Syntax: operand1 << operand2;
Shift left operator shifts bits of operand1 left by distance operand2.
3. Shift right unsigned (>>>):
Syntax: operand1 >>> operand2;
Shift right operator unsigned shifts bits of operand1 right by distance operand2
(unsigned).

4. Bitwise AND ( &):


Syntax: operand1 & operand2;
AND operation is performed on operand1 and operand2 bitwise.

5. Bitwise OR ( | ):
Syntax: operand1 | operand2;
OR operation is performed on operand1 and operand2 bitwise.

6. Bitwise XOR ( ^ ):
Syntax: operand1 ^ operand2;
OR operation is performed on operand1 and operand2 bitwise.

7. Bitwise Compliment ( ~ ):
Syntax: ~operand1;
Bitwise compliment operation is performed on operand1.

Note:

In all cases Operand1 and Operand2 specifies the values. The following example
demonstrates the usage of all bitwise operators.

/* Program to test different bitwise operators*/

#include <stdio.h>
#include <conio.h>
void main()
{
int operand1,operand2,result;
printf("Enter Values for Shift operations\n");
printf("\nEnter the value for operand1:");
scanf("%d",&operand1);
printf("Enter the value for operand2:");
scanf("%d",&operand2);
result=operand1>>operand2;
printf("\nResult of bitwise Shift right operation is: %d\n",result);
result=operand1<<operand2;
printf("Result of bitwise Shift left operation is: %d\n",result);
printf("\nEnter Values for Logical operations\n");
printf("\nEnter the value for operand1:");
scanf("%d",&operand1);
printf("Enter the value for operand2:");
scanf("%d",&operand2);
result=operand1 & operand2;
printf("\nResult of bitwise AND operation is: %d\n",result);
result=operand1 | operand2;
printf("Result of bitwise OR operation is: %d\n",result);
result=operand1 ^ operand2;
printf("Result of bitwise XOR operation is: %d\n",result);
result=~operand1;
printf("Result of bitwise Compliment operation on operand1: %d\n",result);
}

Output:

7) Special Operators :

Comma operator, sizeof operator, pointer operator ( *, &) and member selection operator (.
, ->).

Comma operator:
The comma operator can be used to link the related expression together. A comma linked
lists of expression are evaluated left to right and the value of right most expression is the
value of the combined expression.

Example:

a=(x=10, y=15, x+y);

In the above example value 10 is assigned to x then the value 15 is assigned to y and the
apition of two values 10 and 15 is assigned to a. Since comma operator has the lowest
precedence of all operators the parenthesis is required.

The Sizeof operator: The sizeof is a compile time operator. When it is used with an
operand it returns the number of bytes the operand occupies. The operand may be a data
type qualifier, variable, or a constant.

Example:

n=sizeof(b);

n=sizeof(float);

In the above example sizeof operator will return the number of bytes allocated to the
variable b. So if b is an integer variable n will have the value 2 and if b is a float variable n
will have the value as 4. Similarly in the second example the argument used with the sizeof
operator is float so now n will have the value as 4.

Note:

Pointer operator and member selection operator are explained in chapters Pointers and
Structures and Unions respectively.

An arithmetic expression is a combination of variables, constants and


operators arranged as per the syntax of the language.

Integer arithmetic:

When the operands in a single arithmetic expression are integers, then the expression is
called integer arithmetic. Integer values are yielded by integer arithmetic.
Example:
a= 14 , b=4
x = a + b; x will have the value as 18
x = a - b; x will have the value 10
x = a * b; x will have the value 56
x = a / b; x will have the value 3. The decimal part is truncated
x = a % b; x will have the value 2. This operation yields the remainder after division.

Real mode Arithmetic:

An arithmetic operation involving real operands is called real arithmetic.


Example:

x = 3.0 / 2.0 = 1.5


Y = 5.0 / 2.0 = 2.5

The modulo operator ( % ) cannot be used in real arithmetic.

Mixed mode arithmetic:

When one of the operands is integer and other is real the expression is called as mixed
mode arithmetic expression. If either operand is of real type then only the real operation is
performed and the result is always a real value.

Example:

x = 15/10.0 = 1.5 y = 15.0 / 10 = 1.5

Note:

The variable x and y should be of real data type.

Precedence of arithmetic operators:

An arithmetic expression without parenthesis will be evaluated from left to right using the
rules of precedence of operators.
Following table gives the precedence from Lowest to Highest priority:

Assignment =, +=, -=, *=, /=, %=


Conditional ?:
Relational <, >, <=, >= = = , ! =
Arithmetic *, /, % +, -
Unary ! , ++, --
Parenthesis ()
Logical &&, ||

Example 1.

2 + 3 * 2 / 2-1

The expression is evaluated from left to right according to the rules of precedence.

2+6/2-1
2+3-1
5-1
4

Example 2.

3 * 2 + 3 - 1 * 2/3 + 3 % 2

The expression is evaluated from left to right according to the rules of precedence.
6 + 3 -1 * 2/ 3 + 3 % 2
6+3-1*0+3%2
6+3-0+3%2
6+3-0+1
9-0+1
9+1
10

Example 3.
(2 + 3) * 2/ 2 -1

The expression is evaluated from left to right according to the rules of precedence.

5 * 2 / 2 -1
10 / 2 - 1
5-1
4

Input and Output Operations

When a program is written, you may need to give some input to the program at the time of
execution and the result generated by the program may be required to be displayed on the
computer screen. So C provides certain library functions, which are used for input and out
operations.

Reading a character:

Reading a single character can be done by using a function getchar().

The syntax is:

Variable name= getchar();

Variable name is a valid C name that has been declared as char data type. When this
statement is encountered the computer waits until a key and Enter key is pressed and then
assigns the inputted value to the getchar() function and since getchar() function is used at
the right hand side of the assignment statement the inputted value in turn is assigned to the
variable name which is at the left hand side of the assignment statement..

Example:

#include <stdio.h>
void main()
{
char c;
printf("Enter a character\n");
c=getchar();
printf("Entered character is %c\n",c);
}

In the above program the variable name c is declared as character data type. The printf()
function is used to display the message. When the computer encounters c=getchar();
statement, it waits for the user to input a value and press an Enter key. When you do so, the
value is assigned to the function and it is assigned in turn to the variable name c and using
the printf() function present in the next line, the value present in the variable c is displayed
on the screen.

To read the characters contained in a line of text the getchar() function can be called
successively. This function accepts any characters that is keyed in including new line and
tab.

Note:

When we enter a character and press Enter key, along with the character the new line
character will also be present in the input buffer after getchar() returns. So this may cause
problems when we use getchar() function repeatedly. So to take care of new line character
present in the input buffer, flushall() function or an extra getchar() function can be used.

Writing a character:

We can use a function named as putchar() to display a character on the computer monitor.

The syntax is:

putchar(Variable name);

Where variable name is a valid C name that has been declared as char data type of which
the value will be displayed.
Example:

#include<stdio.h>
void main()
{
char c;
printf("Enter a character\n");
c=getchar();
putchar(c);
}

The above program displays the message, accepts the entered character and displays
that character on the computer monitor.

Note:

C provides other functions like getch() and putch() to accept and display the character
from the standard input and standard output unit respectively.

When getch() function is used it will not display the entered character on the
computer monitor and it will not wait for the user to press an Enter key. So one can
use getch() function at the end of the program so as to stay on the output screen after
the execution of the program and when any key is pressed, the program screen is
shown. So the pressed key is not displayed.

Similarly putch() function is available to display the value of a character variable.

Formatted input:

This refers to an input data that has been arranged in a particular format. For
example if three data elements, say 13, 13.37, 'z' has to be read in according to the
format of its appearance, that is 13 to be read into variable of data type int, 13.37 to
be read into variable of data type float and 'z' to be read into variable of data type
char. This type of inputting is possible with the help of function scanf().

Syntax:

scanf("control string",arg1,arg2....argn);

The control string specifies the field format in which the data is to be entered and the
arguments arg1, arg2…..argn specifies the apresses of locations where the data is to
be stored. Comma separates control string and arguments.

Field specifications which is specified in control string, directs the computer to the
interpretation of input data. It may include following things:

Format (field) specifications consisting of conversion character %, a data type


character and an optional number specifying the field width.
Blanks, tabs or new lines are ignored.

The data type character indicates the type of data that is to be assigned to the variable
associated with the corresponding argument.

There should be one to one correspondence between the order of field specification
appeared within the control string and the order of arguments arg1, arg2,....argn.
This means within the control string if %d has appeared first, then %f and then %c
then, after the control string first you should have arg1 as an integer variable, arg2 as
real variable and arg3 as character variable.

Example:

#include <stdio.h>
void main()
{
int m;
float n;
char c;
printf( "Input an integer, real and an alphabet\n");
scanf("%d%f%c",&m,&n,&c);
printf("inputted values are %d%f%c\n",m,n,c );
}

In the above example % sign in both scanf() and printf() function is used and the %
sign indicates that conversion specification follows. In the above example %d means
integer, %f means real and %c means character.

Formatted Output:

Similar to Formatted Input we have Formatted Output. In this the results, messages
can be displayed the way in which we want. Here also we have the control string
which is followed by arguments.
Syntax:

printf("control string",arg1,arg2....argn);

The control string is made up of:

Characters, special symbols that you want to be displayed on the computer

Field specifications that tell you the type of data you want to display and the way in
which you want to display. This also specifies the number of arguments that follows
the control string.

Format (field) specifications consisting of conversion character %, a data type


character and an optional number specifying the field width. The data type character
indicates the type of data that is to be assigned to the variable associated with the
corresponding argument.

Escape sequence characters such as new line (\n), tab (\t) etc.

The arguments arg1, arg2....argn specifies the apresses of locations from where the
data is to be retrieved. Comma separates control string and arguments.
There should be one to one correspondence between the order of field specification
appeared within the control string and the order of arguments arg1, arg2,....argn.
This means within the control string if %d has appeared first, then %f and then %c
then, after the control string first you should have arg1 as an integer variable, arg2 as
real variable and arg3 as character variable.

Example:

#include<stdio.h>
void main()
{
int m=51;
float n=13.7;
char c='a';
printf("Values are m=%d n=%f c=%c\n",m,n,c );
}

Output of Integer Number:

For printing an integer number %wd can be used as format specification where w
specifies the minimum number of columns used for the output. However if a number
is greater than the specified number of columns, it will be printed in full, overwriting
the minimum specification. d specifies that the value to be printed is an integer. The
number is written right justified in the given field width. Leading blanks will appear
as necessary. The leading blanks can be filled with zeros by placing 0 before the field
width specifier. By placing a minus (-) sign directly after % character, it is possible to
force the printing to be left justified. By specifying ld in the place of d in the format
specification long integer may be printed.

Example showing different format specifications for integer:

void main()
{
int a;
a = 1234;

printf("%d\n",a);
printf("%6d\n",a);
printf("%-6d\n",a);
printf("%06d\n",a);
printf("%2d\n",a);
}

Output:
Output of Real Numbers:

A real number may be displayed in decimal notation using the format %w.pf. The
integer w indicates the minimum number of columns that are to be used for the
display of the value and integer p indicates the number of digits to be displayed after
the decimal point. The value is rounded to p decimal places and printed right justified
in the field of w columns. Leading blanks and trailing zeroes will appear as necessary.
The default precision is 6 decimal places. The negative numbers will be printed with
the minus (-) sign. We can also display a real number in exponential notation by using
specification % w.pe

In the exponential notation the display takes the form [ - ]m.nnne [ ] xx where the
length of the string n is specified by precision 'p'. The default precision is 6. The field
width w should satisfy the condition w p + 7. The value will be rounded off and
printed right justified in the field of w columns.

Paping the leading blanks with zeroes and printing with left justification is also
possible by introducing 0 or '-' before field width specification specifier w.

#include <stdio.h>
void main ()
{
float a;
a=33.456;
printf("%f\n",a);
printf("%6.2f\n",a);
printf("%-6.2f\n",a);
printf("%9.2e\n",a);
printf("%-9.2e\n",a);
printf("%9.2e\n",-a);
}

Output:

Outputting a single character:

A single character can be displayed using the format %wc. The character will be
displayed right justified in the field of w columns. By placing a minus (-) sign before
the integer w the display can be made left justified. 1 is the default value of w.

#include <stdio.h>

void main()
{
char a = 'x';
printf("%c\n",a);
printf("%4c\n",a);
printf("%-4c\n",a);
}
Output:

Outputting of Strings:

For this the format specification is %w.ps, where w specifies the number of columns
for display and p specifies that only the first p characters of the string are to be
displayed and the display is right justified in the field of 'w' columns.

#include <stdio.h>

void main()
{
char a[50];
printf("Enter the Input String\n");
scanf("%[^\n]",&a);
printf("\nDifferent types of Outputs are\n\n");
printf("%s\n",a);
printf("%25s\n",a);
printf("%25.15s\n",a);
printf("%.9s\n",a);
printf("%10s\n",a);
printf("%-25.15s\n",a);
}

Output:

Note:

While reading character string using scanf() function if we give the format
specification as %[characters] it means that only the characters specified within the
brackets are allowed to be inputted and inputting of any other character other than
those given inside the brackets will terminate the reading.

Example:

scanf("%[a-n]", &a);

The above statement will allow you to input any characters from a to n and if any
other characters other than these characters are entered from the keyboard then the
reading is terminated immediately.
Whereas in the format specification %[^character] the character specified are not
permitted to be inputted. Upon encountering the character specied within the bracket,
the inputting is terminated.

Example:

scanf("%[^a-n]",&a);

The above statement will not allow you to input any characters from a to n and any
other characters other than these characters are accepted and if any characters
specifed in the bracket are entered from the keyboard, immediately the reading is
terminated.

The format specification %s will terminate the reading at the encounter of the space.
So it is used to read the string without any space.

Example:

scanf("%s",&a);

To read the string till the end of line use the format specification character as %[^\n].
It means allow all the characters including space till the end of line character is
pressed.

Example:

scanf("%[^\n]",&a);

Sequential Flow of Instructions:

When the instructions of a program are executed one after another from the
beginning to the end, it is called as Sequential Flow of instructions.

The following are the programming examples for sequential flow of instructions.
Example 1:

/*C Program to calculate sum of two numbers*/

#include <stdio.h>

void main()
{
int a,b,c;
a=55;
b=65;
c=a+b;
printf("A is %d, B is %d, Result is %d",a,b,c);
}

Output:

Example 2:

/*C Program to find Area of a Cirle*/


#include <stdio.h>
#include <conio.h>
void main()
{
int r;
float area;
r=6;
area=3.14*r*r;
printf("The Area of the Circle is %f",area);
getch();
}

Output:

Example 3:

/*C Program to calculate Area of a Triangle*/

#include <stdio.h>
#include <conio.h>
void main()
{
int b, h;
float area;
printf("Enter the Base of Triangle\n");
scanf("%d",&b);
printf("Enter the Height of the Triangle\n");
scanf("%d",&h);
area=(b*h)/2;
printf("\nThe Area of the Triangle is %f",area);
getch();
}

Output:

C- Preprocessors
This is a program that processes our source program before it is passed to the compiler.
Preprocessor commands (generally known as directives) make a program easy to read,
modify, and more efficient

Features of C preprocessors:

The preprocessor commands often known as directives begin with # symbol.


These directives can be placed anywhere in a program but most oftenly placed at the
beginning of the program i.e., before main () or before the beginning of a particular
function.
In C we have following preprocessor directives:

1. Macro expansion directives


2. File inclusion directives
3. Compiler control directives

Examples for Preprocessor Directives are # include, # define, # else, # endif, # if, # elif

Macros:
When a program is compiled, before the source code is passed through the
compiler, it is examined by preprocessor for any macro-definition.
When it encounters # (hash) sign and define directive it goes through the entire
program to search for macro template and replaces it with the appropriate macro
expansion.
Only after this procedure has been completed, the program is handed over to
compiler.

Example:
# include <stdio.h>
# define a 25
main ()
{
int b;
printf ("a is %d", a);
b = a + 25;
printf ("b is %d\n", b);
}

In the example the value of a is replaced by 25 by the preprocessor in the source code
(program) before it is passed on to the compiler.
Rules to use Macros:

1. It is only customary to use capital letters for macro template.


2. Macro template and its macro expansion is separated by blank space or tabs.
3. The blank spaces between the # sign and define statement is optional.
4. Macro definition is never terminated by a semicolon.

Use of Macro - definitions:

1. To improve the readability.


2. If the value of say a is changed (see the above program) and if macro is not used
then, we have to go to every place and change the values manually wherever it is
used.
3. If the macros are used, you can change the values only at macro definition.

Note:
Although we can use the variables for the same purpose we won't use it because:

1. It is inefficient since the compiler can generate faster and micro compact code for
constants than it can for variables.
2. In the program if the variable changes because of something somewhere then the
concept of constants will not hold good.

Define directive and operators:


Operators can even be replaced with most descriptive word.
# include<stdio.h>
# define AND &&
# define OR ||

void main ()
{
int a, b, c;
printf ("Enter the values for a, b, c\n");
scanf ("%d %d %d", &a, &b, &c);
if ((a<25) AND (b<5 OR c>10))
printf ("In the range \n");
else
printf ("Not in the range \n");
}

Output:

Define directive can be used even to replace a condition.


# include<stdio.h>
# define AND &&
# define RANGE (a>2 AND a<25)
void main ()
{
int a;
printf("Enter the values for a \n");
scanf("%d",&a);
if (RANGE)
printf("In the range \n");
else
printf("Not in the range \n");
}

Output:

Define directive can be used to replace even an entire C statement.


# include <stdio.h>
# define WELCOME printf ("Welcome to M.G.M. \n");
void main ()
{
WELCOME
}

Macros with arguments:


Macros can have arguments just as functions can
# include<stdio.h>
# define AREA(x) (3.14 *x *x)
void main ()
{
float r, a;
printf ("Enter the value for radius \n");
scanf("%f", &r);
a =AREA(r);
printf("Area is % f\n", a);
}

Output:

In the above program, wherever the pre-processor finds AREA (x) it expands it into
a statement (3.14*x*x).
The x in the macro template AREA (x) is an argument that matches x in the macro
expansion.
The statement AREA (r) in the program causes the variable r to be substituted for x.
Thus the statement AREA (r) is equivalent to (3.14 *r *r).

Although macro calls are function calls they are not really the same.
In a macro call, the pre-processor replaces the macro calls as it is.
Whereas in a function call the control is passed to a function along with certain
arguments and a value may be returned by a function.
Usually macros make the program faster but increase the program size.
But functions make the program smaller and compact.
In functions, passing the arguments and getting back the result may take time and
slow down the program. But the macros are expanded and kept.

File Inclusion:
These directives are used to include the files within a program. These are the
directives, which we have been using in all our programs.
Using this directive one file is included in another file.

Syntax:
#include "file name"
#include <file name>

Example:
#include <stdio.h>
In the example the file stdio.h will be inserted in the file in which it is specified.

Compiler Control Directives:


These are the directives, which can be used for conditional compilation.
The example given illustrates this:
# include <stdio.h>
# define i 1
# define j 5
# if i==1
# include <stdlib.h>
# else
# include<conio.h>
# endif
void main()
{
int k;
k=j * 10;
printf("calculated value of k is %d", k);
}

Note:
The example is just the illustration for usage of conditional compilation directives
and does not carry much meaning.

Storage Classes And User Defined Data types


Scope and life time of variable in functions
Storage class provides the information about location and visibility of a variable. Thus a
variable in C along with the data type also has the storage class. The visibility of a variable
means the portion of the program within which the variables are available.

Example:

int m;
main ( )
{
int i;
m = 60;
disp ( );
}
disp( )
{
int n;
:
:
m = 50;
:
:
}

In the above example the variable m, this has been declared before the main and it is called
as Global variable.

It can be used in all the functions in the program. It need not be declared in other functions.
A global variable is also known as an external variable. In the above example the variables
i and n declared within main() and disp() respectively and are known as local variable
because they are declared within a function. Local variables are visible and meaningful
only inside the functions in which they are declared. They are not known to other functions.

C provides a variety of storage class specifiers that can be used to declare explicitly the
scope and life time of variables. The concepts of scope and life time are important in
multifunction and multiple file programs. Following are the different storage classes that
exist in C.

1. Automatic variables
2. Static variables
3. Register variables
4. External variables

o Automatic Variables: These variables are declared inside a function in


which they are to be utilized. They are created when the function is entered.
Hence the name automatic. Thus automatic variables are local to the
function in which they are declared. Thus, they are referred to as local or
internal variables. An automatic variable does not retain its value once
control is transferred out of its defining function. Therefore any value
assigned to automatic variable within a function will be lost once the
function is exited. These variables are destroyed automatically when the
function is exited.
Example: auto int n;

o Static variables: The value of the static variable is retained until the end of
the program. A variable can be declared static using the keyword static.

Example: static int m;

The static variables which are declared inside a function is called as internal
static variable. The static variables can be external also. The scope of
internal static variables extends up to the end of the function in which they
are defined. Therefore internal static variables are similar to auto variables,
except that they remain in existence throughout the remainder of the
program. Thus even between the function calls the internal static variables
retain their values. For example it can be used to count the number of calls
made to a function as shown in the following examples.
A static variable is initialized only once when the program is compiled. It is
never initialized again.

#include <stdio.h>
#include <conio.h>
main( )
{
int i;
void disp();
clrscr();
for (i=0; i<5; i++)
{
disp();
}
getch();
}
void disp()
{
static int counter = 0;
printf("This is BCA Class \n");
counter = counter + 1;
printf ("Counter is %d \n", counter);
}

Output:
From the output of the example you can observe that value of the variable
counter is retained and it is incremented each time the function is visited.

o Register variables: When we declare a variable as a register variable the


CPU's registers are assigned instead of memory location. Since register
access is much faster than the memory access, keeping the frequently used
variables in the register will lead to the faster execution of program. Since
only few variables can be placed in the register it is important to carefully
select the variables for this purpose. However C will automatically convert
register variables to non-register variables once the limit is reached.

{
register int i;
:
:
}

o External variable: External variable are not confined to a single function.


Their scope extends from the point of definition to the remainder of the
program. That means they often span an entire program and they are also
known as global variables. The global variables are initialized to 0 by
default. Once a variable is declared as global, any function can use it and
change its value. Then subsequent function can reference only that new
value. Because of this property we can use global variables for variables
shared between functions when it is inconvenient to pass them as
parameters.

Note: Global variable is visible only from the point of declaration to the end
of the program.
Example

main( )
{
extern int m;
void disp();
:
}
void disp( )
{
extern int m;
:
}
int m;

As shown in the example although the variable m has been defined after
both the functions. The external declaration of m inside the function informs
the compiler that it is an integer type defined some where else in the
program. The extern declaration does not allocate memory space for
variables. An extern within function provides the type of information to just
that function. In case of arrays the definition should include their size as
well.

User defined data types:

typedef: This feature allows user to define new data type that are equivalent to
existing data types. Once a user-defined data type has been established then new
variables, arrays, structures and so on can be declared in terms of this new data type.

Syntax:
typedef data type newtype;

Where datatype refers to an existing data type either a standard data type or a
previous user-defined data type and newtype refers to the new user-defined data
type. However the new data type will be new in name only. In reality this new data
type will not be fundamentally different from one of the standard data type.

Example:

typedef int empno;


empno a;
typedef int age;
age male, female;
In the examples empno and age are the user-defined data type equivalent to type int.
So, the variable declaration empno a; is equivalent to writing int a; and age male,
female; is equivalent to writing

int male, female;


typedef float height [100];
height men, women;

In the examples, new data type height is defined as 100-element floating-point


array. Hence men and women are 100-element floating-point array.
typedef feature is particularly convenient when defining structures, since it
eliminates the need to repeatedly write the struct tag.

typedef struct
{
member 1;
:
:
member n;
}
new type;

Example:

typedef struct
{
int empno;
char emphame [25];
} employee;
employee a, b;

New type is the user-defined structure type. The structure variables can be defined
in terms of new datatype. Hence employee is the new user defined data type. So, a
and b are the structure variables of type employee.

Enumerate Data Type:

This is a user-defined data type, which can be used to declare variables that can
have any one of the values enclosed within the flower brackets. These values are
known as enumeration constants. Once this definition is made, variables of this
enumerate type can be declared.

Syntax:

enum identifier {value1, value2,....valuen};


enum identifier variable1, variable2....variablen;
The declared enumerated variables can have any one of the values defined inside
the flower brackets.

Example:

enum day { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};


enum day weekbegin, weekend;

In the above example weekbegin and weekend can have any one of the values i.e.it
can have Monday or Tuesday and so on.
Beginning with integer value zero all the enumeration constants are assigned values
by the compiler automatically.
So in the above example Monday is assigned with the value zero, Tuesday gets the
value 1 and so on. Thus Sunday gets the value as 6. But automatic assignment can
be overridden by assigning explicit values to the enumeration constants i.e. Monday
can have value 5. So Tuesday will take value 6, Wednesday will take value 7 etc.

Example:

#include <stdio.h>
#include <conio.h>
void main( )
{
int weekday1, weekday2;
enum day {Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday};
enum day weekbegin, weekend, weekmiddle;
printf("Enter 0 for Monday\n");
scanf("%d",&weekday1);
printf("Enter 3 for Thursday\n");
scanf("%d",&weekday2);
weekbegin=weekday1;
weekmiddle=weekday2;
weekend=Sunday;
if(weekbegin==Monday)
printf("Week has begun\n");
if(weekmiddle==Thursday)
printf("Middle of the week\n");
if(weekend==Sunday)
printf("Enjoy the Sunday the week end\n");
getch( );
}

Output:
Decision Making and Branching
In programming the order of execution of instructions may have to be changed depending
on certain conditions. This involves a kind of decision making to see whether a particular
condition has occurred or not and then direct the computer to execute certain instructions
accordingly.

Decision making with if statement:

The if statement is a powerful decision making statement and is used to control the flow of
execution of statements.

The syntax is

if (expression)

The expression is evaluated and depending on whether the value of the expression is true
(non zero) or false (zero) it transfers the control to a particular statement.

The general form of simple if statement is:

if (test expression)
{
statement block;
}
statement n;
Example:

if (code = = 1)
{
salary = salary + 500;
}
printf("%d",salary);

The statement block may be a single statement or a group of statements. If the test
expression is true, the statement block will be executed otherwise the statement block will
be skipped and the execution will jump to the statement n. But if the condition is true, both
the statement block and statement n will be executed. Thus if the expression is false only
statement n will be executed.

The if... else statement is an extension of the simple if statement

Syntax is:

if (test expression)
{
statement block;
}
else
{
statement block;
}
statement n;
if (code = = 1) {
salary = salary + 500;
}
else
{
salary = salary + 250;
}
printf ("%d', salary);

If the code is equal to 1, the statement salary = salary + 500 is executed and the control is
transferred to statement printf("%d",salary) after skipping the else part.
If the code is not equal to 1, the statement, salary = salary + 500 is skipped and the
statement in the else part, salary = salary + 250 is executed before the control reaches the
statement printf ("%d", salary);

Nesting of if....else statement:

When a series of conditions are to be checked, we may have to use more than one if... else
statement in the nested form.

if (test condition 1)
{
if (test condition 2)
{
statement block 1;
}
else
{
statement block 2;
} statement m;
}
else
{
if (test condition 3)
{
statement block 3;
}
else
{
statement block 4
} statement n;
}
statement x;

If the test condition 1 is true then, test condition 2 is checked and if it is true, then the
statement block 1 will be executed and the control will be transferred to statement m and it
will executed and then statement x will be executed.

If the test condition 1 is true but test condition 2 is false, statement block 2 will be executed
and the control is transferred to statement m and it will be executed and then statement x
will be executed.

If the test condition 1 is false, then test condition 3 is checked and if it is true, statement
block 3 will be executed, then control is transferred to statement n and it will be executed
and then statement x will be executed.

If the test condition 1 is false and test condition 3 is also false, statement block 4 will be
executed, then the control is transferred to statement n and it will be executed and then
statement x is executed.

The switch statement:

When many conditions are to be checked then using nested if...else is very difficult,
confusing and cumbersome.So C has another useful built in decision making statement
known as switch. This statement can be used as multiway decision statement.The switch
statement tests the value of a given variable or expression against a list of case values and
when a match is found, a block of statements associated with that case is executed.

Syntax:

switch (expression)
{
case value 1 :
statement block 1;
break;
case value 2:
statement block 2;
break;
:
:
default:
default block;
}
statement n;

The expression is an integer expression or characters. Value 1, value 2, ...... are constants ,
constant expressions (evaluable to an integral constant) or character and are known as case
labels. Each of these values should be unique within a switch statement, statement block 1,
statement block 2, ........ are statement list and may contain 0 or more statements. There is
no need to put braces between these blocks. The case labels end with a colon (:).

When the switch is executed, the value of the expression is successively compared against
the values value 1, value 2, ...... If a case is found whose value matches with the value of
the expression, then the block of statement that follows that case are executed. The break
statement at the end of each block, signals the end of a particular case and causes an exit
from the switch statement transferring the control to the statement n following the switch
block. The default is an optional case. When present, it will be executed if the value of the
expression does not match with any of the case values and then the statement n will be
executed. If not present no action takes place if all matches fails and the control goes to
statement n.

Example:

Consider an organization, in which da(Dearness Allowance) of an employee is


calculated depending on the category of employee.
Codes are given to different categories and da is calculated as follows:
For code 1,10% of basic salary.
For code 2, 15% of basic salary.
For code 3, 20% of basic salary.
For code >3 da is not given.

# include<stdio.h>

#include

<conio.h>
void main ()
{
float basic , da , salary ;
int code ;
char name[25];
da=0.0;
printf("Enter employee name\n");
scanf("%[^\n]",name);
printf("Enter basic salary\n");
scanf("%f",&basic);
printf("Enter code of the Employee\n");
scanf("%d",&code);
switch (code)
{
case 1:
da = basic * 0.10;
break;
case 2:
da = basic * 0.15;
break;
case 3:
da = basic * 0.20; break;
default :
da = 0;
}
salary = basic + da;
printf("Employee name is\n");
printf("%s\n",name);
printf ("DA is %f and Total salary is =%f\n",da, salary);
getch();
}

Output:
The goto statement:

This statement is used to branch unconditionally from one point to another in the
program.This statement goto requires a label to locate the place where the branch is to be
made.A label is any valid identifier and must be followed by a colon. The label is placed
immediately before the statement where the control is to be transferred.

Different ways of using goto statement are given below:

Syntax:

goto label;
(1)

1. Forward jump: In this the position of the label is after the goto statement.
goto label;
:
:
label:
statement n;
Example:
goto read;
n = 5 * 4;
:
:
read:
scanf ("%d", &code);
:
:
(2)
2. Backward jump: In this, the position of the label is before the goto statement
label:
statement n;
: goto label;

Example:
. . read:
scanf ("%d", &code);
:
:
goto read;
n = 5 * 4;
:
:

/*C program to find the roots of a quadratic equation*/

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

void main( )
{
int a, b, c;
float disc, root1, root2, img1;
clrscr();

printf("Enter the value for a, b and c \n");


scanf("%d%d%d",&a,&b,&c);
disc=b*b-(4*a*c);
if(disc==0.0)
{
root1=b/(2.0*a);
root2 = root1;
printf("The roots are equal %f and %f \n", root1, root2);
}
else
if (disc>0)
{
root1=(-b+sqrt(disc))/(2.0*a);
root2=(-b-sqrt(disc))/(2.0*a);
printf("The roots are real %f and %f\n", root1, root2);
}
else
{
if(disc<0)
{disc=abs(disc);
root1= b/(2.0*a);
img1=sqrt(disc)/(2.0*a);
printf("The roots are imaginary %f+i%f\n",root1, img1);
printf("The roots are imaginary %f-i%f\n",root1, img1);
}
}
getch( );
}

Output:

/* C program to convert a decimal number to character below 1000*/

#include<stdio.h>

#include <conio.h>

void main( )
{
int a, b, c, d, n;
clrscr();
printf("Enter the number \n");
scanf("%d",&n);
if (n<20)
{
switch (n)
{
case 1: printf("one "); break;
case 2: printf("Two "); break;
case 3: printf("Three "); break;
case 4: printf("Four "); break;
case 5: printf("Five "); break;
case 6: printf("Six "); break;
case 7: printf("Seven "); break;
case 8: printf("Eight "); break;
case 9: printf("Nine "); break;
case 11: printf("Eleven "); break;
case 12: printf("Twelve "); break;
case 13: printf("Thirteen "); break;
case 14: printf("Fourteen "); break;
case 15: printf("Fifteen "); break;
case 16: printf("Sixteen "); break;
case 17: printf("Seventeen "); break;
case 18: printf("Eighteen "); break;
case 19: printf("nineteen "); break;
}
}
if(n>19 && n<1000)
{
a=n/100;
b=n%100;
c=b/10;
d=b%10;
switch(a)
{
case 1: printf("One Hundred "); break;
case 2: printf("Two Hundred "); break;
case 3: printf("Three Hundred "); break;
case 4: printf("Four Hundred "); break;
case 5: printf("Five Hundred "); break;
case 6: printf("Six Hundred "); break;
case 7: printf("Seven Hundred "); break;
case 8: printf("Eight Hundred "); break;
case 9: printf("Nine Hundred "); break;
}
if(b>10 && b<20)
{
switch(b)
{ case 11: printf("Eleven "); break;
case 12: printf("Twelve "); break;
case 13: printf("Thirteen "); break;
case 14: printf("Fourteen "); break;
case 15: printf("Fifteen "); break;
case 16: printf("Sixteen "); break;
case 17: printf("Seventeen "); break;
case 18: printf("Eighteen "); break;
case 19: printf("nineteen "); break;
}
}
else
{
switch (c)
{
case 1: printf("Ten "); break;
case 2: printf("Twenty "); break;
case 3: printf("Thirty "); break;
case 4: printf("Forty "); break;
case 5: printf("Fifty "); break;
case 6: printf("Sixty "); break;
case 7: printf("Seventy "); break;
case 8: printf("Eighty "); break;
case 9: printf("Ninety "); break;
}
switch(d)
{
case 1: printf("one "); break;
case 2 : printf("Two "); break;
case 3 : printf("Three "); break;
case 4 : printf("Four "); break;
case 5 : printf("Five "); break;
case 6 : printf("Six "); break;
case 7 : printf("Seven "); break;
case 8 : printf("Eight "); break;
case 9 : printf("Nine "); break;
}
}
getch();
}
}

Output:
Decision Making and Looping
Execution of a statement or set of statement repeatedly is called as looping.

The loop may be executed a specified number of times and this depends on the satisfaction
of a test condition.

A program loop is made up of two parts one part is known as body of the loop and the other
is known as control condition.

Depending on the control condition statement the statements within the loop may be
executed repeatedly.

Depending on the position of the control statement in the loop, a control structure may be
classified either as an entry controlled loop or as an exit controlled loop.

Entry Controlled Loop:

When the control statement is placed before the body of the loop then such loops are called
as entry controlled loops.

If the test condition in the control statement is true then only the body of the loop is
executed.

If the test condition in the control statement is not true then the body of the loop will not be
executed. If the test condition fails in the first checking itself the body of the loop will
never be executed.
Exit Controlled Loop:

When the control statement is placed after the body of the loop then such loops are
called as exit controlled loop.
In this the body of the loop is executed first then the test condition in the control
statement is checked.
If it is true then the body of the loop is executed again.
If the test condition is false, the body of the loop will not be executed again. In exit
controlled loops even if the test condition fails in the first attempt itself the body of
the loop is executed at least once.
Following are the steps in a looping process:

1. Initialization of loop control variable


2. Test of a specific condition for the execution of the loop
3. Execution of the statements in the body of the loop
4. Altering the value of the loop control variable

In the beginning the loop control variable is initialized after that step2, step3 and step4 are
carried out till specified test condition becomes false.
The test may be either to determine whether the loop has been repeated the specified
number of times or to determine whether a particular condition has been met. The C
language provides 3 loop structures.

1. while loop.
2. do loop.
3. for loop.

Control Variables:
These are the variables which control the number of times a loop will be executed.

The while statement: The basic format of the while statement is as given below

 while (test condition)


{
body of the loop;
}

This is an entry controlled loop. In this the test condition is placed before the body of the
loop. The test condition is evaluated first and if it is true, then only the body of the loop will
be executed.

In the body of the loop there will be statement, which will alter the value of the loop control
variable. After the execution of the body of the loop the test condition is again evaluated
and if it is true then only the body of the loop will be executed. Then the body of the loop
will be executed till the test condition becomes false.

Once the test condition fails the statement following the body of the loop will be executed.
The body of the loop will begin with a opening brace and ends with a closing brace. The
body of the loop may have one or more statements.
The loop control variable is used in the test condition to check whether the body of the loop
is to be executed or not.
If the test condition fails in the beginning itself the body of the loop will never be executed.

Segment of a program is given below:


i = 0;
while (i<=10)
{
printf ("%d\n", i);
i++;
}

In the above example, the body of while loop is executed 11 times. Since the test of a
condition is carried out before the loop is executed, the body of the loop may not be
executed at all if the condition is not satisfied at the very first instance.

 The do statement:
 In some occasions it might be necessary to execute the body of the loop before the test is
performed. Such situations can be handled with the help of do statement.
Syntax:
do
{
body of the loop;
}
while (test condition);

When the do statement is encountered, the body of the loop is executed. The test
condition is present at the end of the body of the loop. The test condition is
evaluated and if it is true, the body of the loop is executed again. This process will
continue till the test condition becomes false. Now the statement next to the test
condition will be executed. Since the test condition is placed at the end of the body
of the loop, this do loop is called as exit controlled loop. Therefore, even though the
test condition fails at the first attempt itself, the body of the loop is executed at least
once.

Example:
i=0;
do
{
printf ("%d\n", i);
i++;
} while (i< 10);

The above loop is executed 11 times because the value of I is initialized to zero.
Consider the following case where i is initialized to 11, still the body of the loop is
executed at least once although the test condition fails at the first attempt itself.

i=11;
do
{
printf ("%d\n",i);
i++;
} while (i< 10);

/* Program to test do loop*/


# include <stdio.h>
# include <conio.h>
void main(){
char c;
c=' ';
do
{
clrscr();
printf("Welcome to MGM\n");
c=getch();
} while(c!= 'q');
}

Note:

Test conditions in loop constructs can also have compound statements as shown
below.

Example:
while (i>0 && k<5)
{
printf ("%d\n", i);
i --;
}

for loop:

 This is an entry controlled loop which provides compact loop control structure. In
this, the initialization of loop control variable, the test condition and change in the
value of loop control variable is done in the single statement separated by
semicolon.
Syntax:
for (initialization; test condition; increment)
{
body of the loop;
}

The for loop is executed as given below:


 The initialization of the loop control variable is done first with the help of the
assignment statement such as i = 1 or j = 0. In this assignment statement, i and j are
known as loop control variables. This is the first part of for statement.

 The second part of the for statement is the test condition. The test condition is
evaluated and if it is true, the body of the for loop is executed. If the test condition is
false, the body of the loop will not be executed and the control will be transferred to
the statement immediately after the body of the loop.

 After the execution of the body of the loop the control is transferred to the third
part of the for statement where the value of the loop control variable is changed, say
incremented or decremented depending on the statement in this part. Now again the
test condition is evaluated and if it is true, then, the body of the loop is executed.
Otherwise the loop is terminated and the control will be transferred to the statement
that follows the body of the loop.

Example 1:
Output of this loop construct
for (i=0; i<5; i++) 0
{1
printf ("%d\n", i); 2
}3
4
for (i=4; i>=0; i--) 4
{3
printf ("%d\n", i); 2
}1
0

In Example 1 the output is printed from 0 to 4 because the value of i is incremented


by one and the loop is executed five times.
The for loop in example 2 is also executed 5 times but it prints 4 to 0.
Semicolons must separate the 3 sections enclosed within the paranthesis. Note that
there is no semicolon at the end of increment section (i.e., i++, i --). Since the
conditional test is always performed at the beginning of the loop, the body of the
loop may not be executed at all if the condition fails at the beginning itself. So it is
entry controlled loop.
Example:
for (i=6; i>7; i++)
{
printf ("%d\n", i);
}

The above loop will not be executed at all as the condition fails in the first attempt itself.
One of the important points about the for loop is that all the 3 sections, namely
initialization, testing and assigning (incrementing & decrementing) are placed in the for
statement itself.

Additional Features of for loop:


 The for loop in C has several capabilities that are not found in other loop constructs.
More than 1 variable can be initialized at a time in the for statement.

Example:
p=1
for (i=0; i<5; i++)

The above statement can be written as,


for (p=1, i=0; i<5; i++)

In the above for statement the initialization section has 2 parts i.e., p=1 and i=0 separated
by, (comma). Like the initialization section, the incremenation section may also have more
that one part.

Example:
for (n=1, m=10; n<=m; n++, m--)

The multiple arguments in the increment section are separated by commas. Another feature
of the for loop is that the test condition may have any compound relation and the testing
need not be limited to the loop control variable.

Example:
for (i=0; i<10 && k==2; i++)

In for loop it is also possible to use expression in the assignment statement of initialization
and increment statement.

Example:
for (i=m+n; i<10; i=m++)

Another unique aspect of for loop is that one or more sections can be omitted if necessary.
i=0;
for (; i<5;)
{
printf ("%d\n", i);
i++;
}

Consider the for loop given above, both the initialization and increment section is omitted
in the for loop. The initialization is done before the for loop and the control variable is
incremented inside the loop. In such cases the sections are kept blank however the
semicolons separating the sections must remain.

Null statement or Empty statement:


When loop does not contain any statement instead if it contains just a semicolon
then such statement is called as Null statement or empty statement. A time delay
can be set up using a null statement in the for loop.

Example:
for (i=0; i<100000; i++)
 The above loop is executed 100000 times without producing any output. It simply causes
time delay. The body of the loop contains only semicolon and it is known as null statement.

Nesting of loop:
Placing one loop within another is called as Nesting of loops. This nesting can be achieved
with any of the loop constructs namely for, while, or do statements. You can even mix
different loop constructs.
Example:
 for (i=5; i<20; i++)
{
for (j=5; j<20; j++)
{
statements;
}
statements;
}

When the value of i is equal to 5 then the for with j will take the value from j=5 to j=19.
Again for the i value of 6, j will take the value from j=5 to j=19 this continues till the value
of i becomes 19.
i=0;
while (i<10)
{
j=1;
while (j<20)
{
statements;
j++;
}
i++;
}

For each value of I starting from 0 till 9 j will take the value from 1 to 19.

Jumps in loops:
Transferring the execution control from one statement to another statement within the loop
or from within the loop to outside the loop is possible in C. This kind of jumps can be
achieved by break, goto and continue statements.

break:
Using this statement an early exit from a loop can be achieved. That is when the break
statement is encountered within a loop then the loop is exited immediately and the
statements following the loop will be executed. When a break is encountered within a
nested loop, the loop in which this statement is present only that loop will be exited.
That means the break will exit only one loop.
Syntax:
break;
(a) i=0;
while (i<10>
{
:
:
if (condition)
break;
:
:
}
n=7;
(b) i=0;
do
{
:
:
if (condition)
break;
:
:
} while (i<10);
n=7;
(c) while (i<15)
{
:
for (j=0; j<10; j++)
{
:
if (condition)
break;
:
}
n=7; (d) for (i=0; i<10; i++)
{
:
if(condition)
break;
}
n=7;
In all the above cases the break statement will make the control exit the loop in which it is
residing. Thus it executes the statement n=7 in all the cases.
Since goto can be used to transfer the control to any place in a program it can be used to
provide a branching within a loop or exit from deeply nested loop. A simple break
statement could not work here.
Example 1:
while (i<15)
{
:
:
for (j=0; j<10; j++)
{
:
:
if (condition)
goto xyz;
:
:
xyz:
r=4
}
n=7;
:
}
m=8;
Example 2:
while (i<15)
{
.
.
.
for(j=0; j<10; j++)
{
. while(p< 6)
{
.
if(condition)
goto xyz;
.
.}
.
k=4;
}
}
n=7;
xyz:
m=8;
In example1 the goto statement will transfer the control to lable xyz within the same loop
skipping the part of the loop.

continue:
 In a loop on some occasions depending on some condition we may have to skip a part of
the body of the loop and proceed with the next iteration.
This can be achieved by the statement continue.

Syntax:
continue;

Example 1:
while (i<10)
{
:
if (condition)
continue;
:
}
n=7;
Example 2:
do
{
:
if (condition)
continue;
:
} while (i<10);
while (i<15)
{
:
for (j=0; j<10; j++)
{
:
if (condition)
continue;
:
}
:
}

/*C Program to reverse the digit of a number and to find the sum of the digits*/
#include<stdio.h>
#include <conio.h>
void main()
{
int n,m,sum,rev;
clrscr();
printf("Enter the integer \n");
scanf("%d",&n);
m=0;
rev=0;
sum=0;
while (n>0)
{
m=n%10;
sum=sum+m;
n=n/10;
rev= rev*10+m;
}
printf("The reversed digit is = %d \n",rev);
printf("The sum of the digits is = %d \n", sum);
getch();
}

output:
/*C Program to count the number of palindromes in a given list of n numbers*/
#include<stdio.h>
#include <conio.h>
void main()
{
int a[50], m, i, md, rev, count, sum, item;
count = 0;
clrscr();
printf("Enter the number of integers \n");
scanf("%d",&item);
for (i=0; i<item;i++)
{
printf("Enter the number\n");
scanf("%d",&a[i]);
}
for(i =0; i<item;i++)
{
m = a[i];
rev = 0;
sum = 0;
while (m>0)
{
md = m%10;
sum =sum+md;
m = m/10;
rev=rev* 10+md;
}
if(rev == a[i])
{
count=count+1;
printf("Entered number %d is a palindrome \n",a[i]);
}
else
{
printf("Entered number %d is a not a palindrome \n",a[i]);
}
}
printf("The number of palindromes in the list of numbers are = %d\n",count);
getch( );
}

Output:
/*C Program to find the factors of the given number*/
#include <stdio.h>
#include <conio.h>
void main()
{
int a, b, n;
clrscr();
printf("Enter the number to find the factor \n");
scanf("%d",&n);
a = n;
printf("The factor of a\n");
while (a>0)
{
b = n%a;
if (b == 0)
{
printf("%d\n",a);
}
a--;
}
getch( );
}

Output:
/*C Program to find all the prime numbers between a & b*/
#include <stdio.h>
#include <conio.h>
void main( )
{
int a, b, i, temp, count;
count = 0;
clrscr();
printf("Enter two numbers \n");
scanf("%d%d", &a, &b);
printf("Prime and non prime numbers between %d and %d are \n",a,b);
if(a>b)
{
temp=a;
a=b;
b=temp;
}
a++;
while(a<b)
{
for(i=1; i<=a; i++)
{
if(a%i==0)
count++;
}
if(count == 2)
printf("%d is prime \n",a);
else
printf("%d is not prime \n",a);
a++;
count=0;
}
getch( );
}

Output:

/*C Program to generate the fibonacci series and also to find the prime numbers in
it*/
#include<stdio.h>
#include <conio.h>
void main()
{
int a, b, c, n, m, i, count;
count = 0;
clrscr();
printf("Enter the terms \n");
scanf("%d", &n);
a=0;
b=1;
if(n == 0)
{
printf("no series \n");
}
else
{
if( n == 1)
{
printf("fibonacci series is \n%d \n",a);
}
else
{
printf("Fibonacci series and");
printf(" Prime numbers in the series\n");
printf("%d\n%d\n", a, b);
printf("%d is prime\n",b);
m=2;
while(m<n)
{
c = a+b;
printf("%d\n",c);
for (i=1; i<c;i++)
{
if(c%i == 0)
count++;
}
if(count == 2|| count == 1)
printf("%d is prime\n",c);
a=b;
b=c;
m++;
count=0;
}
}
getch( );

Output
/*C Program to calculate the sum of series 2/3!+3/4!+4/5!+......*/
#include <stdio.h>
#include <conio.h>
void main()
{
int j,i,n,m,p;
float sum, fact;
sum=0.0;
clrscr( );
printf("Enter the number of terms \n");
scanf("%d", &n);
if(n == 0)
printf("Cannot calculate the series \n");
m=2;
p=3;
for(i=1; i<=n; i++)
{
fact=1.0;
for (j=p;j>=1;j--)
{fact=fact*j;
}
sum=sum+m/fact;
m++;
p++;
}
printf("The sum of the series is %f\n",sum);
getch( );
}

Output
/*C Program to check whether a number is odd or even, prime or unprime*/
#include <stdio.h>
#include <conio.h>
void main( )
{
int i, n, r, count;
count = 0;
clrscr();
printf("Enter the number \n");
scanf("%d", &n);
for(i=1; i<=n; i++)
{
if(n%i == 0)
count++;
}
if (count == 2)
printf("%d is prime \n",n);
else
printf("%d is not prime \n",n);
r = n%2;
r ==0? printf("%d is even \n",n): printf("%d is odd \n",n);
n >0? printf("%d is positive \n",n): printf("%d is negative \n",n);
getch();
}

output:
Arrays
Till now we have seen different data types.
Variables of those data types are used to store the values.
Now assume that we want to store the marks of a group of students then instead of
giving different variable name to the marks of different students we can give a
common variable name to store the marks of this group of students.
Array can be used to do this.
An array is a group of related data items that share a common name.

Declaration of arrays:
Like any other variables, arrays must be declared before they are used.
The general form of array declaration is,
Data type variable name [size];
The data type specifies the type of element that will be contained in the array such
as int, float or char and the size indicates the maximum number of elements that can
be stored inside the array.
Variable name is the name given to the array.

For example:
int marks [100];
The above statement declares the marks to be an array containing 100 integer
elements.

Each of the elements in the array is accessed with the help of an index or subscript.
In arrays the first element has the subscript as 0. So marks[0] represents the first
element of the array marks, marks[1] represents second element, marks[2]
represents third element and so on. Thus according to the above declaration the last
subscript will be 99.
So any subscripts from 0 to 99 are valid.

One dimensional array:


A list of items can be given one variable name using only one subscript and such a
variable is called a single subscripted variable or a one dimensional array.

When we declare say int marks [4], the computer reserves 4 storage locations i.e., 8 bytes
because marks is an integer array.
int marks [4];

We can store the values in each of the location as given below


marks [0] = 77;
marks [1] = 78;
marks [2] = 67;
marks [3] = 81;
The subscript of an array can be an integer constant, integer variables like i, j, k or
expressions that yield integers.
Array elements are stored in contiguous memory locations.

Note:
C performs no bounds checking and therefore care should be exercised to ensure
that the array indices are within the declared limits. i.e. if we have declared an array
of marks[50] then we should use only from marks[0] to marks[49] but we should
not use any subscript which is above 49.

Similarly we can declare an array of real values as float array name[size]

Example:
float salary[100];
which declares salary as an array of 100 real elements.
Like wise we can declare an array of characters.

Example:
char strn [25] is an array named strn containing 25 character elements.

Note:
When declaring character array we must allow an extra-element space for the null
character ('\0').

Two-dimensional arrays:

These are used to represent table of values that is having more than one row
and one column.

Syntax:

Data type variable name [row size] [column size];

Assume if roll number and marks of students as shown in the table below are to be
stored then we can use an array say int marks[4][2];
Where 4 represents the number of rows and 2 represents the number of columns.

Col. 0 Col. 1
row 0 181 475
row 1 182 490
row 2 183 488
row 3 184 465
Because of the declaration as given above we can store 8 values.
We store roll number in the first column of the row and marks in the second column
of the row.
Since memory does not contain rows and columns whether it is a one-dimensional
or two-dimensional array the memory elements are stored in one contiguous
location.

/*Program to read the roll numbers and marks of students and calculate the average
marks obtained*/

#include
#include
void main ( )
{
int marks[4][2],sum,i,j;
float average;
sum = 0;
average = 0.0;
for (i=0; i<4;i++)
{
printf ("Enter the roll number & marks a student\n");
for (j=0; j<2; j++)
{
scanf ("%d",&marks[i][j]);
}
}
for (i=0; i<4; i++)
{
sum = sum + marks[i][1];
}
average = sum /4.0;
printf ("Average marks obtained by the students is %f\n ", average);
getch();
}

Output:
Initialization of arrays:
Array elements can be initialized in the same way as the ordinary variables when
they are declared.

Syntax:
static data type array name[size] = {list of values}

Example:
static int marks[5] = {77, 87, 67, 53,}

The above statement will declare the variable marks as an array of size 5 and assign 77 to
0th element, 87 to 1st element, 67 to 2nd element, 53 to 3rd element, 47 to 4th element. If
the number of values in the list is less than the number of elements then only that many
elements will be initialized and the remaining elements will be set to zero automatically
since the storage class is declared as static.

Example:
static float code[5] = {1.0, 2.0, 3.0};

The above statement will initialize first 3 elements to 1.0, 2.0 and 3.0 respectively
and the rest 2 elements to zero
.

When you are initializing the array elements the size may be omitted. In such cases
a compiler allocates enough space for all initialized elements.
static int counter[ ] = {1, 1, 1};
The above statement declares an array named as counter for which compiler will
allocate three locations each of two bytes because there are three elements inside the
flower brackets. The initial value of each element is 1.
As long as we initialize every element in the array this approach works fine. Thus
character arrays may be initialized as given below.

Example:
static char alphabets[ ] = {'x', 'y', 'z'};

Initializing 2 dimensional arrays:

Similar to one dimensional array, two dimensional arrays may be initialized by


following their declaration with a list of initial value enclosed in braces.

Example:
The examples given below shows the different types of initialization of two-
dimensional arrays.

static int table[2 ][2 ] = {1, 2, 3, 4};


In the case shown above first two elements represent two columns of first row and
the next two elements represents two columns of next row.
static int table[ 2 ][ 2 ] ={{1, 2}, {3, 4}};

In the above case two elements within the brace represent two columns of first row
and the next two elements within the brace represents two columns of next row. The
same concept can be extended to the next example.
static int table[ 2 ][ 2 ] ={
{1, 2},
{3, 4}
};

In the following example the first two columns of first row will be initialized to 1
and 2. The first column of the second row will be initialized to 3 and the second
column will be initialized to 0.
static int table [ 2 ] [ 2 ] ={
{1, 2}
{3}
};

/* Program to perform addition and subtraction on matrices*/


#include <stdio.h>
#include<conio.h>
void main( )
{
int a[10][10], b[10][10], c[10][10],i, j,m,n,p,q,choice;
char ch;
clrscr();
printf("Enter the order of the matrix a \n");
scanf("%d%d",&m,&n);
printf("Enter the order of the matrix b \n");
scanf("%d%d",&p,&q);
if((m!=p)||(n !=q))
{
printf("Order of matrix are not same so addition or subtraction is not possible \n");
}
else
{
printf("Enter the elements of the matrix a \n");
for(i=0; i<m;i++)
{
for(j=0; j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements of the matrix b \n");
for(i=0; i<p;i++)
{
for(j=0; j<q;j++)
{
scanf("%d", &b[i][j]);
}
} do{
printf("\t 1. Addition of matrix\n");
printf("\t 2. Subtraction of matrix\n");
printf(" Press 1 for Addtion and 2 for Subtraction---> ");
scanf("%d",&choice);
switch(choice)
{
case 1:
for(i=0; i<m;i++)

for(j=0; j<n;j++)
{
c[i][j]= a[i][j] + b[i][j];
}
}
printf("The resultant matrix of addition is\n");
break;
case 2:
for(i=0; i<m;i++)
{
for(j=0; j<n;j++)
{
c[i][j]= a[i][j] - b[i][j];
}
}
printf("The resultant matrix of subtraction is\n");
break;
}
for(i=0; i<m;i++)
{
for(j=0; j<n;j++)
{
printf("%d\t", c[i][j]);
}
printf("\n");
}
flushall();
printf("Press 'N' to terminate or any key to continue---> ");
ch=getchar();
} while(ch !='N');
}
getch();
}

Output 1:
Output 2:

/*C Program to multiply 2 matrices a & b and to store the result in c and to find the
transpose and norm of c*/

#include
#include
#include
void main( )
{
int a[10][10], b[10][10], c[10][10], d[10][10],i, j, k, p, q, m, n, sum;
float norm;
clrscr( );
printf("Enter the order of the matrix a \n");
scanf("%d%d", &m,&n);
printf("Enter the order of the matrix b \n");
scanf("%d%d", &p,&q);
if(m!=p)
{
printf("Matrix multiplication is not possible \n");
}
else
{
printf("Enter the elements of the matrix a \n");
for(i=0; i
{
for(j=0; j
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements of the matrix b \n");
for(i=0; i
{
for(j=0; j
{
scanf("%d",&b[i][j]);
}
}
for(i=0; i
{
for(j=0; j
{
c[i][j]=0;
for(k=0; k
{
c[i][j]= c[i][j]+ a[i][k]* b[k][j];
}
}
}
printf("The resultant matrix \n");
for(i=0; i
{
for(j=0; j
{
printf("%d\t", c[i][j]);
}
printf("\n");
}
for(i=0; i
{
for(j=0; j
{
d[i][j]= c[j][i];
}
}
printf("The transpose of the matrix c is \n");
for(i=0; i
{
for(j=0; j
{
printf("%d\t",d[i][j]);
}
printf("\n");
}
sum=0;
for(i=0; i
{
for(j=0; j
{
sum=sum+ c[i][j]* c[i][j];
}
}
norm=sqrt(sum);
printf("The norm of the given matrix is %f \n",norm);
}
getch();
}

Output

/*Program to count the number of words in a string */


#include <stdio.h>
#include <conio.h>
void main ()
{
char strn [100];
int word, i;
i = 0; word = 0;
printf ("Enter any string\n");
scanf("%[^\n]",strn);
while (strn[i]!= '\0')
{
if ((strn[i] == ' ' && strn[i+1]!= ' ') || strn[i+1] == '\0')
{
word++;
}
i++;
}
printf ("Number of words present in the entered string is %d\n", word);
getch();
}

Output 1

Handling of Charactor Strings


A string is an array of characters. Any group of characters (except double quote sign)
defined between double quotation marks is a constant string.

"He is good";
If you want to include a double quote in the string we may use it with a back slash.

printf ("\" he is good \" ");

Out put is:

"he is good"

operations performed on character strings are-

1. Reading and writing strings.


2. Combining strings together.
3. Copying one string to another.
4. Comparing strings for equality.
5. Extracting a portion of a string.
Declaring and initializing a string variable:

A string variable is any valid C variable name and is always declared as an array.
When the compiler assigns a character string to a character array, it automatically
supplies a null character '\0' at the end of the string.
Therefore, the size should be equal to the maximum number of characters in the
string +1.
This extra location is to store the null character '\0'. Character arrays may be
initialized when they are declared.

Example:
char name[25] = "RAMESH";
or
char name[25] = 'R', 'A', 'M', 'E', 'S', 'H', '\0'}
or
char name[ ] = "RAMESH";

Different String Handing Functions:


Different kinds of manipulations can be performed on strings.

C library provides string handling functions and few of which are listed below:

(1) strcat ( )
This function concatenates two strings. The process of combining two strings
together is called concatenation.

Syntax:
strcat (string1, string2);

Example:
char string1[25] = "he is good";
char string2[10] = " smart too";

Example 1:
strcat (string1, string2);

As shown in example1 string1 and string2 are character arrays, when the function
strcat( ) is executed, string2 is appended to string1.
It does so by removing the null character at the end of string1 and placing string2
from there.
The string at string2 remains unchanged.
Thus now string1 will have he is good smart too.
Example 2:
strcat (string1, "smart too");
The function strcat( ) can also append a string constant to a string variable as shown
in example2.

Note:
We must make sure that the size of string1 to which string2 or a string constant
appended, is large enough to accommodate the final string.

It is also possible to nest strcat( ) functions.

Example:
strcat (strcat (string1, string2), string3);
char string1[ ] = "he is"
char string2[ ] = "good"
char string3[ ] = "smart too"

All the three strings string1, string2 and string3 are combined together to form a resultant
string stored in string1.

(2) strcmp( )

This function compares two strings identified by the arguments and returns a value
0 if they are equal.
If they are not equal it returns the numeric difference between the first non-
matching characters in the string

. Syntax:
strcmp (string1, string2);
string1 and string2 may be string variables or string constants.

Example:
#include <stdio.h>
#include <conio.h>
void main()
{
char string1[ ] = "Mahatma";
char string2[ ] = "Mahatma";
int n;
n = strcmp (string1, string2);
if (n == 0)
{
printf("Strings are equal");
}
else
{
printf ("Strings are not equal");
}
}

Output

We can also compare a string variable with a constant as shown in the example
below.

strcmp (string1, "END");

In the above example the string constant END is compared with the contents of character
array variable string1.
If variable string1 contains the string END then the strcmp() function returns zero else it
returns a nonzero.

(3) strcpy ( )

This function works like a string assignment operator.


The source string is assigned i.e. copied on to the destination character array.

Syntax :
strcpy (string 1, string 2);

Example:
char string1[ ] = "He is good";
char string2[ ] = "hello";
strcpy(string1, string2);
The contents of string2 is assigned to string1, string2 can be a character array
variable or a string constant.

In the above example after the execution of the statement strcpy(string1, string2);
the character array variable string1 will contain hello overwriting earlier value of He
is good.

strcpy (string1 , "hai");


In the above example the string hai is assigned to string1 overwriting the earlier
value present in string1.
In both the cases, the size of the array string1 should be large enough to receive the
contents of string variable string2 or the string constant.

(4) strlen( )

This function counts and returns the number of characters in a given string.

Syntax:
n = strlen (string1);
Where n is an integer variable and string1 is an array of characters.

Example:
#include <stdio.h>
#include<conio.h>
void main()
{
char string1[ ] = "He is good";
int length;
length = strlen(string1);
printf("Length of the string is %d\n",length);
}

Where length is an integer variable, which receives the value of the length of the
string.
The argument can be a string variable or a string constant. The counting ends at the
occurrence of the first null character.
Similarly you can have string constant as argument for the function strlen() as
shown below.
length = strlen ("he is good");

Note:
All the string handling functions are defined in the header file string.h.
Searching and Sorting
Searching:

This is the process by which one searches the group of elements for the desired
element. There are different methods of searching but let us deal two popular
methods of searching and they are linear search and binary search.
Linear Search:
This is one of the simplest techniques for searching an unordered table for a
particular element.

In this each and every entry in the table is checked in a sequential manner until the
desired element is found.

Binary Search:
In binary search the basic requirement is the elements of the array should have been
sorted alphabetically or numerically in the ascending order.

In this technique the approximate middle entry of the array is located, and its key
value is examined. If its value is too high, then the key value of the middle entry of
the first half of the table is examined and the procedure is repeated on the first half
until the required element is found or the search interval becomes empty. If the
value is too low then the key of the middle entry of the second half of the array is
tried and the procedure is repeated on the second half. The procedure continues until
the desired key is found or the search interval becomes empty.

Program to find an element m in an array a of n elements and print it's position in the array.

/* C program to search an element using linear / binary search*/

#include <stdio.h>
#include <conio.h>
void main( )
{
int m,i, n, j, temp, flg, low, middle, high, a[100];

char ch;
clrscr( );
flg=0;
printf("Enter the size of the array \n");
scanf("%d",&n);
printf("Enter the elements of the array \n");
for (i=1; i<=n;i++)
{
scanf("%d", &a[i]);
}
do
{
printf("\nEnter the number which you want to search :");
scanf("%d", &m);
printf ("\nPress 'l' for linear search and 'b' for binary search :");
ch=getch( );
switch(ch)
{
case 'l':
for (i=1; i<=n;i++)
{
if(a[i]==m)
{
flg=1;
break;
}
}
if(flg== 1)
{
printf("\nThe element is found and the position is: %d",i);
printf("\n");
}
else
printf("\nElement is not found \n");
break;
case 'b':
for (i=1; i<=n-1;i++)
{
for (j=i+1;j<=n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\nThe sorted list is \n");
for (i=1; i<=n;i++)
{
printf("%d ",a[i]);

}
low=1;
high=n;
while (low<=high)
{
middle=(low+high)/2;
if(a[middle] == m)
{
flg = 1;
break;
}
else
{
if(a[middle]<m)
{
low=middle+1;
}
else
{
high=middle-1;
}
}
}
if(flg == 1)
printf("\nThe element is found and the position is : %d", middle);
else
printf("\nThe element is not found \n");
break;
default:
printf("\nWrong selection \n");
break;
}
flushall();
printf("\nPress 'Y' to continue and any other key to discontinue : ");
ch=getch( );
}while (ch == 'Y');
getch( );
}

Output:
Note:
Sorting is discussed next. For binary search to work the array should be in the
ascending order.

Sorting:
This is the operation of arranging the elements of a table into some sequential order
according to ordering criteria. The sort is performed according to the key value of
each of the elements. Depending on the structure of the key, elements can be sorted
numerically, alphabetically or alphanumerically. For example in numerical sorting,
the elements are arranged in ascending or descending order according to the
numerical value of each of the elements.
There are different techniques to perform the sorting. We are discussing bubble sort
in comparison with the selection sort technique.

Bubble sort differs from selection sort. In this instead of finding smallest element
and then performing an interchange two elements are interchanged soon after
discovering that they are out of order.
When the approach is used during the first pass the first and second element a[1]
and a[2] are compared if they are out of order then the two elements are inter
changed. This process is repeated for array of elements a[2] and a[3], a[3] and a[4]
until a[n-1] and a[n].
This process causes elements with smaller values to bubble up the array and hence
the name bubble sort. Thus after the 1st pass the largest element will be at the nth
position and on each of the successive pass the element with next largest value will
be placed in position n-1, n-2 ... so on. Where a is an array of n elements.
/* C program to sort the numbers using bubble sort*/

#include <stdio.h>
#include <conio.h>
void main( )
{
int i, j, temp, n, num, ar[100];
char ch;
clrscr( );
do
{
printf ("\nEnter the size of the array \n");
scanf ("%d", &n);
printf("Enter the elements of the array \n");
for (i=1; i<=n;i++)
{
scanf("%d", &ar[i]);
}
printf(" Select the option \n");
printf("1. Ascending order: \n");
printf("2. Descending order: \n");
scanf("%d",&num);
switch(num)
{
case 1:
for (i=1; i<=n-1;i++)
{
for (j=1; j<=n-i;j++)
{
if(ar[j]>ar[j+1])
{
temp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=temp;
}
}
}
printf("Sorted array in ascending order is \n");
for (i=1; i<=n;i++)
{
printf("%d ",ar[i]);
}
break;

case 2:
for (i=1; i<=n-1;i++)
{
for (j=1; j<=n-i;j++)
{
if(ar[j]<ar[j+1])
{
temp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=temp;
}
}
}
printf("\nSorted array in descending order is \n");
for (i=1; i<=n;i++)
{
printf("%d ",ar[i]);
}
break;
default:
printf("Wrong selection \n");
break;
}
printf("\n Press 'Y' to continue and any other key to terminate : ");
flushall( );
ch=getch( );
}while (ch == 'Y');
getch( );
} Output:

User Defined Function


C functions can be classified into two categories namely library functions and user defined
functions. The main distinction between these two categories is that library functions are
not required to be written by us whereas a user defined function has to be developed by the
user at the time of writing a program.

Whenever we develop our program we write main() function. This is a user defined
function. Every program must have a main function to indicate where the program has to
begin its execution.

A function is a self-contained block of code that performs a particular task. Once a function
has been designed and packed, it can be treated as a black box that takes some data from
the main program and returns a value. The inner details of operation are invisible to the rest
of the program.
The general form of the function is:

return type function name (argument list)


argument declaration;
{
local variable declaration;
executable statements;
:
:
return (expression);
}

Argument list and argument declaration are optional, it depends on the calling
function. Local variable declaration is required only when any local variable are
used in the function. A function may or may not send back any value to the calling
function. If it does, it does it through the return statement. While it is possible to
pass to the called function, any number of values, the called function can return one
value per call at the most. The return statement can take one of the following forms

return;
or
return (expression);

The first form does not return value. It transfers the control back to the calling
function. The second one returns the value of the expression to the calling function.

The return statement is the mechanism for returning a value to the calling function.
This is also an optional statement and its absence indicates that no value is being
returned to the calling function.

Note:
If a function is not returning any value to the calling function then it should be
specified in the return type.
We have to specify the return type as void.
By default the return type of a function is int.
So if a function is returning a value and it is not an integer then it should be
specified in the function header and it should specify the proper data type.
This information is also required to be specified in the beginning of the program.

Category of user defined functions:


Depending on the conditions like whether the arguments are passed to the called
function and whether the called function is returning the value back to the calling
function, the functions are categorized into following categories.

(i) Functions without arguments and without any return value.


In this the calling function is not passing any arguments to the called function and
the called function is not returning back any value to the calling function.
Example:
#include<stdio.h>
void main ( )
{
void message();
printf ("I am in MGM College\n");
message ( );
}
void message ( )
{
printf ("I am in C class \n');
}

Output:

In the above program the function message() is called from main(). The function
main() is not passing any value to the function message() and function message() is
also not returning back any value to the function main().

(ii) Functions with arguments and without any return value.


In this the calling function is passing one or more arguments to the called function
and the called function is not returning back any values to the calling function.

#include<stdio.h>
void main ()
{
int a, b;
void add();
a = 5;
b = 6;
add(a,b);
}
void add (x,y)
int x,y;
{
int z;
z = x+y;
printf ("Result is %d \n", z);
}

In the above program the function main() passes two arguments a and b to the function
add(). But the function add() does not return any value back to the function main().
Note :
In the above example add() is called function and main() is the calling function.

(iii) Functions with arguments and with return value.


In this the calling function is passing one or more arguments to the called function
and the called function will return back one value to the calling function.

# include <stdio.h>
void main ( )
{
int a, b, c;
int add();
a = 5;
b = 6;
c = add (a, b);
printf ("In main %d\n",c);
}
add (x, y)
int x, y;
{
int z;
z = x+y;
printf ("Result in function %d\n",z);
return (z);
}

In the above program the function main() passes two arguments a and b to function
add() and the values are received in the function add() through the variables x and y.
The variable z is local to the function add(). The function add() returns the result
back to the main() through the variable z and it is received at the function main()
though the variable c.

Output

Note 1:
In the above program, the arguments a and b are known as actual arguments and
arguments x and y are known as formal arguments. The values of actual arguments
are copied to formal arguments when the functions is called.

Note2:
Wherever function is not returning any value back to the main function we have
used the return type as void. This aspect is explained in the next section.

Overriding the default return type: By default C function returns a value of the
type int when no other type is specified explicitly. When no other type is specified
then it is assumed that the function is going to return a value of type int to the
calling function.

We must do two things to enable calling function to receive a non-integer

value from a called function.

(1) The explicit type specifier corresponding to the data type required must be
mentioned in the function header.

The Syntax is:


return type specifier function name (argument list)
declaration of argument list;
{
local variable declaration;
executable statements;
}
The return type specifier tells the compiler, the type of the data the function is
to return.

(2) The called function must be declared at the start of the body in the calling
function like any other variable. This is to tell the calling function, the type of
data that the function is actually returning.

Example1:
# include <stdio.h>
# include <conio.h>
void main ( )
{
float a, radius;
float area( );
printf(“Enter the radius\n”);
scanf("%f", &radius);
a = area (radius);
printf ("Area of circle is : %f\n",a);
getch ( );
}

Function: float area (r)


float r;
{
float ar;
ar = 3.14 *r * r;
return (ar);
}

Output:
In the above program the function area() which calculates the area of the circle
and returns the value of type float to the calling function main(). So the return
type specifier is mentioned in the function header as float it is given by the
statement float area(r);. It is also specified in the calling function main() that
the type of the value the function area() is going to return is float with
statement float area(); after the declaration of the variables.

Example 2:
void main ( )
{
void message( );
printf ("I am in MGM College\n");
message ( );
}
void message ( )
{
printf ("I am in C class\n");
printf ("I am enjoying C programming language\n");
printf("I am dreaming about writing good programs in C\n");
}

Output:
Similarly in the above program the function message() which just display the
messages does not return any value to the calling function main(). So the
return type specifier is mentioned in the function header as void as it is given
by the statement void message();. It is also specified in the calling function
main() that the function message() is not going to return any value with the
statement void message(); in the beginning of the function main.

Note:
Void is the keyword which is used when a function is not returning any value
to the calling function.

Scope Rule of functions:


Consider the following example:
# include <stdio.h>
void main ( )
{
int i;
void display();
i= 80;
display(i);
}
void display (j)
int j;
{
int k;
k = 25;
printf("j = %d\n", j);
printf("k = %d\n", k);
}
Output

In the program it is necessary to pass the value of the variable i to the function
display() and it is not automatically available to the function display. Because
by default, the scope of the variable is local to the function in which it is
defined. The presence of variable i is known only to the function main() and
not to any other function
.
Similarly, the variable k is local to the function display() and hence it is not
available to the function main(). That is why to make the value of i available to
display(), we have to explicitly pass it to the function display. Likewise, if we
want k to be available to the function main(), we will have to return it to
main(), using the return statement.

So In general, we can say that the scope of a variable is local to the function in which
it is defined.

/*C Program to print the GCD of 4 given numbers using function to calculate the GCD
of two numbers*/
#include <stdio.h>
#include <conio.h>
void main( )
{
int a, b, c, d, a1, a2, a3;
int gcd();
clrscr( );
printf("Enter the number \n");
scanf ("%d %d %d %d", &a, &b, &c, &d);
a1=gcd(a,b);
a2=gcd(c,d);
a3=gcd(a1, a2);
printf("GCD of 4 numbers is %d \n",a3);
getch( );
}
int gcd (p,q)
int p, q;
{
int g;
while (p != q)
{
if (p>q)
{
p=p - q; g = p; }
else
{
q = q - p;
g = q;
}
}
if ( p == q)
g = p;
return (g);
}

Output

/* C program to perform computation on matrices


1. addition
2. subtraction
3. multiplication*/
#include <stdio.h>
#include<conio.h>
void main( )
{
int a[10][10], b[10][10], c[10][10],i, j, m, n, p,q, choice, flg=0, ch;
void sum();
void sub();
void mul();
clrscr();
printf ("Enter the order of the first matrix\n");
scanf ("%d%d", &m,&n);
flushall( );
printf ("Enter the order of the second matrix\n");
scanf ("%d%d", &p,&q);
flushall( );
printf ("Enter the elements of the first matrix\n");
for (i=0; i<m;i++)
{
for (j=0; j<n;j++)
{
scanf("%d", &a[i][j]);
}
}
printf ("Enter the elements of the second matrix\n");
for (i=0; i<p;i++)
{
for (j=0; j<q;j++)
{
scanf("%d", &b[i][j]);
}
}
do
{
printf("1)Addition \t");
printf("2)Subtraction \t");
printf("3)Multiplication ");
printf("Enter Your Choice-->");
scanf("%d", &choice);
switch(choice)
{sum
case 1:
printf("Matrix Addition and ");
if((m == p) && (n == q))
{
(a, b, c, m, n);
flg = 1;
}
else
printf("Order of matrix do not match so addition is not possible \n");
break;
case 2:
printf("Matrix Subtraction and ");
if((m == p) && (n == q))
{
sub(a, b, c, m, n);
flg = 1;
}
else
printf("Order of matrix do not match so matrix subtraction is not possible
\n");
break;
case 3:
printf("Matrix Multiplication and ");
if((n== p))
{
mul(a, b, c, m, n,q);
flg = 1;
}
else
printf("Order of matrix do not match so matrix multiplication is not possible
\n");
break;
default: printf("Wrong Choice");
}
if (flg == 1)
{
printf("The resultant matrix is \n");
for (i=0; i<;i++)
{
for (j=0; j<q;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
printf("Press 'Y' to continue and any other key to terminate --->");
flushall();
ch=getchar();
}
while (ch == 'Y');
getch( );
}
/* Function for matrix addition */
void sum(d, e, h, f, g)
int d[10][10], e[10][10], h[10][10], f, g;
{
int i, j;
for (i=0; i<f;i++)
{
for (j=0; j<g;j++)
{
h[i][j] = d[i][j] + e[i][j];
}
}
}

/* Function for matrix subtraction */


void sub(d, e, h, f, g)
int d[10][10], e[10][10], h[10][10], f, g;
{
int i, j;
for (i=0; i<f;i++)
{
for (j=0; j<g;j++)
{
h[i][j] = d[i][j] - e[i][j];
}
}
}

/* Function for matrix multiplication */


void mul(d, e, h, f, g,l)
int d[10][10], e[10][10], h[10][10], f, g,l;
{
int i, j, k;
for (i=0; i<f;i++)
{
for (j=0; j<1;j++)
{
h[i][j] = 0;
for (k=0; k<g;k++)
{
h[i][j] = h[i][j] + d[i][k] * e[k][j];
}
}
}
}
Output

Recursion:
It is a process by which a function calls repeatedly the same function until
some specified condition has been satisfied.
The process is used for repetitive computation in which each action is started
in terms of previous result.

Example:

Program to find the factorial of an Integer using recursion


#include <stdio.h>
#include <conio.h>
void main()
{
int n;
long int fct;
long int factorial(int);
printf("Enter the integer\n");
scanf("%d",&n);
fct=factorial(n);
printf("Factorial of %d is %ld\n", n,fct);
getch();
}
long int factorial(m)
int m;
{
long int f;
if(m==0)
return(1);
else
}

f= m * factorial(m-1);
return(f);

Output:

Note:
There should be a terminating condition in the recursive function to come out
of the function.
If recursive function contains local variables a different set of local variables
will be created during each call.
The names of the local variables of course will always be same as declared
within the function. However the variable will represent set of values each time
a function is executed. Each set of values will be stored on the stack so that they
can be popped off the stack and used.

Program to generate fibanacci series using a recursive function


#include <stdio.h>
#include <conio.h>
void main()
{
int i,n,fib;
int fibanacci(int);
printf("Enter the number of terms for series\n");
scanf("%d",&n);
printf("Fibanacci series with %d terms\n",n);
for(i=1; i<=n; i++)
{
fib=fibanacci(i);
printf("%d\n", fib);
}
getch();
}
fibanacci( m)
int m;
{
int f;
if(m==1)
return(0);
if( m==2)
return(1);

f=fibanacci(m-1) + fibanacci( m-2); return(f);


}

Output:

Files
Opening and closing of data files:

Files are the area in secondary device where the data can be stored.
The first step is to establish a buffer area when working with a stream oriented data
files, where information is temporarily stored while it is transmitted between
computers memory and data files.

Because of the concept of buffer area the information can be written or read from
data files more rapidly. Otherwise the speed could not be achieved.

The buffer area is established by writing the statement FILE *ptr where FILE is a
special structure type that establishes the buffer area and ptr is a pointer variable
that indicates the beginning of the buffer area. The structure type FILE is defined
within the header file stdio.h.

The pointer ptr is often referred to as stream pointer or simply as a stream.

Note:
Use upper case letters to write FILE

A data file must then be opened before it can be created or processed. This
associates the file name with the buffer area.
It also specifies how the data file will be utilized i.e., read only, write only, append
or read/write etc.
The library function fopen() can be used to open the file in different modes.

Syntax:
ptr = fopen (file name, file type);
Where file name and file type are strings that represent the name of the data file and
the manner in which the data file will be utilized respectively.
The name chosen for the file name must be consistent with the values for naming
files as

determined by the operating system of the computer. The file type must be one of
the strings r, w, a, r+, w+, a+
r opens an existing file for reading only.
w opens a new file for writing only, if a file with specified file name exists, it will
be destroyed and a new file is created.

A new file will be created if a file with the specified file name does not exist.

r+ opens an existing file for both reading and writing. If a file with the given name
doesn't exist it will not be created newly.
w+ opens a new file for both reading and writing.
If a file with the specified file name currently exists it will be destroyed and a new
file will be created in its place.

a+ opens an existing file for reading and appending.


A new file will be created if the file with the specified file name does not exist.
The fopen() function returns a pointer to the beginning of a buffer area associated
with the file. A null value is returned if a file can't be opened i.e., when the given
file is not found.
Finally a data file must be closed at the end of the program.
This can be accomplished with the library function fclose().

The syntax:
fclose (pointer variable);

Example:
fclose (ptr);
The pointer variable associated with the respective file.

Creating a data file:


A data file must be created before it can be processed. This data file can be created
in two ways.
One way is to create the file directly using a text editor or a word processor.
The other is to write a program that enters information into the computer and then
writes it out into the data file.
If the data file consists of individual characters the library functions getchar() and
putc() can be used to recieve the data from the keyboard and to write it out into the
data file.

The syntax:
putc (character to be written into the data file, ptr);

Note:
ptr is the pointer variable associated with the respective file buffer

/*The following program accepts characters from the keyboard and writes into the
data file tst.dat*/
# include<conio.h>
#include <stdio.h>
void main ( )
{
FILE *ptr;
char c;
printf ("Enter the characters \n");
ptr = fopen("tst.dat", "w");
do
{
putc (c=getchar( ),ptr);
} while (c != EOF);
fclose (ptr);
getch();
}

Output:
Contents of the file tst.dat:

Read a data file and display its contents:


The library function getc() will read the individual characters from data file and
library function putchar() will display them onto the screen.

The Syntax:
c=getc (ptr);
Where c is a character variable and ptr is the pointer variable associated with the file
buffer.

/*The following example which reads the characters from the data file tst.dat and
displays the contents on the monitor */
#include <stdio.h>
#include <conio.h>
void main()
{
FILE *ptr;
char c;
if ((ptr=fopen("tst.dat", "r"))==0)
printf("File cannot be opened \n");
else
{
do
{
putchar (c = getc(ptr));
} while(c!= EOF);
fclose (ptr);
}
getch();
}

Output:

Program to
a) Read a line of characters from the keyboard and write it on to a file.
b) Read a line of characters from the file in which it is written and display it on the
screen.

# include <stdio.h>
#include <conio.h>
void main ( )
{
FILE *ptr;
char c;
printf ("Enter the characters\n");
ptr = fopen ("tst.dat", "w");
do
{
putc(c = getchar(),ptr);
} while (c!= EOF);
fclose (ptr);
printf("\nEntered characters written in the file\n\n\n");
printf("Characters are read from the file and displayed on the screen\n");v ptr =
fopen("tst.dat", "r");
do
{
putchar (c=getc(ptr)); } while (c!= EOF);
fclose (ptr);
}

Output:

Note:

1. After typing line of characters, to signify the EOF, press Ctrl key and holding it
down, press z.
2. Before opening a file in different mode you should close the file. So after the file
was opened in write mode, before opening it in the read mode it was closed. It is
always a good practice to close the file at the end of the program.

More functions used to perform input and output operations on files:


Data files containing records can be processed using the library functions fscanf()
and fprintf(). Thus fscanf() function permits formatted data to be read from data file
associated with particular strings and fprintf() permits formatted data to be written
to the data file. The actual format specifications are same as those used with the
scanf() and printf() functions.

/*C Program to read a text file and convert lowercase to uppercase letters*/
#include<stdio.h>
#include <conio.h>
#include <ctype.h>
void main( )
{
FILE *ptvar;
int i;
char a[100], c;
clrscr( );
i=0;
ptvar=fopen("text1.dat","w");
printf ("Enter the text \n");
do
{
putc(c = getchar( ), ptvar);
} while (c!=EOF);
fclose (ptvar);
ptvar=fopen("text1.dat","r");
do
{
c= getc(ptvar);
a[i]=c;
i++;
}while (c!=EOF);
a[i]='\0';
fclose(ptvar);
i=0;
while (a[i]!='\0')
{
(isupper(a[i]))? (a[i]=tolower(a[i])): (a[i]=toupper(a[i]));
i++ ;
}
printf("\nThe resultant string is\n %s",a);
getch( );
}

Output
/*C Program to read a text file and accumulate the occurrence of each character in
two dimensional array and output the frequency*/
#include <stdio.h>
#include <conio.h>
#include <ctype.c>
void main( )
{
FILE *ptvar;
char c;
int j, a=0, l=0, k=0,w;
static int b[26][26];
clrscr();
ptvar=fopen("text1.dat","w");
printf ("Enter the string : \n");
do
{
c = getchar( );
putc(toupper(c),ptvar);
if (c== '\n' || c==EOF)
{
l++;
}
}while(c!=EOF);
fclose (ptvar);
flushall( );
ptvar=fopen("text1.dat","r");
while((c=getc(ptvar))!=EO
{
while(c == '\n')
{
a++;
break;
}
k = c-65;
b[a][k]=b[a][k]+1;
}
printf("Frequency of characters is \n");
for(w=0; w<26;w++)
{
printf("%c ", (65+w));
}
printf("\n");
for(w=0; w<l-1;w++)
{
for(j=0; j<26;j++)
{ printf("%d ", b[w][j]);
}
printf("\n");
}
fclose(ptvar);
getch( );
}

Output:

Pointers
Pointer variables are the variables capable of holding the addresses. Before using a
pointer variable we must declare the variable as a pointer variable as we declare any
other variable. But we must use an indirection operator while declaring a pointer
variable.

Syntax :
Type specifier * name of the variable;

Example:
int *ptr;
The above declaration specifies to the compiler that ptr is pointer variable capable
of holding the address of an integer variable.

How do we store the address of a variable in a pointer variable?


We can use the ampersand ( & ) sign to specify the address of a variable i.e. by
prefixing the ampersand sign (&) with the variable of which we want to store the
address.

Syntax:
Pointer variable = & name of the variable of which we want to store the Address;

Example:
ptr = &i;
The above statement stores the address of the variable i in the pointer variable ptr.

Example:
# include <stdio.h>
#include<conio.h>
void main ( )
int i, *j;
i = 5;
j = &i;
printf ("Value stored in i = %d\n", i);
printf ("Address of i stored in j = %u\n", j);
printf ("Value of i obtained using the pointer variable j = %d\n", *j);
getch ( );
}

OutPut:

Note:
(1) The address of i is 8778 and the format specification character of %u i.e.
unsigned is selected because format specification character %d will display values
only up to 32768.
(2) The value of i is obtained indirectly with the help of the pointer variable j by
prefixing the indirection operator (*) with the pointer variable j i.e. *j.

The indirection operator * means value at address.

You can see the address of any variable in memory location by prefixing the
ampersand sign (&) to the variable. The following program shows it.

#include<stdio.h>
void main ( )
{
int i=3, *j;
j= &i;
printf ("Address of i is %u\n",j);
printf("Value of i is %d\n",*j);
printf ("Address of j is %u\n",&j);
}

Output:

In the above program you can get the address of the variable i by displaying the
contents of pointer variable j and you can even see the address of the pointer
variable j by prefixing the ampersand sign with the pointer variable j as shown in
the example.

Consider the following examples:


int *a;
char *c;
float *j;
All are pointer variables that is, variables capable of holding addresses. Addresses
are always whole numbers. So, when we declare float *j, it does not mean that j is
going to contain a floating point value. What it means is j is going to contain the
address of a floating point value. Similarly char *c means that c is going to contain
the address of a character value.

# include <stdio.h>
#include<conio.h>
void main ( )
{
int i,*ptr1;
float j,*ptr2;
char c,*ptr3;
i=5;
j=54.53;
c='A';
ptr1=&i;
ptr2=&j;
ptr3=&c;
printf("Address of i %u\n",ptr1);
printf("value of i is %d\n",*ptr1);
printf("Address of j %u\n",ptr2);
printf("value of j is %f\n",*ptr2);
printf("Address of c %u\n",ptr3);
printf("Value of c is %c\n",*ptr3);
getch() ;
}

Output:
Now although i occupies 2 bytes in memory, the statement ptr1 = &i; stores only
the address of the first byte i.e. 9308 in ptr1.
As we can see from the output memory locations addressed as 9308 and 9309 is
allocated to the integer variable i.
Similarly ptr2 = &j; stores only the address of first byte i.e., 9300 out of 4 bytes
allocated to j. As we can see from the output, memory locations addressed as 9300,
9301,9302 and 9304 is allocated to the float variable j; similarly the memory
location 9295 is allocated to the character variable c.
This address of the first byte is often known as the base address. Although ptr1 and
ptr2 are containing only the base address, *ptr1 and *ptr2 will allow the access to all
the bytes occupied by the integer variable
i and float variable j respectively. That is why, while defining the pointers, we are
defining it by the type of variable’s value it can hold.

Arithmetic on Pointers:
Pointer arithmetic is different than the normal arithmetic. This is because in pointer
arithmetic when you increment the pointer or decrement the pointer the quantity by
which the pointer increments or decrements depends on the type of the pointer on
which the operation is performed.
Increment/Decrement operation:
When you increment or decrement an integer pointer it increments or decrements by
two because each of the integer variables are assigned with two bytes. Similarly
when you increment or decrement a float pointer it increments or decrements by
four because each of the float variables are assigned with four bytes and when you
increment or decrement an character pointer it increments or decrements by only
one because each of the character variables are assigned with one byte.

Following examples explain this operation:

Example1:
# include<stdio.h>
#include
void main ( )
{
char c, *cc;
c = 'A';
cc = &c;
printf ("Address of c % u\n", cc);
printf ("Value of c is %c\n", *cc);
cc = cc+5;
printf ("Contents of cc %u after incrementing \n", cc);
getch ( );
}
Output:

In the above example address of the variable c is 8745 and when we add 5 to the contents
of pointer variable cc it is incremented to 8750.

Example2:
# include <stdio.h>
#include <conio.h>
void main ()
{
int i, *ii;
i = 5;
ii = &i;
printf ("Address of i is %u\n", ii);
printf ("Value of i is %d\n", *ii);
ii = ii+5;
printf ("Content of ii is %u\n", ii);
getch ();
}
Output:

In the above example address of the variable i is 8728 and when we add 5 to the
contents of pointer variable ii it is incremented to 8738 because the integer variable
is allocated two bytes. Thus 2 ? 5 = 10.

# include <stdio.h>
#include <conio.h>
void main ()
{
float f, *ff;
f = 55.54;
ff = &f;
printf ("Address of f is %u\n", ff);
printf ("Value of f is %f\n", *ff);
ff = ff+5;
printf ("Content of ff is %u\n", ff);
ff = ff-2;
printf ("Content of ff is %u\n", ff);
getch ();
}
Output:

In the above example address of the variable f is 9280 and when we add 5 to the
contents of pointer variable ff it is incremented to 9300 because the float variable is
allocated four bytes. Thus 4 ? 5 = 20. Similarly when the pointer is decremented by
two it's contents are reduced by 8 numbers which is shown in the above example i.e.
9292.

As discussed above in the examples addition of a number to a pointer or subtraction of a


number from a pointer is also possible like ff =ff+5; or ff =ff-2;
Note:
Following operations should not be performed on pointer variables.
(1) Addition of pointers.
(2) Multiplying a pointer with a number or another pointer variable.
(3) Dividing a pointer with a number or another pointer variable.

Passing Arguments to a function:


Arguments can generally be passed to functions in one of the two ways.
(1) Sending the values of arguments or call by value.
(2) Sending addresses of arguments or call by reference.

1. Sending the values of arguments or call by value: Whenever a function is called


with parameters passed to it and if the values of the parameters (variables) are
passed to the called function, such function calls are called as call by value. By
this what we mean is on calling a function we are passing the values of
variables to it.

Example:
#include <stdio.h>
#include <conio.h>
void main ( )
{
float radius;
void area();
printf("Enter the radius\n");
scanf("%f", &radius);
area (radius);
}
void area (r)
float r;
{
float ar;
ar = 3.14 *r * r;
printf("area of the circle is %f\n",ar);
}

Output

In the above example the value of the radius is copied on to the variable r in
the function area();
In this the value of the actual arguments in the calling function are copied onto
the formal arguments of the called function.
This means that even if we make changes to the values of the formal variables
in the called function the value of the actual arguments in the calling function
will not change.
If suppose the value of the variable radius is 5.0 when it was passed to the
function area(), but assume if we change the value of the variable r to 10.0 in
the function area(), this change will not reflect in the value of the variable
radius in the function main() and it is 5.0 only.

1. Sending addresses of arguments or Call by Reference:


Whenever a function is called with parameter passed to it and if the
addresses of the variables are passed to the called function such a
function calls are called as call by reference. By this what we mean is on
calling a function we are passing the address of the variable to it.
In this the address of the actual arguments in the calling function are
copied into formal arguments of the called function.
This means that using the formal variables in the called function we can
make changes in the actual arguments of the calling function.

Example:

# include <stdio.h>
# include <conio.h>
void main ( )
{
float radius;
void area();
printf("Enter the radius\n");
scanf("%f", &radius);
area (&radius);
}
void area (r)
float *r;
{
float ar;
ar = 3.14 *(*r) * (* r);
printf("area of the circle is %f\n",ar);
}

Output:
In the above example the address of the radius ( &radius- This ampersand
sign(&) prefixing the variable radius means that the address of the variable
radius) is copied on to the variable r in the function area(); In the function
area(), to catch this address the variable r should be a pointer variable so it is
depicted with float *r; to mean that r is the pointer variable.
In this the address of the actual arguments in the calling function are copied
onto the formal arguments of the called function. This means that if we make
changes to the values of the formal variables in the called function the value of
the actual arguments in the calling function will change.
If suppose the value of the variable radius is 5.0 when it was passed to the
function area() but assume if we change the value of the variable pointed by r
to 10.0 in the function area(), this change will reflect in the value of the variable
radius in the function main() because the pointer variable r is pointing to the
variable radius. Following example shows this.

Example:

# include <stdio.h>
# include <conio.h>
void main ( )
{
float radius;
void area();
radius=5.0;
printf("Value of radius before the call of function area is %f\n",radius);
area (&radius);
printf("Value of radius after the return from function area is %f\n",radius);
}
void area (r)
float *r;
{
float ar;
ar = 3.14 *(*r) * (* r);
printf("Area printed in the function area \n");
printf("Area of the circle is %f\n",ar);
*r=10.0;
ar = 3.14 *(*r) * (* r);
printf("Area of the circle is %f\n",ar);
}

Output:

Note:

In the above example value of radius when it was passed to the function area()
was 5.0. But once the area was calculated with this value the value of the radius
was changed with the help of the pointer variable r to 10.0 and again the area
was calculated. This new value is reflected in the value of actual argument
radius.

Program to interchange the value of two variables:

(1) Call by value method:


# include<stdio.h>
# include <conio.h>
void main ( )
{
int a, b;
void swap();
printf ("Enter the value for a and b\n");
scanf ("%d %d", &a, &b);
printf("Value of a and b before calling the function swap \n");
printf ("a = %d b= %d\n", a, b);
swap (a, b);
printf("Value of a and b aftre returning from the function swap \n");
printf ("a = %d, b = %d\n", a, b);
getch ( );
}
void swap (x, y)
int x, y;
{
int temp;
temp = x;
x = y;
y = temp;
printf("Value of x and y displayed in tthe function swap \n");
printf ("x = %d, y = %d\n", x, y);
}

Output:
(2) Call by reference:

# include<stdio.h>
# include <conio.h>
void main ( )
{
int a, b;
void swap ();
printf ("Enter the values for a and b\n");
scanf ("%d%d",&a,&b);
printf ("Values of a and b before invoking the function swap\n");
printf("a= %d b= %d\n",a,b);
swap (&a,&b);
printf ("Values of a and b after the return from swap\n");
printf("a= %d b= %d\n",a,b);
getch ( );v }
void swap (x, y)
int *x, *y;
{
int temp; temp = *x;
*x = *y;
*y = temp;v printf("Value of x and y in the function swap\n"); printf ("x = %d
y = %d\n", *x, *y);
}

Output:
In this, the address of a and b are copied on to x and y respectively. So
whatever changes are made at the locations pointed by x and y it will affect the
values of a and b.

/*Program to calculate the area and perimeter of the circle using call by
reference method */

# include<stdio.h>
# include <conio.h>
void main ( )
{
int r;
float area, peri;
void area_peri();
printf ("Enter the value of radius \n");
scanf ("%d", &r);
area_peri(&r, &area, &peri);
printf ("area is = %f perimeter is = % f\n", area, peri);
getch ( );
}
void area_peri (x, y, z)
int *x;
float *y, *z;
{
*y = 3.14 *(*x) * (*x);
*z = 2 * 3.14 * (*x);
}

Output
Pointers and Arrays:

When we define one-dimensional array, it is been allocated the consecutive


locations in memory. For example if we have declared an array int ar[5]; then
the compiler allocates 10 bytes as each location means two bytes as the data
type is integer. Following example explains this concept of contiguous
allocation.

#include<stdio.h>
#include <conio.h>
void main ()
{
int ar[5],*ptr, i;
ptr = &ar[0];
printf("Enter five elements for array\n");
for (i = 0; i<5; i++)
{ scanf ("%d", &ar[i]);
} for (i=0; i<5; i++)
{
//two methods of getting the address of each element
printf ("Address of array element %u\n", &ar[i]);
printf ("Contents of ptr %u\n", ptr);
printf ("Value at address pointed by ptr is %d\n",*ptr);
ptr++;
}
getch ();
}

Output:

As you can observe from the output that starting from memory location 9170
to 9179 ten bytes are allocated for the integer array named as ar with size as
five elements. The allocation is continuos memory location.

Consider the following example:

#include<stdio.h>
#include <conio.h>
void main ()
{
int ar[5], i;
void disp();
printf("Enter five elements for array\n");
for (i=0; i<5; i++)
{
scanf ("%d",&ar[i]);
}
disp (&ar[0]);
getch ();
}
void disp (ptr)
int *ptr;
{
int i;
printf("Contents of array printed in the function disp\n");
for (i=0; i<5; i++)
{
printf ("%d\n", *ptr);
ptr++;
}
}

Output:

In the above example, we are assigning the address of 0th element of array ar
to pointer variable ptr. Later we are incrementing the pointer variable ptr to
point to the next element of array ar. Thus the for loop will continue till the last
element of array ar is accessed using pointers. Thus by assigning ptr=&ar[0]
we have collected the base address of ar in the pointer variable ptr i.e. in the
main() function disp() is called by passing the address of the starting element of
array ar[ ] and in the function disp() the address is received by the pointer
variable ptr.

Note:

ptr = &ar and ptr=&ar [0] are equivalent.


Accessing array element by pointer is always faster than accessing them by
subscript.
/* Program to display the contents of an array from last to first using
pointers*/

# include<stdio.h>
#include <conio.h>
void main ()
{
int ar[5], *ptr, i;
printf ("Enter five elements \n");
for (i=0; i<5; i++)
{
scanf ("%d", &ar[i]);
}
ptr=&ar[4];
printf ("Entered elements displayed in reverse order \n");
for (i=0; i<5; i++)
{
printf ("%d\n", *ptr);
ptr--;
}
getch ();
}

Output:
In the above example, we are assigning the address of the last element of
array ar to the pointer variable ptr and in the for loop, we are accessing
the last element of array ar to the first element of ar by decrementing
the pointer.
Passing an entire array to function:

# include<stdio.h>
# include <conio.h>
void main ()
{
int ar[5] = {1,2,3,4,5}, m=5;
void disp();
disp (&ar[0],m);
}
void disp(ptr, n)
int *ptr, n;
{
int i;
for (i=0; i<n;i++)
{
printf ("Address of ar[%d]= %u\n",i,(ptr+i));
printf (" ar[%d]= %d\n",i, *(ptr + i));
}
}

Output: :

2.
/* Program to send the address of one element at a time to the function
and get the value printed in the function.*/

# include<stdio.h>
# include <conio.h>
void main ()
{
int i, *j;
void disp ();
int ar[5]= {1,2,3,4,5};
for (i=0; i<5; i++)
{
j= &ar[i];
disp(j);
}
getch ();
}
void disp (ptr)
int *ptr;
{
printf ("%d\n", *ptr);
}

In the above example, during the first iteration of for loop in main() we are
passing the address of the 0th element of the array ar[ ] to the function disp()
and using the pointer the 0th element of array ar[ ] is displayed. So in the
function disp(), ptr is the pointer variable which will have the address of the
0th element of array ar[ ]. During the successive iterations of for loop,
addresses of each and every element of array ar[ ] is passed to the function
disp() and the element is displayed. Thus, by putting for loop in the main, we
are passing the address of 1 element at a time.

Let us consider a program to find the length of a string using pointers


and functions.

# include <stdio.h>
# include <conio.h>
void main ()
{
char strn[100], *p;
void length();
printf ("Enter the string :\n");
scanf ("%[^\n]", &strn);
p = &strn[0];
length(p);
getch();
}
void length(ptr)
char *ptr;
{
int len=0;
while (*ptr != '\0')
{
len++;
ptr++;
} printf ("Length of the string = %d\n", len);
}

Output:

In the above example the base address of the array strn[ ] is passed to the
function length() using the pointer variable p in the main(). The address is
received in the function length() by the pointer variable ptr. Simple while loop
is used in the function length() to advance the pointer ptr till the end of the
string as we know that the string is terminated by a null (‘\0’) character. The
contents of pointer are increased till a null character is encountered. The
length of the string is printed in the function length() itself.
Let us consider again the program to find the length of a string using pointers
and functions. But in this we are returning back the value of length of the
string back to the function main() through a return statement in function
length() and the value sent back from the function is received by the variable
len and it’s value is printed in the main().

# include <stdio.h>
# include <conio.h>
void main ( )
{
char strn[100]; int len=0;
int length();
printf("Enter a string \n");
scanf ("%[^\n]", &strn);
len=length(&strn[0]);
printf ("Length of the entered string is = %d\n",len);
getch ( );
}
int length(ptr)
char *ptr;
{
int ln=0;
while (*ptr != '\0')
{
ptr++;
ln++;
}
return(ln);
}

output:
Note:

Instead of a separate pointer variable p in the function main() we are directly


sending the address of the first element of the array to the function length in
the function call itself.

Let us consider again another program to find the length of the string. In this
program the length of the string is printed in the main() function itself. Here
both the arguments passed to function length() are pointers.

# include<stdio.h>
# include <conio.h>
void main ( )
{
char strn[100]; int len=0;
void length();
printf("Enter a string \n");
scanf ("%[^\n]", &strn);
length(&strn[0], &len);
printf ("Length of the entered string is = %d\n",len);
getch ( );
}
void length(ptr1, ptr2)
char *ptr1;
int *ptr2;
{
while (*ptr1 != '\0')
{
*ptr2 = *ptr2+1;
ptr1++;
}
}

Output:

In the above program ptr1 and ptr2 are the pointers in the function length(),
ptr1 pointing to the array variable strn[ ] and ptr2 is pointing to the variable
len. Here the length is printed in the main() function and value of the variable
len is not exclusively returned back to main() but it is available because the
value of variable len is changed in the function length() by the statement
*ptr2= *ptr2 + 1.

Pointers and two-dimensional array:


Elements 1 2 3 4 5 6 7 8 9 10
Assumed Memory 150 152 154 156 158 160 162 164 166 168

Consider a two dimensional array having five rows and two columns for
example int test[5][2]; The elements and the assumed memory locations are
given above.
Since the memory is allocated continuously first four memory locations (150,
151, 152, 153 as each element is allocated two bytes because it is an integer
array) correspond to first row, next four memory (154,155,156,157) locations
correspond to second row and last four memory locations (166,167,168,169)
correspond to last row. Thus two elements each are considered as one-
dimensional array of two elements each.
Consider the program given below it displays the address of each one
dimensional array.

# include<stdio.h>
#include <conio.h>
main ( )
{
int test [5][2] = { {1,2}, {3,4}, {5,6}, {7,8}, {9,10} }; int i;
for (i=0; i<5; i++)
{
printf (“Subscript & address %d %u\n”, i, test[i]);
}
getch ( );
}

Output of the Program:

When we are talking of pointers with 2 dimensional array, C language has a


powerful capability of treating each row of a 2-dimensional array as a one
dimensional array. This is a very important fact if we wish to access array
elements of a two-dimensional array using pointers. Thus, the declaration int
test [5][2] can be considered as setting a five one-dimensional array, each of
which is a one-dimensional array of two elements long array. We refer to an
element of one-dimensional array using a subscript. Similarly, if we can
imagine test to be a one-dimensional array then we can refer to its 0th element
as test[0], the next element as test[1] and so on. By writing like this you will get
the address of 0th element, 1st element and so on.
Analyzing the output of above program, the compiler knows that test is an
integer array. So, each element of this array occupies 2 bytes. There are 2
elements in a row. So, each row takes 4 bytes. Thus each row starts 4 bytes
further along from the last one as can be shown in the earlier shown memory
map of the array.

The compiler knows how many columns are there in the array since we
have specified it in the array declaration. So it interprets test [0] at
address 8698 and test [1] at 8702 and so on. So, now we have reached
each individual row and we have to refer to the individual elements of
this row. Suppose we want to refer to the element test [2][1] using
pointer notation then, we know that test [2] will give the address 8706,
the address of second one-dimensional array. So by writing 8706+1 we
will get the address 8708. (Since it is not an ordinary arithmetic but
pointer arithmetic). So it is now test [2]+1 will give us the address 8708
and value at this address can be obtained by * (test [2]+1). The above
expression gives us the value 6, which is stored at that array location.
This explanation is shown with the program given below.

# include<stdio.h>
#include <conio.h>
void main ( )
{
int test [5][2] = {
{1,2},
{3,4},
{5,6},
{7,8},
{9,10}
};
int i, j;
for (i=0; i<5; i++)
{
for (j=0; j<2; j++) {
printf ("Subscript and address %d %u", i, (test [i]+j));
printf ("Element at address is= %d\n", * (test [i]+j));
}
}
getch ();
}

Output of the Program:


Note:

The address allocated for the array is different in this because each time you
execute a program the address allocated may be different. But the concept
behind the explanation remains the same.

Structures and Unions


Why do we need a structure?

We have seen till now that a variable can be of single data type either an int or float
or char etc.
But if we want to store information consisting of different data types as a single
entity then what do we do? Consider the example where we want to store the
information of an employee of an organization and the employee details included
employee number, employee name his address etc.
so C provides a data type called 'structure'.
A structure gathers together, different types of information that comprise a given
entity.

A structure is a collection of one or more variable, possibly of different types


grouped together under a single name for the convenience.

Syntax to declare a structure is:


struct name of the structure {
structure elements;
};

Example:
struct employee {
int emp_no;
char emp_name[25];
};

Once the structure data type has been defined one or more variables can be declared
to be of that type.

For example the variables e1, e2, e3, e4 can be declared to be of the type struct employee as
struct employee e1,e2, e3,e4;

The above statement set aside 27 bytes of memory for each of the variable e1, e2, e3 and e4
as each of the variable need to store an integer value and a string i.e. two bytes for integer
and a character array of 25 bytes.

Different ways of defining the structures and their variables


We can also combine definition of structure type and the declaration of the variables
in one statement.

Example:
struct employee {
int emp_no;
char emp_name[25];
} e1, e2, e3, e4;
If we are combining definition of structure type and the declaration of the variables
in one statement then we can even omit the name of the structure type.

Example:
struct{
int emp_no;
char emp_name[25];
} e1, e2, e3, e4;

Structure variables can also be initialized at the time of declaration.

Example:
struct employee {
int emp_no;
char emp_name[25];
} ; struct employee e1= { 101, "Krithika"};
struct employee e1= { 101, "Gokul"};

Example:
struct employee {
int emp_no;
char emp_name[25];
} e1, e2,e3= { 101, "Krithika"};

Note:
In the above example only the variable e3 is initialized.

We can even assign the value of one structure variable to another.


Example:
struct employee {
int emp_no;
char emp_name[25];
} e1, e2,e3= { 101, "Krithika"};
e2=e3;

Note:

1. Semicolon must follow the closing brace of the structure type definition
2. A structure type defines only the form of a structure and compiler will not reserve
any space in memory

Accessing the structure elements: To access the elements of structure dot operator is used
along with the structure variable.

Example:
e1.emp_no;
e1.emp_name;
Whatever be the elements of a structure, they are always stored in contiguous
memory locations.

The example given reads the elements of the structure and displays the elements.
#include <stdio.h>
main ( )
{
struct employee
{
int emp_no;
char emp_name [25];
};
struct employee e1;
printf ("Enter the Employee number and name\n");
scanf ("%d%s",&e1.emp_no,&e1.emp_name);
printf("Employee Number is %d and Name is %s\n",e1.emp_no, e1.emp_name);
}

Output:

One structure can be nested within another structure:


A structure can be the element of another structure, which is given in the example.

Example:
struct empdt
{
char emphname [10];
int hnum;
};
struct employee
{
char empname [25];
int empno;
struct empdt empad;
} e1;

Program to read in the values for the employee details and display the read in values.
# include<stdio.h>
# include<conio.h>
main ( )
{
struct empdt
{
char emphname[10];
int hnum;
};
struct employee
{
char empname [25];
int empno;
struct empdt empad;
}e1;
printf ("\nEnter the name, number, housename and number\n\n");
scanf("%s%d%s%d",&e1.empname,&e1.empno,&e1.empad.emphname,
&e1.empad.hnum);
printf("\n\nEntered details of employee\n\n");
printf ("Employee Name = %s\n",e1.empname);
printf ("Employee Number = %d\n",e1.empno);
printf ("Employee House Name = %s\n",e1.empad.emphname);
printf ("Employee House Number = %d\n",e1.empad.hnum);
}

Output:

Array of Structures:
Structures can be stored in the array when there are more number of records or
structures to be stored.
Example:
# include<stdio.h>
# include<conio.h>
main ( )
{
struct empdt
{
char emphname[10];
int hnum;
};
struct employee
{
char empname [25];
int empno ;
struct empdt empad;
}e1[25];
int i,n;
clrscr();
printf("Enter the number of records you want to input\n\n");
scanf("%d",&n);
printf("Enter %d number of records\n\n",n);
printf ("\nEnter the name, number, housename and number\n\n");
for(i=0;i< n; i++)
{
scanf("%s%d%s%d",&e1[i].empname,&e1[i].empno,&e1[i].empad.emphname,&e
1[i].empad.hnum);
}
printf("\n\nEntered details of employee\n\n");
for (i=0; i<n;i++)
{
printf ("Employee Name = %s\n",e1[i].empname);
printf ("Employee Number = %d\n",e1[i].empno);
printf ("Employee House Name = %s\n",e1[i].empad.emphname);
printf ("Employee House Number = %d\n",e1[i].empad.hnum);
}
getch();
}

Output:
In the above program, in memory, compiler reserves space for two structures of
type employee.

Passing Structures To Function:


Like an ordinary variable, a structure variable can also be passed to function. We
may either pass individual structure element or entire structure at a time.
Program to pass individual structure element to the function.

#include<stdio.h>
# include<conio.h>
main ()
{
void disp1(char *, int);
void disp2(char *, int *);
struct employee
{
char empname[25];
int empno;
};
struct employee a;
clrscr();
printf("\nEnter the Name and Number\n\n");
scanf("%s%d",&a.empname,&a.empno);
disp1(a.empname,a.empno);
disp2(a.empname,&a.empno);
getch();
}
void disp1(s,t)
char *s;
int t;
{
printf("\nEntered details are\n\n");
printf ("Employee Name = %s Number = %d\n", s, t);
}
void disp2(s,t)
char *s;
int *t;
{
printf("\nEntered details are\n\n");
printf ("Employee Name = %s Number = %d\n", s, *t);
}

Output:

To the function disp1() Employee name (empname) is passed by the method call by
reference and Employee number (empno) is passed by the method call by value.
To the function disp2() both Employee name (empname) and Employee number
(empno) is passed by the method call by reference.

Note:
When you write a.empname, we get the base address of the array variable
empname, so naturally it is call by reference in the calls for disp1() and disp()2.

#include<stdio.h>
# include<conio.h>
struct employee
{
char empname[25];
int empno;
};
main ()
{
struct employee a;
void disp1(struct employee);
clrscr();
printf("\nEnter the Name and Number\n\n");
scanf("%s%d",&a.empname,&a.empno);
disp1(a);
getch();
}
void disp1(e)
struct employee e;
{
printf("\nEntered details are\n\n");
printf ("Employee Name = %s Number = %d\n",e.empname,e.empno);
}

Output:

Note:
Structure is defined outside the main so that it will be available to the entire
program i.e. to main and to the function disp1().

Structure Pointers:
The pointer pointing to a structure is called as structure pointer. It is like we have a
pointer pointing to an int or a pointer pointing to a char or a pointer pointing to a
float. Likewise, we have a pointer pointing to a structure. The example
demonstrates the use of structure pointers.

# include<stdio.h>
#include<conio.h>
struct employee
{
char empname [25];
int empno;
};
main ()
{
struct employee a, *ptr;
ptr = &a;
printf ("Enter the empname & empno\n");
scanf("%s %d", &a.empname, &a.empno);
printf("Employee name%s Employee number%d\n",ptr->empname, ptr->empno);
getch ( );
}

Output:

To pass a structure variable to a function by referenc:


# include<stdio.h>
# include <conio.h>
struct employee
{
char empname[25];
int empno;
};
main ( )
{
void disp (struct employee *);
struct employee a;
printf ("Enter empname and empno\n");
scanf ("%s %d", &a.empname, &a.empno);
disp (&a);
getch();
}
void disp (ptr)
struct employee *ptr;
{
printf("Employee name is %s \n", ptr->empname);
printf ("Employee number is %d\n",ptr->empno);
}

Output:

The address of the structure variable a is passed to the function disp().

Self Referential Structures -


This is a structure in which one member is a pointer to the parent structure type.

Syntax :
struct tag
{
member 1;
member 2;
:
:
struct tag *next;
};

Where *next is the pointer variable. Thus, the structure of type tag will contain a
member that points to another structure of type tag. Such structures are known as
self-referential structures.

Example:
struct employee
{
char empname [25];
int empno;
struct employee *next;
};

Note:
These kinds of structures are used to create data structures like linked lists, stacks,
queues etc.

UNIONS:
Similar to structures unions contain members which may have different datatype.
But the difference is that all the members share the same storage area within the
memory of the computer. br>Where as in structures each of the members are
allocated a separate storage area within the memory.

Thus unions are used to conserve the memory.


There may be applications in which all the data members may not be used at a given
point of timei.e. all the data members are not assigned the values.
In such cases instead of using Structures, Unions are used.

Syntax to declare a union:


union tag {
data member1;
data member2;
. data membern;
};

Syntax to declare a variable:


union tag variable1, variable2, varibale3...variablen;
Example:
union id{
char make[15];
int size;
};

A union can be embedded within a structure as shown below:


union id{
char make[15];
int size;
};
struct wear{
char color[15];
int price;
union id details;
};
struct wear shirt;

/* Program to demonstrate the use of union*/


#include<stdio.h>
#include<conio.h>
void main()
{
union id{
char make[15];
int size;
};
struct wear{
char color[15];
int price;
union id detai ls;
};
struct wear shirt;
printf("Enter Color, Make, Cost and Size of the shirt\n");
scanf("%s%s%d%d",&shirt.color,&shirt.details.make,&shirt.price,&shirt.details.siz
e);
printf("Color: %s, Cost: %d,",shirt.color,shirt.price);
printf(" Make: %s, Size: %d\n",shirt.details.make,shirt.details.size);
printf("Enter Make\n");
scanf("%s",&shirt.details.make);
printf("Color: %s, Cost: %d,",shirt.color,shirt.price);
printf(" Make: %s Size: %d\n",shirt.details.make,shirt.details.size);
getch();
}

Output:

By looking at the output of the program we can make out that at a given point of time only
one field of the union can have the value.
As seen in the above example although the value for make and size were given only the
value of size is displayed correctly and we got garbage value for the make.
But next time when the value for make was given, value of correct value for make is
displayed but for size we got garbage value.
Thus either the union field make will have correct value or size will have correct value but
not the both.
Most recently used union field will retain the value.

Dynamic Memory Allocation


Consider an array declaration say int marks[100]; such a declaration would reserve 200
bytes in memory to store 100 integers, say it is to store the marks of 100 students. But when
a program is executed we may want to store the marks of only say 50 students. That means
we have made use of only 100 bytes and 100 bytes are wasted or if we want to store the
marks of 200 students, it is not possible to store because the array size is only for storing
the marks of 100 students. So, when we declare arrays in the beginning static memory
allocation takes place. But memory can be allocated only at the time of execution of the
program and this is called as dynamic allocation. This kind of allocation can be done by
using the library function malloc() or calloc() and these are known as dynamic memory
allocation function. Thus the functions malloc() and calloc() allocates block of memory.
malloc() :

This allocates single block of memory

Syntax:
void * malloc (byte_size);
It takes the form ptr=(cast type *)malloc(byte_size)
Function malloc() returns a pointer to space for an object of size byte_size or null if the
request cannot be satisfied. The space is not-initialized.

Example:

ptr=(int *) malloc(50*sizeof(int));

The above statement will allocate a block of 100 bytes of memory and it's starting address
will be stored in the pointer variable of type int. The cast type specified is int.

calloc():This allocates multiple blocks of storage, each of same size.


Syntax:
void * calloc (n, element size);

It takes the form ptr=(cast type *)calloc(n,element_size);


Function calloc() returns a pointer to space for an array of n objects, each of size
element_size or null if the request cannot be satisfied. The space is initialized to zero.

Example:
ptr=(int *)calloc(5,sizeof(int));

The above statement allocates contiguous space for 5 blocks each of size 2 bytes.

realloc():

This changes the size of the object pointed to by ptr to new_size


.

Syntax:

void * realloc (void *ptr, newsize);

It takes the form ptr=realloc(ptr,new_size);


Function realloc() changes the size of the object pointed to by ptr to new_size The contents
will be unchanged up to the minimum of the old and new sizes. If the new size is larger, the
new space is un initialized. realloc() returnes a pointer to the new space or null if the
request cannot be satisfied in which case, *ptr is unchanged.
free(): This deallocates the space pointed to by ptr.
Syntax:
void free (void *ptr);

The function free() de-allocates the space pointed to by ptr, it does nothing if ptr is null. ptr
must be a pointer to space previously allocated by malloc(), calloc() or realloc().

All the above dynamic memory allocation functions are defined in header files alloc.h or
stdlib.h.

Example:
#include <stdio.h>
# include <conio.h>
# include <alloc.h>
void main ( )
{
int *ptr,n,i;
printf ("Enter the number of items \n");
scanf ("%d",&n);
ptr = (int*) malloc(n* sizeof(int));
printf("Enter %d number of elements\n",n);
for (i=0; i<n;i++)
{
scanf ("%d",ptr+i);
}
printf("Entered elements are\n");
for (i=0; i<n;i++)
printf ("%d\n",*(ptr+i));
}
getch();
}

Output:
The function malloc() returns a null if memory allocation is unsuccessful and if successful
it returns the address of the memory chunc (block) that was allocated. This address in the
above program is collected in the pointer variable ptr. The expression (int *) is used to type
cast the address being returned as the address of the integer. So, in the above example, ptr
will have the starting address of the memory location.
The calloc() function works exactly similar to malloc() except for the fact that it needs two
arguments as against one argument required by malloc().

Example:
ptr = (int *) calloc (10,2);
dd>
In the example , (int *) indicates that we wish to allocate the memory for storing integers. 2
indicates the size of the data type i.e., integer, 10 indicates that we want to reserve space for
storing 10 integers.
Another difference between malloc() and calloc() is that, by default memory allocated by
malloc() contains garbage value whereas that allocated by calloc() contains all zeroes.

Write a program to read the marks of students and calculate the average marks. Use
calloc() function to allocate the space.
# include <stdio.h>
# include <conio.h>
# include <alloc.h>
void main ( )
{
int *ptr, i, n, total = 0;
float avg = 0.0;
printf ("Enter the number of students \n");
scanf ("%d",&n);
ptr = (int*)calloc(n,2);
printf ("Enter the Marks for %d number of students \n",n);
for (i=0; i
{
scanf ("%d",ptr+i);
total = total + *(ptr + i);
}
printf ("Marks are : \n");
for (i=0; i
{
printf("%d\n",*(ptr + i));
}
avg = total/n;
printf ("Average = %f\n", avg);
getch();
}

Output

Command Line Parameters


The empty paranthesis of function main() may contain special arguments that allow
parameters to be passed to main() from the operating system level.
There are two such arguments, which are traditionally called argc and argv
respectively.
The first of the argument argc must be an integer variable and the second argument
argv is an array of pointers to strings. Each element in this array will represent a
parameter that is passed to main().
The value of argc will indicate the number of parameters passed from the operating
system level.
Example:
main (int argc, char *argv[ ])
{
:
:
}

The execution of program is normally initiated by specifying the name of the


program at the operating system level.
The program name is interpreted as an operating system command.
Hence the line in which it appears refers to as command line.

The parameters must follow the program name on the command line in order to pass
one or more parameters to the program when the program execution is initiated.

Syntax:
program name parameter1 parameter2 ...................parameter n

The individual parameters must be separated by one another either by blank spaces
or by tabs.
Blank spaces can be included within a parameter by enclosing the entire parameter
within quotation marks.

The program name may be stored as the first item in argv followed by each of the
parameters.
Hence, if the program name is followed by n parameters there will be n+1 entries in
argv ranging from argv[0] to argv [n].
Moreover, argc will automatically be assigned the value n+1.

Note:
The value for argc is not supplied explicitely from the command line.

Example:
#include <stdio.h>
# include<conio.h>
main (int argc, char *argv[])
{
int count;
printf ("Number of arguments %d \n", argc);
for (count = 0; count <argc;count++)
{
printf ("argv[%d] = %s\n", count,argv[count]);
}
getch( );
}

If the input given at the command prompt is >cmdarg1 Suvarni Krithika Gokul then the out
put will be as shown below.

Note:

1. Create an exe file of the program i.e. after compiling the program go for the make
option and create an exe file.
2. Go to DOS prompt. Be in the proper directory if the path is not set and type in the
filename (for example cmdarg1 is the file name) and the parameters (cmdarg1
Suvarni Krithika Gokul). Here Suvarni, Krithika and Gokul are the parameters.

If the command line parameters are given as> cmdarg1 Suvarni “Krithika Gokul”
The output will be:
Program to read a data file and display its contents. Use command line parameters to pass
in the file name.
# include<stdio.h>
#include <conio.h>
main (int argc, char *argv[])
{
FILE *ptvar;
char c;
printf ("Number of arguments passed %d\n", argc);
if ((ptvar = fopen(argv[1],"r")) ==0)
printf ("Error opening the file \n");
else
{
do
{
c=getc(ptvar);
putchar(c);
} while(c!= EOF);
fclose (ptvar);
}
getch();
}

We have given the parameter as >cmdarg2 cmdarg1.c


The Output is:
The parameter cmdarg2 is the filename of the program and cmdarg1.c is the name
of the file of which we want to display the contents.
You can see the output we are reading the contents of the file cmdarg1.c (our earlier
program).

Some Programs
Annexure:

1. program to convert hexadecimal/octal number to decimal equivalent


2. Program to find the length of the given string
3. Program to convert Upper case letters to lower case letters and vice versa
4. program to compare two strings
5. Program to concatenate two strings
6. Program to find the number of vowels in a given string
7. Program to convert first character of a word to capital letter
8. Program to reverse the given string and check whether it is a palindrome
9. Program to add and multiply two complex numbers
10. Program to add and multiply two complex numbers using structure pointers
11. Program to add and subtract matrices using functions and pointers
12. Program to read the contents of the file & send it to the printer (parallel port)
13. Program to copy the contents from one file to another using command line
arguments
14. Program to count the number of bit set and unset in an integer

/* C program to convert hexadecimal /octal number to decimal equivalent*/


#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <math.h>
void main( )
{
int i , len, deci, z, d, j,flg1,flg2;
char a[25], c;
clrscr( );
flg1=0;
flg2=0;
do {
printf("Enter Hexadecimal number: ");
scanf("%s",&a);
flushall();
i=0;
len=0;
while (a[i]!='\0')
{
len++;
i++;
}
len--;
deci=0;
d=0;
z=0;
for (i=len; i>=0; i--)
{
c=tolower(a[i]);
z=pow(16,d);
j=a[i]-48;
if((c >='a' && c <='f')|| (j <= 9))
{
switch (c)
{
case 'a':deci=deci+10*z; break;
case 'b':deci=deci+11*z; break;
case 'c':deci=deci+12*z; break;
case 'd':deci=deci+13*z; break;
case 'e':deci=deci+14*z; break;
case 'f':deci=deci+15*z; break;
}
switch(j)
{
case 1:deci=deci+1*z; break;
case 2:deci=deci+2*z; break;
case 3:deci=deci+3*z; break;
case 4:deci=deci+4*z; break;
case 5:deci=deci+5*z; break;
case 6:deci=deci+6*z; break;
case 7:deci=deci+7*z; break;
case 8:deci=deci+8*z; break;
case 9:deci=deci+9*z; break;
}
d++;
}
else
{
printf("\nInvalid Hexadecimal number\n");
flg1=1;
break;
}
}
if(flg1==0)
{
printf("\nDecimal equivalent of Hexadecimal %s is %d ",a,deci);
}
printf("\n\nEnter the Octal number: ");
scanf("%s",&a);
i=0;
len=0;
while(a[i]!='\0')
{
len++;
i++;
}
len--;
deci=0;
d=0;
z=0;
for (i=len; i>=0; i--)
{
z=pow(8,d);
j=a[i]-48;
if (j<=7)
{
switch (j)
{
case 1:deci=deci+1*z; break;
case 2:deci=deci+2*z; break;
case 3:deci=deci+3*z; break;
case 4:deci=deci+4*z; break;
case 5:deci=deci+5*z; break;
case 6:deci=deci+6*z; break;
case 7:deci=deci+7*z; break;
}
}
else
{
flg2=1;
printf("Invalid Octal Number\n");
break;
}
d++;
}
if(flg2==0)
{
printf("\nDecimal equivalent of Octal %s is %d ",a,deci);
printf("\n");
}
printf("\nPress 'Y' to continue any other key to terminate: ");
scanf ("%c",&c);
c=getchar( );
printf("\n\n");
}while (c == 'Y');

/* Program to find the length of the given string*/

#include <stdio.h>
#include <conio.h>
void main()
{
char strn[100];
int i, len;
printf("Enter the string\n");
scanf("%[^\n]",&strn);
i=0;
len=0;
/*It even counts the number of spaces included in the string*/
while(strn[i] != '\0')
{
len++;
i++;
}
printf("Length of the entered string is : %d ",len);
}

Output:
/* Program to convert Upper case letters to lower case letters and vice versa*/

#include <stdio.h>
#include <conio.h>
#include <ctype.h>
void main ()
{
char strn [100];
int i;
i = 0;
printf ("Enter any string\n");
scanf("%[^\n]",strn);
while (strn[i]!= '\0')
{
isupper(strn[i])?(strn[i]=tolower(strn[i])):(strn[i]=toupper(strn[i]));
i++;
}
printf ("Coverted string is: %s\n",strn );
getch();
}

Output:
/* C program to compare two strings*/

#include <stdio.h>
#include <conio.h>
void main( )
{
int i,j,flg=0;
char string1[100], string2[25];
clrscr( );
printf("Enter the first string\n");
scanf("%[^\n]", &string1);
flushall( );
printf("Enter the second string\n");
scanf("%[^\n]", &string2);
flushall( );
i=0;
j=0;
while (string1[i]!='\0'||string2[j]!='\0')
{
if(string1[i]!= string2[j])
{
flg=1;
break;
}
i++;
j++;
}
if(flg==0)
{
printf("The strings are equal \n");
}
else
{
printf("The strings are not equal \n");
}
getch();
}

Output:

/* Program to concatenate two strings*/

#include <stdio.h>
#include <conio.h>
void main( )
{
int i,j;
char string1[100], string2[25];
clrscr( );
printf("Enter the first string\n");
scanf("%[^\n]", &string1);
flushall( );
printf("Enter the second string\n");
scanf("%[^\n]", &string2);
flushall( );
i=0;
j=0;
i=0;
while (string1[i] !='\0')
{
i++;
}
j=0;
string1[i]=' ';
i++;
while (string2[j] !='\0')
{
string1[i]= string2[j];
j++;
i++;
}
string1[i] ='\0';
printf("The resultant string after concatenation is: %s \n", string1);
getch( );
}

Output:

/* Program to find the number of vowels in a given string*/

#include <stdio.h>
#include <conio.h>
#include <ctype.h>
void main()
{
char strn[100],ch;
int i, vowel;
i=0;
vowel=0;
clrscr();
printf("Enter the string\n");
scanf("%[^\n]",&strn);
while(strn[i] != '\0')
{
ch=tolower(strn[i]);
switch(ch)
{
case 'a':
vowel++;
break;
case 'e':
vowel++;
break;
case 'i':
vowel++;
break;
case 'o':
vowel++;
break;
case 'u':
vowel++;
break;
}
i++;
}
printf("Number of vowels present in the entered string is : %d",vowel);
getch();
}

Output:

/* Program to convert first character of a word to capital letter*/

#include <stdio.h>
#include <conio.h>
#include <ctype.h>
void main ()
{
char strn[100];
int i;

i = 0;
printf ("Enter any string\n");
scanf("%[^\n]",&strn);
strn[i]=toupper(strn[i]);
while (strn[i]!= '\0')
{
if(strn[i]==' ' && strn[i+1]!=' ')
{
strn[i+1]=toupper(strn[i+1]);
}
i++;
}
printf ("Coverted string is: %s\n",strn );
getch();
}

Output:

/*Program to reverse the given string and check whether it is a palindrome*/

#include <stdio.h>
#include <conio.h>
void main()
{
char strn[100],temp[100];
int i, j,flg;
flg=0;
printf("Enter the string\n");
scanf("%[^\n]",&strn);
i=0;
while(strn[i] != '\0')
{
i++;
}
i--;
j=0;
while( i >=0)
{
temp[j]=strn[i];
j++;
i--;
}
temp[j]='\0';
i=0;
printf("\nReversed string is --> %s \n",temp);
while(strn[i] != '\0')
{
if (strn[i] != temp[i])
{
flg=1;
break;
}
i++;
}

If(flg==0)
printf("\nEntered string %s ---> is a palindrome\n",strn);
else
printf("\nEntered string %s ---> is not a palindrome\n",strn);
}

Output:

/* Program to add and multiply two complex numbers*/


#include <stdio.h>
#include <conio.h>

struct complex
{
int real;
int img;
};
void main()
{
struct complex c1,c2,c3;
struct complex complex_add();
struct complex complex_mult();
printf("Enter the real and imaginary parts of first complex number: ");
scanf("%d%d",&c1.real,&c1.img);
printf("Enter the real and imaginary parts of second complex number: ");
scanf("%d%d",&c2.real,&c2.img);
c3=complex_add(c1,c2);
printf("\nResult of addition of two complex numbers\n");
printf("\nReal and Imaginary parts of the complex number is:");
printf("%d+i%d\n",c3.real,c3.img);
c3=complex_mult(c1,c2);
printf("\nResult of multiplication of two complex numbers\n");
printf("\nReal and Imaginary parts of the complex number is:");
printf("%d+i%d\n",c3.real,c3.img);
getch();
}
struct complex complex_add(cmpl1, cmpl2)
struct complex cmpl1,cmpl2;
{
struct complex cmpl3;
cmpl3.real=cmpl1.real + cmpl2.real;
cmpl3.img=cmpl1.img + cmpl2.img;
return(cmpl3);
}
struct complex complex_mult(cmpl1, cmpl2)
struct complex cmpl1,cmpl2;
{
struct complex cmpl3;
cmpl3.real=cmpl1.real*cmpl2.real - cmpl1.img*cmpl2.img;
cmpl3.img=cmpl1.real*cmpl2.img + cmpl1.img*cmpl2.real;
return(cmpl3);
}

Output:
/* Program to add and multiply two complex numbers using structure pointers */

#include <stdio.h>
#include <conio.h>

struct complex
{
int real;
int img;
};
void main()
{
struct complex c1,c2,c3;
void complex_add();
void complex_mult();
printf("Enter the real and imaginary parts of first complex number: ");
scanf("%d%d",&c1.real,&c1.img);
printf("Enter the real and imaginary parts of second complex number: ");
scanf("%d%d",&c2.real,&c2.img);
complex_add(&c1,&c2,&c3);
printf("\nResult of addition of two complex numbers\n");
printf("\nReal and Imaginary parts of the complex number is:");
printf("%d+i%d\n",c3.real,c3.img);
complex_mult(&c1,&c2,&c3);
printf("\nResult of multiplication of two complex numbers\n");
printf("\nReal and Imaginary parts of the complex number is:");
printf("%d+i%d\n",c3.real,c3.img);
getch();
}
void complex_add(cmpl1, cmpl2, cmpl3)
struct complex *cmpl1,*cmpl2, *cmpl3;
{
cmpl3->real=cmpl1->real + cmpl2->real;
cmpl3->img=cmpl1->img + cmpl2->img;
}
void complex_mult(cmpl1, cmpl2,cmpl3)
struct complex *cmpl1,*cmpl2, *cmpl3;
{
cmpl3->real=cmpl1->real*cmpl2->real - cmpl1->img*cmpl2->img;
cmpl3->img=cmpl1->real*cmpl2->img + cmpl1->img*cmpl2->real;
}

Output:

Standard file pointers:

stdin Standard Input device keyboard


stdout Standard Output device Monitor
stdaux Standard Auxiliary device Serial Port
stdprn Standard Printing device Parallel Port
stderr Standard Error device Monitor

/*Read the contents of the file & send it to the printer (parallel port)*/
# include <stdio.h>
# include <conio.h>
void main ( )
{
FILE *ptvar;
char c;
ptvar = fopen ("test.dat", "r");
do
{
c=fgetc(ptvar);
fputc(c,stdprn);
}while (c!= EOF);
fclose(ptvar);
getch();
}
Standard File pointers are defined in stdio.h.

/* Program to copy the contents from one file to another using command line
arguments*/
#include <stdio.h>
#include <conio.h>
void main(int argc, char *argv[])
{
FILE *ptr1,*ptr2;
char ch;
clrscr();
ptr1=fopen(argv[1],"w");
printf("Enter the contents for the file: %s\n",argv[1]);
do
{
ch=getchar();
putc(ch,ptr1);
}while(ch !=EOF);
fclose(ptr1); /* argv[1] is opened in read mode and argv[2] is opened in the
write mode*/
ptr1=fopen(argv[1],"r");
ptr2=fopen(argv[2],"w");
do
{
ch=getc(ptr1);
putc(ch,ptr2);
}while(ch !=EOF);
fclose(ptr1);
fclose(ptr2);
/*Open argv[2] in read mode to read the copied contents*/
printf("\nCopied contents of file: %s\n",argv[2]);
ptr1=fopen(argv[1],"r");
do
{
ch=getc(ptr1);
putchar(ch);
}while(ch !=EOF);
fclose(ptr1);
getch();
}

Output:

Note:
See the chapter Command Line Parameters to learn the way of executing the
program with command line parameters.

/*C Program to count the number of bit set and unset in an integer*/
#include
#include
void main()
{
int n;
void dispbits();
clrscr( );
printf("Enter an integer \n");
scanf("%d",&n);
dispbits(n);

getch( );
}

void dispbits(m)
int m;
{
int mask, k, i, s, u;
u=0;
s=0;
printf("Bit pattern of %d is : ",m);
for (i=15;i>=0;i--)
{
mask=1;

mask=mask<<i;
k=mask&m;
k==0?u++:s++;
k==0?printf("0"):printf("1");
}
printf("\n\nNumber of bits set in %d is %d and unset is %d \n",m , s, u);
}
<

/* Program to add and subtract matrices using functions and pointers*/


#include <alloc.h>
#include <stdio.h>
#include <conio.h>
void main( )
{
int i, j, m, n, p,q, choice, flg=0, ch;
int *ptr1,*ptr2, *ptr3,*temp1,*temp2;
void sum(int *, int *,int *,int,int);
void sub(int *, int *,int *,int,int);
clrscr();
printf ("Enter the order of the first matrix\n");
scanf ("%d%d",&m,&n);
flushall( );
ptr1=(int *)malloc(m*n*sizeof(int));
temp1=ptr1;
printf ("Enter the order of the second matrix\n");
scanf ("%d%d",&p,&q);
flushall( );
ptr2=(int *)malloc(p*q*sizeof(int));
temp2=ptr2;
printf ("Enter the elements of the first matrix\n");
for (i=0; i<m;i++)
{
for (j=0; j<n;j++)
{
scanf("%d",ptr1);
ptr1++;
}
}
printf ("Enter the elements of the second matrix\n");
for (i=0; i<p;i++)
{
for (j=0; j<q;j++)
{
scanf("%d",ptr2);
ptr2++;
}
}
do
{
ptr1=temp1;
ptr2=temp2;
printf("1)Addition \t");
printf("2)Subtraction \t");
printf("Enter Your Choice-->");
scanf("%d", &choice);
switch(choice)
{
case 1: printf("Matrix Addition and ");
if((m==p)&&(n==q))
{
ptr3=(int *)malloc(m*n*sizeof(int));
sum(ptr1,ptr2,ptr3,m,n);
flg = 1;
}
else
printf("Order of matrix do not match so addition is not possible \n");
break;
case 2: printf("Matrix Subtraction and ");
if((m == p) && (n == q))
{
ptr3=(int *)malloc(m*n*sizeof(int));
sub(ptr1,ptr2,ptr3,m,n);
flg = 1;
}
else
printf("Order of matrix do not match so matrix subtraction is not possible \n");
break;
default: printf("Wrong Choice");
}
if (flg == 1)
{
printf("The resultant matrix is \n");
for (i=0; i<m;i++)
{

for (j=0; j<q;j++)


{
printf("%d\t",*ptr3);
ptr3++;
}
printf("\n");
}
}
printf("Press 'Y' to continue and any other key to terminate --->");
flushall();
ch=getchar();
}
while (ch == 'Y');
getch( );
}
/* Function for matrix addition */
void sum(pt1,pt2,pt3,row,col)
int *pt1,*pt2,*pt3,row,col;
{
int i,j;
for (i=0; i<row;i++)
{
for (j=0; j<col;j++)
{
*pt3 = *pt1 + *pt2;
pt1++;
pt2++;
pt3++;
}
}
}

/* Function for matrix subtraction */


void sub(pt1,pt2,pt3,row,col)
int *pt1,*pt2,*pt3,row,col;
{
int i, j;
for (i=0; i<row;i++)
{
for (j=0; j<col;j++)
{
*pt3 = *pt1 - *pt2;
pt1++;
pt2++;
pt3++;
}
}
}

Output:
Short hand assignment operator:

Instead of writing i=i+1 you can use a short hand assignment operator and write the
same statement as i +=1; Similarly i=i-5 can be written as i-=5; This can be used for
all the arithmetic operators.

The general syntax will be v op= exp; and it will be expanded as v= v op exp;

So if we have a statement i += j+2; it is equivalent to i = i+(j+2)

Increment and Decrement operators:


The most usefull operators of C are increment ( ++) and decrement ( -- ). The
operator ++ adds 1 to the operand and -- subtracts 1 from the operand.

Prefix and Postfix increment and decrement operators:

Prefix operator first adds 1 to the operand and then assigns the result to the
variable on left.
Example:
i=1;

m=++i;

In the above example the value of both i and m will be 2.

Postfix operator first assigns the value of the operand to the variable on the left
and then increments the operand.

Example:
i=1;
m=i++;

In the above example the value of m will be 1 and i will be 2.

Character Test Functions:


The following functions tests the value present in the variable passed as argument :
1)isalnum(c)- is c an alphanumeric character.
2)isalpha(c)- is c an alphabet
3)isdigit(c)- is c a digit
4)islower(c)- is c a lower case letter
5)isupper(c)- is c an upper case letter
6)ispunct(c)- is c a punctuation mark
7)isspace(c)- is c a white space character
8)isprint(c)- is c a printable character

All the above character test functions are defined in the header file ctype.h

exit() :
This function terminates the program.
Syntax:
void exit(int);

This function is defined in the header file stdlib.h

Example:
#include <stdio.h>
#include<conio.h>
#include <stdlib.h>
void main()
{
int i;
for(i=0; i< 15; i++)
{
if(i==7)
exit(0);
printf("i is %d\n",i);
}
getch();
}

Output:
When the value of i equals to 7 the program is terminated.

Table containing different data types, the data range, number of bytes and the format
specification character is given below:
Dta type Range Bytes Format
signed char or char -128 to +127 1 %c
unsigned char 0 to255 1 %c
signed int or int -32768 to +32767 2 %d
unsigned int 0 to 65535 2 %u
signed long int or long int -2147483648 to +2147483647 4 %ld
unsigned long int 0 to 4294967295 4 %lu
Float 3.4E-38 to 3.4E+38 4 %f
Double 1.7E-308 to 1.7E+308 8 %lf
long double 3.4E-4932 to +1.1E+4932 10 %Lf

Lenguaje C++

Keywords and Identifiers

The set of "C++" words that can be formed from C++ Character set can be classified as
keywords and identifiers. The keywords are a part of "C++", which have a predefined
meaning and cannot be changed. All keywords must be written in lower-case.

 "C++" Keywords

auto do for return

break double goto start

case else if sizeof

char enum int static

continue extern long struct


default float register switch

 Identifiers are user defined names that refer to variables and function names.

Our aim is to provide information to the knowledge seekers.

Variables and constant

 Variables enable the programmers to assign and manipulate data using symbolic
names.
 Strings and numeric values can be stored in the memory of the computer for
subsequent recall.
 Whenever the memory is used for this purpose the programmer must assign a
unique name to each area in memory.
 In brief, a variable is a data name that may be used to store a data value, for easy
retrieval.
For example, the legal identifier names in ‘C++’ are,
 totsalary
 Counter
 Stdname

The maximum length of the variable is machine dependent. Usually 32 is the maximum
number of characters permissible in a variable name.

Constants

 Constant is one that has a fixed value throughout the program.


 The constant can be viewed as a read only memory variable.
 In a program, the constant identifier should not be changed by any manifestation of
the program statements.
 Constants are broadly classified into numeric constant and character constant.
 An integer constant is a sequence of digits that may or may not be prefixed with a
minus sign.

Some examples are,

 14
 -32
 0
 576321
 Note that C++ does not support unary plus hence, +14 is not a valid integer.

A real constant can be represented in 2 forms,


1.Decimal form 2.Exponent form
Eg.12.75 Eg.1275E-2
-14E-2
A character constant is a single character enclosed within a pair
of single quotes.
Eg.: ‘7’ , ‘M’

 Note that the character constant ‘7’ is not the same as the integer 7.
 A string constant is a group of characters enclosed double quotes. These characters
may be letters, numbers, or any special characters.
 Eg.: “welcome” , “2000”

Data Types

 Data type is defining an attribute to the variable.


 It defines the set of legal data the variable can store. The basic data types are int, char,
and float.
 Integer, the data type ‘int’ are whole numbers with a range of values supported by a
particular machine. For instance, in a 16 bit word length machine, the integer values lie
between -32768 to 32767.
 ‘C++’ facilitates some control over the integer data type by providing sub data types
namely short int, int, long int.
 Short int represents fairly small integer values and requires half the amount of storage as
a normal int uses.
 Similarly a long int represents fairly higher integer values and requires generally twice
the number of bits as a normal int uses. Nevertheless these are highly machine dependent.
 Another option in integer data type is declaring it as unsigned. This unsigned integer uses
all bits for the magnitude of the number and is always positive. For instance, in a 16 bit
machine the range of unsigned integer is 0 to 65,535. Thus long and unsigned are intended
for increasing the range of values.
 Floating point type: Floating point numbers are numbers that have a decimal point. This
data type in ‘C++’ is an attribute for real numbers.
 The corresponding declaration is, float a;
 which instructs the compiler that the variable ‘a’ belongs to the data type real. If you
want to initialize the variable, then
 float a;
 a = 14.752;
 This can also be achieved through a single statement. float a = 14.752;
 The keyword float defines the floating point number.
 When more accuracy is required, another sub data type, double under the float data type
can be used.
 Obviously this double data type uses twice the storage as that of the float data type (8
bytes).
 To display a double value the format specifier %f is used to obtain the standard floating
point notation and %e for scientific or exponential notation
 Character data type: The char keyword defines a character data type. Thus the
declaration for this is char x;
 x = ‘a’;
 The variable x is of type character and is initialized to the character ‘a’. The same effect
could be achieved as,
 char x = ‘a’;

Declarations

o The modern programming languages clearly separate the data and the
control statements.
o All the data is declared first. This makes the program more readable and also
provides the information to the compilers to allocate memory spaces.
o The data and control are well separated by making the former to precede
textually, in the name of declarations. Such declarations follow the syntax,
o type identifier v1, v2......vn; Example: int n1, n2, n3; Example: float x, y, z;
o Initialization of the data is also possible at the time of declaration. Example:
int x =72;
o Data declaration does two significant tasks for each variable. They are:
7. Tells the compiler what the variable name is.
8. Specifies what type of data the variable will hold

Input and output operations

 The most common way in which a program communicates with the outside world is
through simple, character-oriented Input/Output (IO) operations. C++ provides two
useful operators for this purpose: >> for input and << for output.
 cin is used for input in C++. It is defined as an object in the header file iostream.h.
The >> is the ‘extraction’ or ‘get from’ operator.
 cout is used for output in C++. It is defined as an object in the header file
iostream.h. The << is the ‘insertion’ or ‘put to’ operator.
 cout: The cout stream is used in conjunction with the overloaded operator <<(a pair
of “less than” signs).
 cout << “Output sentence”; // prints Output sentence on screen
 cout << 120; // prints number 120 on screen
 cout << x; // prints the content of variable x on screen
 The << operator is known as insertion operator since it inserts the data that follows
it into the stream that precedes it.
 In the examples above it inserted the constant string Output sentence, the numerical
constant 120 and the variable x into the output stream cout.
 Notice that the first of the two sentences is enclosed between double quotes (“it is a
string of characters. Whenever we want to use constant strings of characters we
must enclose them between double quotes (“) so that they can be clearly
distinguished from variables.

Cin

 Handling the standard input in C++ is done by applying the overloaded operator of
extraction (>>) on the cin stream. This must be followed by the variable that will
store the data that is going to be read. For example:
 cin >> age;
 Above statement waits for an input from keyboard in order to store it in the variable.
 cin can only process the input from the keyboard once the RETURN key has been
pressed.
 Therefore, even if you request a single character cin will not process the input until
the user presses RETURN once the character has been introduced. Click here For
i/o Example
 You can also use cin to request more than one data input from the user:
 cin >> a>> b; is equivalent to: cin >> a; cin>>b;
 In both cases the user must give two data, one for variable a and another for variable
b that may be separated by any valid blank separator: a space, a tab character or a
new line.

Enumerated Data Types

 This is used when you know in advance the finite number of values a variable can
take in a program. E.g. The days of the week can only take values from Sunday to
Saturday.
 An ‘enum’ specifier defines the set of all names that will be permissible values of
the type. These values are called ‘members’ of the data variable. ‘Enumerated’
means that all values are listed.
 These data types are treated internally as integers. The first member gets value 0,
the second 1, and so on.
 Changes can be done by assigning it in the declaration. The values can be set by
initializing the first value.
 In following example, Sunday is initialized the value 1. Thus Monday gets the value
2 and so on. If the difference between the first and second value is more than 1, the
values must be specified explicitly while declaring the data type.
 E.g. Sun = 10, Mon = 20, and so on. Click here For Enumarated data type Examples

Example-1
# include < iostream.h >
enum days_of_week {Sun = 1, Mon, Tue, Wed, Thu, Fri, Sat};
void main()
{
days_of_week day1, day2;
day1 =Mon; day2=Thu;
int diff day2-day1;
cout << "Days between = "<< diff << endl;
if (dayl < day2)
cout << "day1 comes before day2 \n";
}

#include < stdio.h >


#include < conio.h >
main(){
int w;
enum weekday{mon,tue,wed,thu,fri,sat,sun};
enum weekday week_st, week_end;
clrscr();
printf("enter 0 for monday\n");
scanf("%d",&w);
week_st=w; /* if w is equal to 0 then only the condition is true*/
week_end=sun;
if(week_st==mon && week_end==sun)
printf("beginning and end of the week\n");
else
printf("not valid\n");
getch();
}

C++ operator

 ‘C++’ supports a rich set of operators. An operator can be defined as just a symbol
that tells the compiler to perform certain mathematical or logical manipulations.
Taxonomy of operators
 Assignment operators
 Arithmetic operators
 Relational operators
 Logical operators
 Increment/decrement operators
 Conditional operators
 Bitwise operators.

Expresssions
The following are the rules regarding expressions:

 A signed or unsigned variable name or constant is an expression.


 An expression connected by an arithmetic operator to an unsigned variable name or
an unsigned constant is an expression.
 An expression enclosed in parenthesis is an expression.
 Two expressions connected by an arithmetic operator is an expression.
 Two arithmetic operators should not occur in succession in an expression.

Assignment operators

 Values need to be assigned to variables.


 The ‘=’ operator is used for assignment.
 int a =5;
 The syntax of an assignment is,
 Variable-name = expression;
 For example, sum=a+b;
 Assigns the total of contents of a and b to a variable named, sum. The variable sum
appears to the left of the equal sign, which is called the assignment operator. On the
left of this operator only a single variable can occur, expressions and constants are
strictly not allowed. The declaration of a variable and its initial assignment is
possible, as a single line. This takes the form,
 [qualifier] type identifier=constant Or
 [qualifier] type identifier = predefined variable
 example: auto int no = 100;
 C++ also permits the initialization of more than one variable in one statement using
multiple assignment operation.
 Similarly, compound assignment operators can be used.
 Eg:+=,-=,/=,%=

 a=a+5 or a+=5

 b=b-10 or b-=10

 c=c/2 or c/=2

 prod=prod * no or prod*=no
 year=year%4 or year%=4

Arithmetic operators

The arithmetic operators are given below

Operator Meaning
+ Addition
- Substraction
* Multiplicationtion
/ Division
% Modulus

Modulus operators

 The modulus operator is represented by the % (percent symbol).


 This returns the remainder of the division, hence 11 mod 2 is represented in ‘C++’
as, and 11 % 2 and the result is 1, the remainder of the division.
 10 % 5 is 0 as there is no remainder after this division. Click here for arithmetic
operator Example

Symbolic Constant

 When a numeric value or a constant is to be used in the program, then, it can be


given a symbolic definition or preprocessor directives which means, before
compilation every occurrence of the symbolic-name in the program is substituted by
the value-of-constant as given in the syntax.
 SYNTAX: #define symbolic-name value-of-constant
 This name is usually written in upper case to distinguish it from an ordinary
Variable Example: # define MAX_MARKS 100

Mixed Mode

 Having known the basic two types int and float, the next logical question is whether
mixing of these two types is allowed.
 That is, an arithmetic expression containing an operand of type int and another of
type float, this is allowed.
 The integer value is automatically converted to type float for the calculation.
 Such automatic conversions are known as implicit conversion.

Type Conversions

 When two operands of different types are encountered in the same expression, the
lower type variable is converted to the type of the higher type variable. These
conversions take place invisible to the user.

Relational Operators

Logical Operators
Increment/Decrement Operator

 Apart from + and -, ‘C++’ provides two very useful operators. They are ++ and --
defined as increment and decrement respectively.
 The ++ adds 1 to the operand and -- operators decrements 1 from the operand. Both
are unary operators.
 These operators can be used in two ways:
 pre-operator (before the variable)
 post-operator (after the variable)
 Syntax: ++variable; --variable (pre-operator)
 variable++; variable-- (post-operator)
 A prefix operator first adds 1 to the operand and then the result is assigned to the
variable on left.
 In contrast, a postfix operator first assigns the value to the variable on the left and
then increments the operand

 ++variable is equivalent to
 variable=variable+1; or variable+=1;

 --variable is equivalent to
 variable=variable-1; or variable-=1;

 example:

 int i=5;
 a=i++; a=++i;
 a will be 5 a will be 6

 i will be 6 i will be 6
Conditional Operators(?:)

 This is a compressed version of the if - else statement.


 The syntax of this statement is: Expression 1 ? Expression 2 : Expression 3;
 The operator pair works as given below:
 The entire expression is evaluated from left to right. Expression 1 is evaluated first,
if it is non zero (true) then Expression 2 is evaluated and becomes the value of the
expression. If Expression 1 is false (not true) Expression 3 is evaluated and its value
becomes the value of the expression. So only one of the Expressions either
Expression 2 or Expression 3 is evaluated and not the both during a given
evaluation.
 Example:
 #include
 main()
 {
 int a, b, x;
 a=5;
 b=10;
 x=(a > b) ? a:b;
 printf(“ Value of x is %d\n”,x);
 }
 In the statement c = (a > b) ? a: b;
 a and b are operands.
 If the condition is true then value of a is assigned to c otherwise b is assigned to c

Precedence of Operators

 In an expression, operators with higher precedence are evaluated before those of


lower precedence. Operators on the same row have equal precedence and are
evaluated left to right.

 Example: 2 + 3 * 2 / 2-1
 The expression is evaluated from left to right according to the
rules of precedence.
 2 + 6 / 2 -1
 2 + 3 - 1
 5 - 1
 4
 Example: 3 * 2 + 3 - 1 * 2/3 + 3 % 2
 The expression is evaluated from left to right according to the
rules of precedence.

 6 + 3 -1 * 2/ 3 + 3 % 2
 6 + 3 - 1 * 0 + 3 % 2
 6 + 3 - 0 + 3 % 2
 6 + 3 - 0 + 1
 9 - 0 + 1
 9 + 1
 10
 Example: (2 + 3) * 2/ 2 -1
 The expression is evaluated from left to right
 according to the rules of precedence.
 5 * 2 / 2 -1
 10 2 - 1
 5 - 1
 4

Decision Statements
 C++ programs are a collection of functions each having a declaration part and a
computational part which are executed sequentially, that is statement by statement.
 In practice, it becomes necessary to make some changes in the order of execution
and you need the in-built decision making mechanism. This is accomplished by a
set of control constructs. In accordance with the decision, control will be transferred
to another part of the computational structure, inside the function.
 This decision may be, 1. One way branching. 2. Two way branching.

One way branching.

 One way branching means evaluating a condition and then branching. In accordance
with the test advocated, set of statements are executed. This type of decision making
technique takes the form:
 If (test expression)
 {
 statement-block
 }
 statement-n;
 This syntax is for a simple if statement. That is, the decision is made based on a
condition,
 Braces { and } are used to group declarations and statement together into a
compound statement. This set of compound statement together enclosed within
parenthesis is called a block.

One way Branching


 he statement block can be a single statement or group of statements. If the logical
expression is true then statement block is executed otherwise the statement block
will be skipped and the statement-n will be executed. When the condition is TRUE
that is when logical expression evaluates to true both the statement and the
statement-n are executed.
 The test expression is made up of relational and logical operator.
 example
 # include < iostream.h >
 void main()
 {
 int n1 ,n2;
 cout << "Enter 2 random numbers \t";
 cin >> n1
 cin >> n2;
 if (n1 == n2)
 cout << "Numbers are equal" << endl;
 }

Two way branching

 Two way branching is used in situations, wherein you need to trace two mutually
exclusive sets of actions. This is accomplished by if ..then ..else construct of C++,
which takes the form:
 If (test expression)
 {
 statement(s) for the condition being true
 }
 else
 {
 statement(s) for the condition being false
 }
 statement-n;

Two way branching


 If the test expression is true then the block of statement, immediately following the
if statement is executed; otherwise the second block of statement is executed.
 Note that, in either case, either the true-block of statements or the false-block of the
statement alone is executed and not both.
 Example
 void main()
 {
 int a, b;
 cout << Enter Two Number;
 cin >> a >> b;
 if(a==b)
 {
 cout << "Both number is equal";
 }
 else
 {
 cout << "Both number is not equal";
 }
 }

Nested if-then-else
 The else clause, like the if clause, may contain a compound statement. This is a
more flexible control construct. Further, a clause of if statement may itself contain
another if statement. This is known as nested if statement.
 The following example illustrates the nested if construct.
 If (x == 3)
 {
 if (x> 5)
 cout << "The numbers lies between 3 and 5" << endl;
 }
 else
 cout << "the number does not lie between 3 and 5" << endl;

Multi-way branching
 The multiway branching can be achieved by a nested if-then-else clause.
 Thereby any number of mutually exclusive statement blocks can be accommodated.
 Example:
 if (marks> 80)
 grade = 'A';
 else if (marks > 60)
 grade = 'B';
 else if (marks> 50)
 grade = 'C';
 else
 grade = 'F';

Switch-case

 The construct switch(), which is an extension of the if. else... Statement is used for
efficient multiway branching. The switch statement tests the value of the expression
or variable, which is of type int or int compatible, against a list of case values and
when a match is found, a block of statements associated with that case is executed.
 The syntax is as below:
 switch (expression)
 {
 case value-1:
 block-1;
 break;
 case value-2:
 block-2;
 break;
 case value-3:
 block-3;
 break;
 default:
 default-block;
 break;
 }
 statement-n;
 When the switch is executed, the value of the expression is successfully compared
against the values, value-1, value-2, etc.
 If a case is found where values matches with the value of the expression, then the
block of statements that follows the case is executed.
 If there exists no match then the default block is executed. The default is optional.
 The break statement at the end of each block indicates the end of a particular block
and the execution resumes at statement-n.
 If a break is not included in the end of each block, then all the blocks following the
block executed will also be executed.
Click here for example

//Sample Program to check whether a given character is Vowel or not


#include < iostream.h >
#include < conio.h >
void main()
{
char x;
clrscr();
cout << "\n Enter a character: ";
cin>>x;
switch(x)
{
case 'A':
case 'a': cout << "\n " << x <<" is a vowel " << endl;
break;
case 'E':
case 'e': cout << "\n " << x <<" is a vowel " << endl;
break;
case 'I':
case 'i': cout << "\n " << x <<" is a vowel " << endl;
break;
case 'O':
case 'o': cout << "\n " << x <<" is a vowel " << endl;
break;
case 'U':
case 'u': cout << "\n " << x <<" is a vowel "<< endl;
break;
default: cout << "\n " << x <<" is not a vowel " << endl;
}
getch();
}

Goto Statement

 Unconditional Branching: SYNTAX: goto <label>


 A label is any valid variable name and must be followed by a colon. goto read;
 The label can be anywhere in the program either before or after the goto statement.
Note that such control transfer occurs unconditionally. It is obvious that the goto
breaks the normal sequential execution of the program. This is basically a control
jump, which could be either forward or backward. Consider the following code,
 # include < iostream.h >
 void main(){
 char c;
 take:
 cout << “ Input the character (z to exit) :”;
 cin >> c;
 if (c==‘z’)
 cout << “ You have typed the right character to exit”;
 else
 {
 cout << “character is” << c << endl;
 goto take;
 }
 }

Looping Structure
 The looping computational structures can be achieved through many control
structures in ‘C++’ Looping is based on a condition.
 A loop terminates only by asserting the status, say true or false of the condition.
Loops can be classified as entry controlled and exit controlled loops based on the
positioning of that condition.
 There are three types of looping structure in C++.
 1. For loop
 2. While loop
 3. Do while lop
 For & while loop are entry control loops, where as do while are exit control loop.

For Loop
SYNTAX:
for(initialization; test condition; modifier expression)
{
body of the loop;
}

 Initialization: The control variable is initialized first with a simple assignment


statement c=0;
 Test condition: The value of the control variable is tested using this test condition.
For each iteration this is performed. This is a relational expression, such as i < 10
which could determine the exit from the loop. If this relational expression is true
then the loop is executed again, otherwise control exits.
 Modifier expression: At the end of the loop the control variable is transferred back
to the for statement. The control variable is modified, and the update value of the
control variable is again tested. If the condition is satisfied, the loop continues, if
not control exits.
 You could also write a program to display all the numbers from 100 to 1, using a
decrement (negative increment), as follows:
 Example:
 //Program to print the numbers from 100 to 1
 #include < iostream.h >
 void main()
 {
 cout << endl << "The first 100 natural numbers are " << endl;
 for (int i=100;i > 0;i--)
 cout << i << ‘\t’;
 cout << endl;
 }
 One of the interesting points of this loop is that it is concise. All the three operators
- the initialization, the iterative test and the increment are stated in the for statement.

Comma operator
 When the execution of the loop depends on two variables or two conditions the for
statement can be set up with two variables separated by a comma.
 for (i=0,j=n;i<=n;i++,j--)
 Note that the comma is included along with the usual semicolons. This is called the
comma operator.
 Example:
 //Program to print ascending and descending numbers
 #include < iostream.h >
 void main()
 {
 int n;
 cout << endl << “Enter a number ”<> n;
 cout << endl;
 for (int i=0,j=n;i<=n;i++,j--)
 cout << k << ‘\t’<< j << endl;
 cout << endl;
 }

Nesting of the for loop


 Nesting of the for loop is permissible in C++, i.e. a body of a for loop can also have
a for structure. To pictorially
 visualize this circumstance, look at the following figure,
 for (x=10;x >0;x--) //outer for loop begins
 {
 for (y=1 ;y< =5;y++) //inner for loop begins
 {
 .......
 } //inner for loop begins
 .......
 } //outer loop ends
 In nested loops the inner loops are executed first and then the outer loops. The
nesting is permissible to any depth. For better degree of readability the loops are to
be indented properly.

While loop
 Looping on a condition can be achieved by the while construct of C++. This is the
simplest of all control structures.
 SYNTAX:
 while (test condition)
 {
 body of the loop;
 }
 This is an entry restricted loop
 That is, only on the condition being true the body of the loop is executed. In other
words, if the condition returns 0, the body of the loop is not executed even once.
 This process of repeated execution, called iteration, continues until the test
condition returns false.
 On exiting from the loop, the control is transferred to the first statement outside the
loop. Example Click here for fibonacci program
//Sample Program to generate the Fibanacci terms
#include < stdio.h >
#include < conio.h >
void main()
{
int a,b,c,terms,count;
clrscr();
printf("\n Enter the number of terms: ");
scanf("%d",&terms);
a=0;
b=1;
printf("\n Fibanacci terms are \n");
printf("\n %d \n",a);
printf("\n %d \n",b);
count=2;
while(count < terms)
{
c=a + b;
printf("\n %d \n",c);
a=b;
b=c;
count++;
}
getch();
}

do while Loop
 An alternate to the while construct is the do control structure. This is also a decision
dependent looping. But the difference here is that the condition is evaluated at the
end, i.e. after executing the body of the loop once.
 While basically was entry controlled. Therefore, the body of the loop may not be
executed even once. There are occasions in programming, wherein the execution
may have to be done at least once. This effect is achieved through the do statement.
 SYNTAX:
 do
 {
 body of the loop
 } while (condition);
 On reaching the do statement, the control proceeds to execute the body of the loop.
When the while statement is reached, the condition is evaluated. If the condition is
true, control is transferred to the beginning of the do loop and the program
continues to execute the body of the loop. Otherwise control is transferred out of the
loop. This loop is an exit controlled loop because, the test condition is evaluated at
the bottom of the do while loop.
continue Statement
 It may be necessary to skip a part of the body of the loop. That is, when loop is in
execution, on certain condition the rest of the execution of the loop concerned is to
be ignored and the loop should continue for the next iteration. The control statement
called continue is used to achieve this. This statement causes the loop to be
continued, with the next iteration after updating the control variable, without
executing the rest of the statements in the loop. It applies to the loop immediately
enclosing the continue statement. It is an error to use the continue statement outside
a loop.
 When the continue statement appears inside nested loops, it applies to the loop
immediately enclosing it, and not to the outer loops.
 Example:
 for (i=1; i<=5; i++)
 {
 if (i==3)
 continue;
 cout << i;
 }
 For the above example, only =1, 2, 4, 5 will be printed. When i
is equal to 3, control is returned to the beginning of the loop.

Example

//Sample Program to demonstrate the use of continue keyword


#include < iostream.h >
#include < conio.h >
void main()
{

int i;
clrscr();
cout << "\n Example for 'continue'keyword -> ";
for (i=1; i <=5; i++)
{
if (i==3)
continue;
cout << i << " ";
}
getch();
}

Break Statement
 A break statement may appear inside a loop (while, do, or for) or a switch
statement. It causes a jump out of these constructs, and hence terminates them. Like
the continue statement, a break statement only applies to the loop or switch
immediately enclosing it. It is an error to use the break statement outside a loop or a
switch.
 For example, suppose we wish to read in a user password, but would like to allow
the user a limited number of attempts:
 for (i = 0; i < attempts; ++i)
 {
 cout << “Please enter your password: ”;
 cin >> password;
 if (Verify(password)) //check password for correctness
 break; //drop out of the loop
 cout << “Incorrect!\n”:
 }
 Here we have assumed that there is a function called Verify which checks a
password and returns true if it is correct and false otherwise. If the password entered
is correct then loop will terminate break statement. Example

//Sample Program to test the Break keyword


#include < iostream.h >
#include < conio.h >
void main()
{
int i,num,flg=0;
clrscr();
for (i = 0; i < 5; ++i)
{
cout << "\n Please enter a number: ";
cin >> num;
if (num==13)
{
flg=1;
break;
}

cout << "\n Not your lucky number \n ";


}
if (flg==1)
{
cout << "\n You entered your lucky number at ";
cout << "attempt "<< i+1;
}
getch();
}

Functions

 User-defined functions as one of the main building blocks of C++ programs. A


function provides a convenient way of pack a computational recipe, so that it can be
used as often as required. A function definition consists of two parts: interface and
body. The interface of a function (also called its prototype) specifies how it may be
used. It consists of three entities:
 The function name. This is simply a unique identifier.
 The function parameters (also called its signature). This is a set of zero or more
typed identifiers used for passing values to and from the function.
 The function return type. This specifies the type of value the function returns. A
function which returns nothing should have the return type void. The body of a
function contains the computational steps (statements) that comprise the function.
 Using a function involves ‘calling’ it. A function call consists of the function name
followed by the call operator brackets ‘0’. inside which zero or more comma-
separated arguments appear. The number of arguments should match the number of
function parameters. Each argument is an expression whose type should match the
type of the corresponding parameter in the function interface.
 When a function call is executed, the arguments are first evaluated and their
resulting values are assigned to the corresponding parameters. The function body is
then executed. Finally, the function return value (if any) is passed to the caller.
 Since a call to a function whose return type is non-void yields a return value, the
call is an expression and may be used in other expressions. By contrast, a call to a
function whose return type is void is a statement.
 A Simple Function: Following program shows definition of a simplefunction which
raises an integer to the power of another, positive integer.
 int Power (int base, unsigned int exponent)
 {
 int result=1;
 for (int = 0; i < exponent; ++i)
 result * base;
 return result;
 }

Parameters and Arguments

 C++ supports two styles of parameters: value and reference. A value parameter
receives a copy of the value of the argument passed to it. As a result, if the function
makes any changes to the parameter, this will not affect the argument.
 #include < iostream.h >
 void test(int num)
 {
 num=0;
 cout << “num= ” << num << “\n”;
 }
 int main (void)
 {
 int x= 10;
 test(x);
 cout << "x =" << x ;
 return 0;
 }
 the single parameter of test() is a value parameter. As far as this function is
concerned, num behaves just like a local variable inside the function. When the
function is called and x passed to it, num receives a copy of the value of x. As a
result, although num is set to 0 by the function, this does not affect x. The Program
produces the following output:
 num =0;
 X= 10;
 A reference parameter, on the other hand, receives the argument passed to it and
works on it directly. Any changes made by the function to a reference parameter is
in effect directly applied to the argument.
 Within the context of function calls, the two styles of passing arguments are,
respectively, called pass-by-value and pass-by-reference. It is perfectly valid for a
function to use pass-by-value for some of its parameters and pass-by-reference for
others. The former is used much more often in practice.

Global and Local Scope

 Everything defined at the program scope level (i.e., outside functions and classes) is
said to have a global scope. Uninitialized global variables are automatically
initialized to zero. Since global entities are visible at the program level, they must
also be unique at the program level. This means that the same global variable or
function may not be defined more than once at the global level. Global entities are
generally accessible everywhere in the program.
 Each block in a program defines a local scope. Thus the body of a function
represents a local scope. The parameters of a function have the same scope as the
function body. Variables defined within a local scope are visible to that scope only.
Hence, a variable need only be unique within its own scope. Local scopes may be
nested, in which case the inner scopes override the outer scopes.
 Generally, the lifetime of a variable is limited to its scope. So, for example, global
variables last for the duration of program execution, while local variables are
created when their scope is entered and destroyed when their scope is exited. The
memory space for global variables is reserved prior to program execution
commencing, whereas the memory space for local variables is allocated on the fly
during program execution.

Auto Variables

 Because the lifetime of a local variable is limited and is determined automatically,


these variables are also called automatic. The storage class specifier auto may be
used to explicitly specify a local variable to be automatic.
 For example:
 void Test (void)
 {
 auto int xyz; // same as: int xyz;
 //.....
 }

Register Variables

 As mentioned earlier, variables generally denote memory locations where variable


values are stored. When the program code refers to a variable (e.g., in an
expression), the compiler generates machine code which accesses the memory
location denoted by the variable.
 For frequently-used variables (e.g., loop variables), efficiency gains can be obtained
by keeping the variable in a register instead thereby avoiding memory access for
that variable.
 The storage class specifier register may be used to indicate to the compiler that the
variable should be stored in a register if possible.

 For example:
 for (register int i = 0; i < n; ++i)
 sum += i;
 Note that register is only a hint to the compiler, and in some cases the compiler may
choose not to use a register when it is asked to do so. One reason for this is that any
machine has a limited number of registers and it may be the case that they are all in
use.
 Even when the programmer does not use register declarations, many optimizing
compilers try to make an intelligent guess and use registers where they are likely to
improve the performance of the program.

Static Variables

 It is often useful to confine the accessibility of a variable to function. This is


facilitated by the storage class specifier static.
 The variable will remain only accessible within its local scope; however, its lifetime
will no longer be confined to this scope, but will instead be global. In other words, a
static local variable is a global variable which is only accessible within its local
scope.
 Static local variables are useful when we want the value of a local variable to persist
across the calls to the function in which it appears. For example, consider an Error
function which keeps a count of the errors and aborts the program when the count
exceeds a preset limit:
 void Error (char *message)
 {
 static int count = 0; // static local variable
 if (++count> limit)
 Abort( );
 }

Example

//Sample Program to demonstrate the use of static variable, default &


parameterized constructor and destructor
#include < iostream.h >
#include < conio.h >
class item{
static int count;
int number;
public:
item(){number=0;}
void getdata (int a)
{
number = a;
count ++;
}
void getcount ()
{
cout << "\n number: " << number << endl;
cout << "\n count: ";
cout << count << endl;
}
~item(){cout << "\n Destroying the object\n";}
};
int item ::count;
void main()
{
item a,b,c;
clrscr();
cout << "\n Objects created calling default construcutor\n";
a.getcount();
b.getcount();
c.getcount();
cout << "\n Objects created with parameterized construcutor\n" <<
"\n";
a.getdata(100);
b.getdata(200);
c.getdata(300);
a.getcount();
b.getcount();
c.getcount();
getch();
}

 Like global variables, static local variables are automatically initialized to 0.


Extern Variables and Functions

 Because a global variable may be defined in one file and referred to in other files,
some means of telling the compiler that the variable is defined elsewhere may be
needed. Otherwise, the compiler may object to the variable as undefined. This is
facilitated by an extern declaration.
 For example, the declaration:
 extern int size; // variable declaration
 informs the compiler that size is actually defined somewhere (may be later in this
file or in another file). This is called a variable declaration (not definition) because
it does not lead to any storage being allocated for size.
 It is a poor programming practice to include an initializer for an extern variable,
since this causes it to become a variable definition and have storage allocated for it:
 extern int size = 10; // no longer a declaration!
 Function prototypes may also be declared as extern, but this has no effect when a
prototype appears at the global scope. It is more useful for declaring function
prototypes inside a function. For example:
 double Tangent (double angle)
 extern double sin(double); // defined elsewhere
 extern double cos(double); //defined elsewhere return sin(angle) / cos(angle);
 The best place for extern declarations is usually in header files so that they can be
easily included and shared by source files.

Default Arguments

 Default argument is a programming convenience which removes the burden of


having to specify argument values for all of a function’s parameters. For example,
consider a function for reporting errors:
 void Error (char *message, int severity = 0);
 Here, severity has a default argument of 0; both the following calls are therefore
valid.
 Error (“Division by zero”, 3); // severity set to 3
 Error (“Round off error”); //severity set to 0
 As the first call illustrates, a default argument may be overridden by explicitly
specifying an argument.
 Default arguments are suitable for situations where certain (or all) function
parameters frequently take the same values. In Error, for example, severity 0 errors
are more common than others and therefore a good candidate for default argument.
A less appropriate use of default arguments would be:
 int Power (int base, unsigned int exponent = 1);
 Because 1 (or any other value) is unlikely to be a frequently-used one in this
situation.
 To avoid ambiguity, all default arguments must be trailing arguments. Example
Example
#include < iostream.h >
#include < conio.h >
class test{
int i,j,k,sum;
public:
void add(int a, int b, int c=77)
{
i=a;
j=b;
k=c;
sum=i+j+k;
cout << "\n\n Sum is : " << sum;
}
};
void main()
{
test t1,t2;
clrscr();
cout << "\n\n Example for Default arguments";
t1.add(22,33,44);
t2.add(58,56);
getch();
}

 The following declaration is therefore illegal:


 void Error (char *message = “Bomb”, int severity); // Illegal!
 A default argument need not necessarily be a constant. Arbitrary expressions can be
used, so long as the variables used in the expression are available to the scope of the
function definition (e.g., global variables).
 The accepted convention for default arguments is to specify them in function
declarations, not function definitions. Because function declarations appear in
header files, this enables the user of a function to have control over the default
arguments. Thus different default arguments can be specified for different
situations. It is, however, illegal to specify two different default arguments for the
same function in a file.

Function Overloading

 The term overloading means ‘providing multiple definitions of’. Overloading ot


functions involves defining distinct functions which share the same name, each of
which has a unique signature. Function overloading is appropriate for:
 Defining functions which essentially do the same thing, but operate on different
data types.
 Providing alternate interfaces to the same function.
 Function overloading is purely a programming convenience. Consider a function,
GetTime, which returns in its parameter(s) the current time of the day, and suppose
that we require two variants of this function: one which returns the time as seconds
from midnight, and one which returns the time as hours, minutes, and seconds.
Given that these two functions serve the same purpose, there is no reason for them
to have different names. C++ allows functions to be overloaded, that is, the same
function to have more than one definition:
 long GetTime (void); // seconds from midnight
 void GetTime (int &hours, int &minutes, int &seconds);
 When Getlime is called, the compiler compares the number and type of arguments
in the call against the definitions of GetTime and chooses the one that matches the
call. For example: int h,m,s; long t = GetTime( ); // matches GetTime(void)
GetTime(h, m, s); // matches GetTime(int&, int&, int&);
 To avoid ambiguity, each definition of an overloaded function must have a unique
signature.
 Member functions of a class may also be overloaded:
 class Time {
 //...
 long GetTime (void); // seconds from midnight
 void GetTime (int &hours, int &minutes, int &seconds);
 Function overloading is useful for obtaining flavors that are not possible using
default arguments alone. Overloaded functions may also have default arguments:
 void Error (int errCode, char *errMsg =“”);
 void Error (char *errMsg); Click here for example

//Sample Program to overload functions


#include < iostream.h >
#include < conio.h >
class arithmatic
{
int i,j,k;
public:
void arith(int m,int n)
{
cout << "\n Function arith is called with two parameters\n\n";
i=m;
j=n;
k=i + j;
cout << " i= "<< i << " j= " << j << " Sum= " << k << endl;
}
void arith (int a,int b,int c)
{
int t;
cout << "\n Function arith is called with three parameters\n\n";
i=a;
j=b;
k=c;
t= i + j + k;
cout << " i= " << i << " j= " << j << " k= " << k << " Sum= " <<
t << endl;
}

};
void main()
{
arithmatic a1,a2;
clrscr();
a1.arith(5,6);
a2.arith(5,6,7);
getch();
}

Command Line Arguments

 When a program is executed under an operating system (such as DOS or UNIX), it


can be passed zero or more arguments. These arguments appear after the program
executable name and are separated by blanks. Because they appear on the same line
as where operating system commands are issued, they are called command line
arguments.
 As an example, consider a program named sum which prints out the sum of a set of
numbers provided to it as command line arguments. Following example illustrates
how two numbers are passed as arguments to sum.
 Prompt > sum 36 12
 Prompt > 48
 Command line arguments are made available to a C++ program via the main
function.
 int main (int argc, const char* argv[])
 This form of main is used when the program is intended to accept command line
arguments.
 The first parameter, argc, denotes the number of arguments passed to the program
(including the name of the program itself).
 The second parameter, argv, is an array of the string constants which represent the
arguments.
 Example: (Strings are converted to real numbers using atof, which is defined in
stdlib.h) Click here for example

// Sample Program to demonstrate the use of Command line arguments


#include < stdio.h >
#include < conio.h >

void main (int argc, char *argv[])


{
int count;
clrscr();
printf ("Number of arguments %d \n", argc);
for (count = 0; count < argc; count ++)
{
printf ("argv[%d] = %s\n", count,argv[count]);
}
getch( );
}

Casting

 For the example given, if the input given as follows Prompt > sum 36 12 Then
 argc is 3
 argv[0] is “sum”
 argv[1] is “10.4”
 argv[2] is “12.5”

Arrays

 Apart from the broad spectrum of data types, C++ supports arrays. This data type is
useful when a group of elements are to be represented by a common name.
 An array is a group of elements that share a common name, that are differentiated
from one another by their positions within the array. An array is a collection of
homogeneous data.
 For example, 21, 20, 14, 25 and 13 are marks in 5 subjects of a student. To store
these five marks, five variables would be required test1, test2, test3, test4 and test5.
For such a representation, arrays are useful. This can be represented as test The
ability of using a single name to represent a collection of items and to refer to an
item by specifying the location number enables the programmer to develop concise
and efficient programs

Declaring arrays

 An array must be declared, since it is basically a variable.


 Syntax: type variable-name [size]; Example: int marks[5];
 This example describes an array of 5 integer elements and the elements are
numbered starting with 0. Each element is accessed by the array name followed by
its position in square brackets. This position is known as the subscript, thus each
array statement is a subscripted variable. Therefore, you would have: mark mark[0],
mark[1], mark[2], mark[3] and mark[4].
 If the array is accessed beyond its limit, C++ does not report it as an error. Having
declared mark, accessing mark[5] is not a syntax error. The program does not
terminate abnormally, though it is a logical error as the last legal element is mark[4]
 One Dimensional Array is a list of data items that can be given one variable name
using one subscript and such a variable is called a one dimensional array.
 The array storage starts from 0 and hence the loop termination is i < 10. The second
loop is to display the 10 integers.

Initialization of Arrays

 An array can be initialized along with its declaration.


 SYNTAX: static type array-name[size]={val1,val2...};
 static int count[3]={0,0,0};
 will declare the variable count as an array of size 3 and will assign zero to each
element. Note that this declares a static variable. The size parameter may be
omitted. In such cases, the compiler allocates enough spaces for all initialized
elements.
 static int count[]={0,0,0};

Two Dimensional Arrays


 The following table could be considered as a two dimensional array:

 Then score[0][0] represents the marks of the first student and score[0][1] the rank of
the first student.
 C++ allows us to define such a table of items by defining a two dimensional array.
 As with the single dimensional array, each element of the array is indexed from zero
to its maximum size minus one, the first index stands for row and the second index
for column. C++ uses the row major arrangement to access the elements of the array
- first number represents the row and the second number represents the column.

Declaration

 int score [3][2]


 will declare a table of 3 rows and 2 columns.
 Initialization: Similar to the one dimensional arrays, two dimensional arrays can
also be initialized.
 static int table[2][3]={0,0,0,1,1,1};
 initializes the elements of the first row to zero and the second row to one. Note that
initialization is done row by row. For better degree of readability, you can also write
the same initialization in the matrix form, as:
 static int table[2][3]={{0,0,0},{1 ,1 ,1}};
 The inner braces are optional, but it is good practice to use them, as they improve
the readability.
 # include < iostream.h >
 void main()
 int num[3][3];
 int i,j;
 cout << “Enter the numbers into the array” << endl;
 for (i=0;i < 3;i++)
 {
 for (j=0;j < 3;j++)
 cin >> num[i][j];
 }
 for (i=0;i<3;i++)
 {
 for (j=0;j<3;j++)
 cout << num[i][j] << ‘\t’;
 cout << endl;
 }
 }
 Multidimensional Arrays C++ permits arrays of more than two
dimensions. The maximum number of dimensions depends on the
compiler.

 SYNTAX: type array-name[s1][s2][s3]...[sm];
 Click here for example

//Sample Program to Demonstrate the use of Array


#include < stdio.h >
#include < conio.h >
void main()
{
int a[10],i,n,sum;
clrscr();
sum=0;
printf("Enter the number of elements: ");
scanf("%d",&n);
flushall();
printf("\nEnter the elements\n\n");
for(i=0;i < n;i++)
{
scanf("%d",&a[i]);
flushall();
}
printf("\nEntered Array elements are\n\n");
for(i=0;i < n;i++)
{
printf("%d\n",a[i]);
sum=sum+a[i];
}
printf("\nSum of all the elements of array is: %d",sum);
getch();
}

Strings

 In C++ strings are really arrays, but there are some different functions that are used
for strings, like adding to strings, finding the length of strings, and also of checking
to see if strings match.
 The definition of a string would be anything that contains more than one character
string together. For example,“This” is a string. However, single characters will not
be strings, though they can be used as strings.
 Strings are arrays of chars. Static strings are words surrounded by double quotation
marks. “This is a static string”
 To declare a string of 50 letters, you would want to say:
 char string[50];
 This would declare a string with a length of 50 characters. Do not forget that arrays
begin at zero, not 1 for the index number.
 In addition, a string ends with a null character, literally a ‘\0’ character,that there
will be an extra character on the end on a string.
 It is like a period at the end of a sentence, it is not counted as a letter, but it still
takes up a space.
 Technically, in a fifty char array you could only hold 49 letters and one null
character at the end to terminate the string.
 Strings are useful for holding all types of long input.
 If you want the user to input his or her name, you must use a string. Using cin>> to
input a string works, but it will terminate the string after it reads the first space.
 The best way to handle this situation is to use the function cin.getline.
 Technically cin is a class, and you are calling one of its member functions.
 The most important thing is to understand how to use the function however.
 The prototype for that function is: cin.getline(char *buffer ,int length, char
terminal_char);
 The char *buffer is a pointer to the first element of the character array, so that it can
actually be used to access the array.
 The int length is simply how long the string to be input can be at its maximum (how
big the array is).
 The char terminal_char means that the string will terminate if the user inputs
whatever that character is. Keep in mind that it will discard whatever the terminal
character is.
 It is possible to make a function call of cin.getline(arry,‘\n’); without the length, or
vice versa, cin.getline(arry,50); without the terminal character. Note that \n is he
way of actually telling the compiler you mean a new line, i.e. someone hitting the
enter key.
 #include < iostream.h >
 int main(){
 char string[256]; // A nice long string
 cout << “Please enter a long string:” ;
 cin.getline(string, 256, ‘\n’); //The user input goes into string
 cout << “Your long string was:” << endl << string;
 return 0;}

String Functions

 String.h is a header file that contains many functions for manipulating strings.
 One of these ,strcmp is the string comparison function. 1) int strcmp(const char *s1,
const char *s2); strcmp will accept two strings. It will return an integer. This integer
will either be:
 Negative if S1 is less than s2.
 Zero if S1 and S2 are equal.
 Positive if S1 is greater than S2.
 strcmp is case sensitive. strcmp also passes the address of the character array to the
function to allow it to be accessed. 2) char *strcat(char *dest char *src);
 strcat is short for string concatenate, which means to add to the end, or append. It
adds the second string to the first string. It returns a pointer to the concatenated
string.
 char *strupr(char *s);
 strupr converts a string to uppercase. It also returns a string, which will all be in
uppercase. The input string, if it is an array and not a static string, will also all be
uppercase.
 char *strlwr(char *S);
 strlwr converts a string to lowercase. It also returns a string, which will all be in
lowercase. The input string, if it is an array, will also all be lowercase.
 size_t =strlen(const char *)
 strlen will return the length of a string, minus the terminating character(‘/0’).
 The size_t is nothing to worry about. Just treat it as an integer, which it is. Here is a
small program using many of the previously described functions Click Here For
example

//Sample Program to demonstrate the use of different string functions


#include < iostream.h > //For. cout
#include < string.h > //For many of the string functions
#include < conio.h >
void main()
{
char name[50]; //Declare variables
char lastname[50];
clrscr();
cout << "Please enter your name: " ; //Tell the user what to do
cin.getline(name, 50); //User gets to input strings with spaces or just
to get strings after the
//user presses enter
if(!strcmp(" Nhep Bora", name)) //The ! means not, strcmpi returns 0
for equal strings
{
cout << "That's my name too." << endl; //Tell the user if its my name
}
else
{
cout << " That's not my name.";
}
cout << "What is your name in uppercase. .." << endl;
strupr(name); //strupr converts the string to uppercase
cout << name << endl;
cout << "And, your name in lowercase... " << endl;
strlwr(name); //strlwr converts the string to lowercase
cout << name << endl;
cout << "Your name is " << strlen(name) << " letters long " << endl;
cout << "your last name:";
cin.getline(lastname, 50); //lastname is also a string
strcat(name, " "); //We want to space the two names apart
strcat(name, lastname); //Now we put them together, we a space in
the middle
cout <<
" full name is " << name; //Outputting it all...
getch();
}

Structure
 Why do we need a structure?
 We have seen till now that a variable can be of single data type either an int or float
or char etc.
 But if we want to store information consisting of different data types as a single
entity then what do we do?
 Consider the example where we want to store the information of an employee of an
organization and the employee details included employee number, employee name
his address etc.
 So C provides a data type called ‘structure’. A structure gathers together, different
types of information that comprise a given entity.
 A structure is a collection of one or more variable, possibly of different types
grouped together under a single name for the convenience.
 Syntax to declare a structure is:
 struct name of the structure {
 structure elements;
 };
 Example:
 struct employee {
 int emp_no;
 char emp_name[25];
 };
 Once the structure data type has been defined one or more variables can be declared
to be of that type.
 For example the variables e1, e2, e3, e4 can be declared to be of the type struct
employee as struct employee e1,e2, e3,e4;
 The above statement set aside 27 bytes of memory for each of the variable e1, e2, e3
and e4 as each of the variable need to store an integer value and a string i.e. two
bytes for integer and a character array of 25 bytes.

Different ways of defining the structures and their variables

 We can also combine definition of structure type and the declaration of the variables
in one statement.

 Example:
 struct employee {
 int emp_no;
 char emp_name[25];
 } e1, e2, e3, e4;
 If we are combining definition of structure type and the
declaration of the variables in one statement then we can even omit
the name of the structure type.

 Example:
 struct{
 int emp_no;
 char emp_name[25];
 } e1, e2, e3, e4;

Structure variables can also be initialized at the time of


declaration.
Example:
struct employee {
int emp_no;
char emp_name[25];
} ;
struct employee e1= { 101, “Krithika”};
struct employee e1= { 101, ‘Gokul”};
Example:
struct employee {
int emp_no;
char emp_name[25];
}
e1, e2,e3= { 101, “Krithika”};

 Note: In the above example only the variable e3 is initialized.


 We can even assign the value of one structure variable to another. Example: struct
employee { int emp_no; char emp_name[25]; } e1, e2,e3= { 101, “Krithika”};
e2=e3; Note:
 Semicolon must follow the closing brace of the structure type definition
 A structure type defines only the form of a structure and compiler will not reserve
any space in memory

Accessing the structure elements

 To access the elements of structure dot operator is used along with the structure
variable. Example: e1.emp_no; e1.emp_name;
 Whatever be the elements of a structure, they are always stored in contiguous
memory locations.
 The example given reads the elements of the structure and displays the elements.
 # include < stdio.h >
 main ( )
 {
 struct employee
 {
 int emp_no;
 char emp_name [25];
 };
 struct employee e1;
 printf (“Enter the Employee number and name\n”);
 scanf (“%d%s”,&e1.emp_no,&e1.emp_name);
 printf(“Employee Number is %d and Name is %s\n”,e1.emp_no,
e1.emp_name);
 }

One structure can be nested within another structure

 A structure can be the element of another structure, which is given in the example.
 Example:
 struct empdt
 {
 char emphname [10];
 int hnum;
 };
 struct employee
 {
 char empname [25];
 int empno;
 struct empdt empad;
 } e1;

Example

# include < stdio.h >


# include < conio.h >
void main ( )
{
struct empdt
{
char emphname[10];
int hnum;
};
struct employee
{
char empname [25];
int empno;
struct empdt empad;
}e1;
clrscr();
printf("\n\n Enter Employee details\n");
printf ("\n Enter the number: ");
scanf("%d",&e1.empno);
printf ("\n Enter the name: ");
scanf("%s",&e1.empname);
printf ("\n Enter house number: ");
scanf("%d",&e1.empad.hnum);
printf ("\n Enter house name: ");
scanf("%s",&e1.empad.emphname);
printf("\n\n Entered details of employee\n\n");
printf ("\n Employee Number = %d\n",e1.empno);
printf ("\n Employee Name = %s\n",e1.empname);
printf ("\n Employee House Number = %d\n",e1.empad.hnum);
printf ("\n Employee House Name = %s\n",e1.empad.emphname);
getch();
}

Preprocessor directives
 Sentences that begin with a pound sign (#) are directives for the preprocessor.
 They are not executable code lines but indications for the compiler.
 The preprocessor is executed automatically by the compiler when we compile a
program in C++ and is in charge of making the first verifications and digestions of
the program’s code.
 All these directives must be specified in a single line of code and they do not have
to include an ending semicolon:
 #define
 the #define directive defines an identifier and a character sequence that will be
substituted for the identifier each time it is encountered in the source file. the
identifier is referred to as a macro name and the replacement process as macro
replacement.
 The general form of the directive is
 #define name value #define MAX_WIDTH 100 char str1[MAX_WIDTH]; char
str2[MAX_WIDTH];
 It defines two strings to store up to 100 characters
 #define can also be used to generate macro functions:
 #define getmax(a,b) a>b?a:b
 int x=5, y;
 y = getmax(x,2);
 after the execution of this code y would contain 5
 #undef
 #undef fulfills the inverse functionality of #define. It eliminates from the list of
defined constants the one that has the name passed as a parameter to #undef #define
MAX_WIDTH 100 char str1[MAX_WIDTH] #undef MAX_WIDTH #define
MAX_WIDTH 200 char str2[MAX_WIDTH];
 #ifdef, #ifndef, #if, #endif, #else and #eIif These directives allow discarding part of
the code of a program if a certain condition is not fulfilled
 #ifdef allows that a section of a program is compiled only if the defined constant
that is specified as the parameter has been defined, independently of its value.
 In the case given below, the line char str [MAX_WIDTH]; is only considered by the
compiler if the defined constant MAX_WIDTH has been previously defined,
independently of its value. If it has not been defined, that line will not be included in
the program.

 #ifndef serves for the opposite: the code between the #ifndef directive and the
#endif directive is only compiled if the constant name that is specified has not been
defined previously.
 For example: #ifndef MAX WIDTH
 #define MAX_WIDTH 100
 #endif
 char str [MAX_WIDTH];
 In this case, if when arriving at this piece of code the defined constant
MAX_WIDTH has not yet been defined it would be defined with a value of 100. If
it already existed it would maintain the value that it had (because the #define
statement won't be executed).
 The #if, #else and #elif (elif = else if) directives serve so that the portion of code
that follows is compiled only if the specified condition is met. The condition can
only serve to evaluate constant expressions.
 For example:
 #if MAX_WIDTH>200
 #undef MAX_WIDTH
 #define MAX_WIDTH 200
 #elsif MAX WIDTH<50
 #undef MAX_WIDTH
 #define MAX _WIDTH 50
 #else
 #undef MAX_WIDTH
 #define MAX_WIDTH 100
 #endif
 char str [MAX_WIDTH];
 Notice how the structure of chained directives #if, #elsif and #else finishes with
#endif.
 #include
 When the preprocessor finds an #include directive it replaces it by the whole
content of the specified file. There are two ways to specify a file to be included:
 #include " file "
 #include < file >
 The only difference between both expressions is the directories in which the
compiler is going to look for the file. In the first case where the file is specified
between quotes, the file is looked for in the same directory that includes the file
containing the directive. In case that it is not there, the compiler looks for the file in
the default directories where it is configured to look for the standard header files.
 If the file name is enclosed between angle-brackets <>the file is looked for directly
where the compiler is configured to look for the standard header files.

Unit 2
Class and Objects

 A class is an abstract idea that can be represented with data structures and functions.
 The class combines data and functions (which work on those data) into a single
programming unit.
 Functions associated with a class are called methods. This is an ‘Encapsulation’.
 Data and procedures are tied together logically.
 The parts that make up a class can be protected from the rest of the program to varying
degrees.
 The programmer has control over the protection of individual entries in the class
definition.
 An object is said to be an instance of a class.
 A class definition consists of two parts: header and body. The class header specifies the
class name and its base classes.
 The class body defines the class members.
 Two types of members are supported:
 Data members have the syntax of variable definitions and specify the representation of
class objects.
 Member functions have the syntax of function prototypes and specify the class
operations, also called the class interface.
 Class members fall under one of three different access permission categories:
 Public members are accessible by all class users.
 Private members are only accessible by the class members.
 Protected members are only accessible by the class members and the members of a
derived class.
 The data type defined by a class is used in exactly the same way as a built in type.
An example program

Explanation for the above program


1) This line contains the class header and names the class as Point. A class definition
always begins with the keyword class, followed by the class name. An open brace marks
the beginning of the class body.
2) This line defines two data members, xVal and yVal, both of type int. The default access
permission for a class member is private. Both xVaJ and yVal are therefore private.
3) This keyword specifies that from this point onward the class members are public.
4,5,6)These three are public member functions. Both have two integer parameters and a
void return type.
7)This brace marks the end of the class body

 The actual definition of the member functions is usually not part of the class and appears
separately. Following code shows the separate definition of SetPt and OffsetPt.
Example

#include < iostream.h >


#include < conio.h >
class item
{
private:
int number;
float cost;
public:
void getdata(int a,float b);
void putdata(void);
};
void main()
{
clrscr();
item x,y,z;
x.getdata(100,25.5);
x.putdata();
getch();
}
void item::getdata(int a,float b)
{
number=a;
cost=b;
}
void item::putdata(void)
{
cout << "number=" << number;
cou << "\n cost=" << cost;
}

Data Hiding
 A key feature of object oriented programming is 'data - hiding' i.e., the data is concealed
within a class, so that it cannot be accessed mistakenly by functions outside the class.
 'private' and 'public' are two types of protection available within a class.
 Items marked with 'private' can only be accessed by methods defined as part of the class.
Data is most often defined as private.
Private members can be accessed by members of the class.
 Public items can be accessed from anywhere in the program without restrictions. Class
methods are usually public. As a general rule, data should not be declared public. Public
members can be accessed by members and objects of the class.
 Protected is another type of protection available within a class. Items declared as
protected are private within a class and are available for private access in the derived class.
The derived class concept is dealt with later in this book. The protected access will be
discussed again.

Example:
class fact{
int n,fct,temp;
public:
void read_data()
{
printf("Enter the Integer whose factorial you want to find\n");
scanf("%d", &n);
}
void calc_fact()
{
fct=1;
temp=n;
while (n>0)
{
fct=fct*n;
n--;
}
printf("Factorial of %d is %d \n",temp,fct);
}
};
void main()
{
fact f;
clrscr();
f.read_data();
f.calc_fact();
getch();
}

Note: As shown in the above example the variables n, fct and temp are private and can not be accessed
from the main function but can be accessed from the public functions read_data() and calc_data()
Constructors and Destructors
 C++ provides a special member function called the constructors which enables an object
to initialize itself when it is created.
 This is known as automatic initialization of objects.
 It also provides another member function called the DESTRUCTOR that destroys the
objects when they are no longer required.

Constructors
 A constructor is a special member function whose task is to initialize the objects of its
class.
 It is special because its name is the same as the class name.
 The constructor is invoked whenever an object of its associated class is created.
 It is called constructor because it constructs the values of data members of the class.

A constructor is declared and defined as follows

class integer
{
int m,n;
public:
integer (void); // constructor declared
…………
……….
};
integer :: integer (void) // constructor defined
{
m=0;
n=0;
}
 When a class contains a constructor , it is guaranteed that an object created by the class
will be initialized automatically.
 integer int1;
not only creates the object int1 but also initializes its data members m and n to zero.
 A constructor that accepts no parameters is called the default constructor.
 For eg: default constructor for class A is A :: A()

The constructor functions have some special characteristics

 Its name is the same as the class name.


 They should be declared in the public section.
 They are invoked automatically when the objects are created.
 They do not have return types, not even void and therefore, and they cannot return
values.
 They cannot be inherited.
 They can have default arguments.
 Constructors cannot be virtual.
 We cannot refer to their addresses.
 An object with a constructor (or destructor) cannot be used as a member of a union.
 They make 'implicit calls' to the operators new and delete when memory allocation
is required.

Parameterized constructors

 It may be necessary to initialize the various data elements of different objects with
different values when they are created.
 C++ permits us to achieve this objective by passing arguments to the constructor
function when the objects are created.
 The constructors that can take arguments are called parameterized constructors.

Example:

ass integer
{
int m,n;
public:
integer (int x, int y); // parameterized constructor
…………
……….
};
integer :: integer (int x, int y)
{
m=x;
n=y;
}

 We must pass the initial values as arguments to the constructor function when an
object is declared.
 This can be done in 2 ways:

1) By calling the constructor explicitly:


i.e integer int1 = integer(0, 100);
This statement creates an integer object int1 and passes the values 0 and 100 to it.

2) By calling the constructor implicitly:


i.e integer int1(0, 100);
This method is sometimes called the shorthand method.

Example
class integer
{
public:
integer(int,int)// constructor declared
void display()
{
cout << "m=" << m << endl;
cout << "n=" << n << endl;
}
};
integer :: integer (int x, int y) // constructor defined
{
m=x;
n=y;
}
int main()
{
integer int1(0, 100);
integer int2 = integer(25, 75);
cout << "OBJECT1=" << endl;
int1.display();
cout << "OBJECT2=" << endl;
int2.display();
return 0;
}

The constructor function can also be defined within the class

The parameters of a constructor can be of any type except that of the class
to which it belongs.
Multiple constructors in a class (Overloading of constructors)

C++ permits to use more than one constructor in the same class.

For example:

 class integer
 {

 int m,n;
 public:
 integer()
 { m=0; n=0; } //C-1
 integer (int a, int b)
 {m=a; n=b; } //C-2
 integer (integer & i)
 { m=i.m; n=i.n; } //C-3
};

 This declares 3 constructors for an integer object.


 Third receives one integer object as an argument.
 For ex: The declaration, integer I1;
would automatically invoke the first constructor and set both m and n to I1 to zero
 The statement, integer I2(20, 40);
would call the second constructor
 The statement, integer I3( I2);
would invoke the third constructor which copies the values of I2 into I3.
In other words, it sets the value of every data element of I3 to the value of the
corresponding data element of I2. Such a constructor is called copy constructor.
 When more than one constructor function is defined in a class, we say that
constructor is overloaded.
Example here
//Sample program to add two Complex numbers using class and object
#include < iostream.h >
#include < conio.h >

class complex
{ float x ,y;
public:
complex() { } //C1
complex (float a){ x=y=a; } //C2
complex (float real, float img)
{ x= real; y= img; } //C3
complex sum (complex);
void show () ;
};
complex complex::sum (complex c)
{
complex temp;
temp.x = x + c.x;
temp.y = y + c.y;
return(temp);
}
void complex ::show ()
{
cout << x << "+j" << y << endl;
}
void main()
{
clrscr();
complex A(2.7, 3.5);
complex B (1.6);
complex C;
C= A.sum (B);
cout << "\n First Complex Number A= ";
A.show();
cout << "\n Second Complex Number B= ";
B.show();
cout << "\n Sum of two Complex Numbers C= ";
C.show();
getch();
}

Constructors with default arguments

 It is possible to define constructors with default arguments.


 For example:
complex (float real, float imag=0);
Then the statement,
complex C (5.0);
assigns the value 5.0 to real and zero to imag (by default)
 The statement, complex C (2.0, 3.0);
assigns 2.0 to real, 3.0 to imag. Overrides the default value.

Destructors
 A destructor, as the name implies, is used to destroy the objects that have been
created by a constructor.
 The destructor is a member function whose name is the same as the class name but
is preceded by a tilde.
 For example, the destructor for the class integer can be defined
~integer() { }
 A destructor never takes any argument nor returns any value.

It will be invoked implicitly by the compiler upon exit from the program to clean up
storage that is no longer accessible.

 Usage of destructor , saves the memory.

Example

#include < iostream.h >


class test{
int x,y;
public:
test(){x=0; y=0;cout << "\nconstructor\n";}
test(int i,int j){
x=i;
y=j; }
~test();
void showdata() {
cout << "x " << x;
cout << "y " << y; } };
test::~test() {
cout << "\ndestructing the object\n"; }
void main() {
test t1,t2;
t2=test(50,25);
t2.showdata();
}

Static data members


 A data member of a class can be qualified as static.
 A static member variable has certain special characteristics.
They are:
 It is initialized to zero when the first object of its class is created. No other
initialization is permitted.
 Only one copy of that member is created for the entire class and is shared by all the
objects of that class, no matter how many objects are created.
 It is visible only within the class, but its lifetime is the entire program.
 Static variables are normally used to maintain values common to the entire class.
 The type and the scope of each static member variable must be defined outside the
class definition.
 Because , the static data members are stored separately rather than as a part of an
object.
 They are also known as class variables.

Click here for example

//Program to demonstrate the use of static variable, default constructor


and destructor
#include < iostream.h >
#include < conio.h >
class item{
static int count;
int number;
public:
item(){number=0;}
void getdata (int a)
{
number = a;
count ++;
}
void getcount ()
{
cout << "\n number: " << number << endl;
cout << "\n count: ";
cout << count << endl;
}
~item(){cout << "\n Destroying the object\n";}
};
int item ::count;
void main()
{
item a,b,c;
clrscr();
cout << "\n Objects created calling default construcutor\n";
a.getcount();
b.getcount();
c.getcount();
cout << "\n Objects created with the member function\n" ;
a.getdata(100);
b.getdata(200);
c.getdata(300);
a.getcount();
b.getcount();
c.getcount();
getch();
}

Sharing of a static data member

Static member functions:

A member function that is declared static has the following properties:

o A static function can have access to only other static members (functions or
variables) declared in the same class.
o A static member function can be called using the class name(instead of its
objects) as follows:
class-name :: function-name;

Example
Friend Functions

We have seen that the private members cannot be accessed from outside the class. That is, a
non member function can not have an access to the private data of the class.

But, there could be a situation where we would like 2 classes to share a particular function.
For ex:
Consider a case where 2 classes manager and scientist have been defined, and we would
like to use a function income-tax() to operate on the objects of both these classes.

In such situations, C++ allows the common function to be made friendly with both the
classes, there by allowing the function to have access to the private data of these classes.

Such functions need not be a member of any of these classes.

To make an outside function "friendly" to a class, we have to declare function as a friend.

Ex:

class ABC
{
…….
……..
public:
……..
………
friend void xyz(); //declaration
};
The function declaration should be preceded by the keyword friend.

The function definition does not use either the keyword friend or the scope operator ::.

The functions that are declared with the keyword friend are known as "friend functions".

A friend function processes certain special characteristics

It is not in the scope of the class to which it has been declared as friend.

Since it is not in the scope of the class, it cannot be called using the object of that class.

It can be invoked like a normal function without the help of any object.

Unlike member functions, it cannot access the member names directly and has to use an
object name and dot operator with each member name.

It can be declared either in the public or the private part of a class without affecting its
meaning.

Usually, it has the objects as arguments.

Example

The friend function accesses the class variables a and b by using the dot operator and the
object passed to it.

The function call mean(X) passes the object X by value to the friend function.

Member functions of one class can be friend functions of another class.

In such cases they are defined using the scope resolution operator as shown below:

class X
{
…..
…….
int fun1(); //member fun of x
};

class Y
{
……
……
friend int X :: fun1(); //fun1() of X
……….
};
The function fun1() is a member of class X and a friend of class Y.

We can also declare all the member functions of one class as the friend functions of another
class.

In such cases, the class is called a friend class.

Ex:
class z
{
………..
friend class x; //all member functions of x are
//friends to z
};

class sample
{
int a;
int b;
public:
void setvalue()
{
a=25; b=40;
}
friend float mean(sample s);
};
float mean(sample s)
{
return float ( s.a + s.b )/2.0;
}

int main()
{
sample X;
X.setvalue();
cout << “mean value= ”<< mean(X);
return(0);
}

Returning Objects

 A function cannot only receive objects as arguments but also can return them.
 Following example illustrates how an object can be created (within a function) and
returned to another function.

Example
//Sample Program to demonstrate the usage of friend function
#include < iostream.h >
#include < conio.h >

class complex
{ float x ,y;
public:
complex() { } //C1
complex (float a){ x=y=a; } //C2
complex (float real, float img)
{ x= real; y= img; } //C3
friend complex sum (complex,complex);
friend void show (complex) ;
};
complex sum (complex c1, complex c2)
{
complex c3;
c3.x = c1.x + c2.x;
c3.y = c1.y + c2.y;
return(c3);
}
void show (complex c)
{
cout << c.x << "+j" << c.y << endl;
}
void main()
{
clrscr();
complex A(2.7, 3.5);
complex B (1.6);
complex C;
C= sum (A, B);

cout << "\n First Complex number is A= ";


show(A);
cout << "\n Second Complex number is B= ";
show(B);
cout << "\n Sum of two Complex number is C= ";
show(C);
complex P, Q, R;
P= complex (2.5, 3.9);
Q= complex (1.6, 2.5);
R= sum (P, Q);
cout << "\n\n First Complex number is P= ";
show (P);
cout << "\n Second Complex number is Q= ";
show (Q);
cout << "\n Sum of two Complex number is R= ";
show (R);
getch();
}

Friend function Example

Example
#include < iostream.h >
// Declaration of the function to be made as friend for the C++
int AddToFriend(int x);
class CPP_Tutorial
{
int private_data;
friend int AddToFriend(int x);
public:
CPP_Tutorial()
{
private_data =5;
}
};
int AddToFriend(int x)
{
CPP_Tutorial var1;
return var1.private_data + x;}

int main() {
cout << “Added Result for this C++ tutorial: “<< AddToFriend(4) << endl;
}

Friend class example

Example
Declaration of a friend class is also similar. Only thing is a class
definition is slightly different.
#include < iostream.h >
class CPP_Tutoriàl
{
in private_data:
friend class friendclass;
public:
CPP_Tutorial()
{
private_data =5;
} };
class friendclass
{
public:
int subtractfrom(int x)
{
CPP_Tutorial var2;
return var2.private_data - x;
}
int main()
{
friendclass var3;
cout << “Added Result for this C++ tutorial:”<< var3.subtracttrom(2)<<
ENDL;
}
This is a good way out given by C++ to avoid restrictions on private variables.

But this should be used with caution though.

If all the functions and classes are declared as friends, then the concept of encapsulation
and data security will be totally lost.

Disadvantage of Friend Class/Functions:

The aim of using object oriented programming is to protect the data members from
accidental or deliberate modifications.

The data is to be accessed only through the member functions of that class.

By declaring a friend, the private variables can be accessed by the friend which defeats the
purpose of OOP.

Operator Overloading
 "C++ has the ability to provide the operators with a special meaning. The
mechanism of giving special meaning to an operator is known as operator
overloading."
 Operator overloading provides for the creation of new definitions for most of the
C++ operators. We can create a new language of our own by the creative use of the
function and operator overloading techniques.
 The term overloading means 'providing multiple definitions' .
 Operators overloading are similar to functions overloading in that they take
operands (arguments) and return a value.
 Most of the built-in C++ operators are already overloaded. For example, the +
operator can be used to add two integers, two reals, or two addresses. Therefore, it
has multiple definitions.
 The built-in definitions of the operators are restricted to built-in types.
 Even user defined types can be overloaded. Overloaded operators are those that
have been redefined within a C++ class using the keyword 'operator' followed by an
operator symbol. We can overload all the C++ operators except the following:
o Class member access operator (., .*)
o Scope resolution operator (::)
o Size operator (sizeof)
o Conditional operator ( ?: )

Defining operator overloading


 To define additional task to an operator, we must specify what it means in relation
to the class to which the operator is applied. This is done with the help of a special
function, called operator function, which describes the task. The general form of an
operator function is: return type classname :: operator op(arglist) { Function body
//task defined } Where return type is the type of value returned by the specified
operation and op is the operator being overloaded.
 The op is preceded by the keyword Operator.
 Operator op is the function name.

Overloading Unary Operators

Unary operators are +, -, ++ and --.


The syntax of overloading is:
return type operator unary operator (arguments)
{
function body
}

Example

# include < iostream.h >


class counter
{
private: unsigned int count;
public : counter() {count = 0;}
int get_count () { return count;}
void operator ++() {count++;}
};
void main()
{
counter c1 ,c2;
cout << "\n c1 =" << c1.get_count();
cout << "c2 ="<< c2.get_count();
c1++;
c2++;
++c2;
cout << "\n c1 ="<< c1 .get_count();
cout << "c2=" << c2.getcount();
}
Output of the Example
c1=0 c2=0
c1=1 c2=2

 In the example the operator is applied once to c1 and twice to c2.


 Both prefix and postfix notation is used on c2.
 The function declaration void operator ++() tells the compiler to call this member
function whenever the ++ operator is encountered, provided the operand is of type
counter.
 Since member functions can always access the particular object for which they have
been called, this operator requires no arguments.
 Notice that the return type of the operator is void. Therefore, it does not return any
value.
 Hence, it cannot be used in assignment statements as in c1 = c2++;
 To rectify this, you have to specify the return type as the name of the object, create
a temporary object and return it. This is shown in the example. In this program,
operator overloading is done as follows:
 1. member data count is incremented.
 2. a new item temp is created of counter type.
 3. the incremented value is assigned to this variable - temp
 4. temp is returned.
 Expressions like c1 ++ now return a value, so they can be used in other expressions
such as
 c2 = c1 ++; and c2++.get_count();

Operator Overloading with Return Values

Example
# include < iostream.h >
class counter
{
private: unsigned int count;
public : counter() { count = 0; }
counter(int x) { count = x; }
int get_count() {return count;}
counter operator ++ ()
{
count ++;
counter temp;
temp.count = count;
return temp;
}
};
void main()
{
counter c1 ,c2;
cout << "\n c1 = " << c1 .get_count() << "\n c2 =" << c2.get_count();
c1++; c2=c1++;
cout << "\n cl =" < c1.get_count() <<"\n c2 =" << c2++.get_count();
}
Nameless Temporary Objects

 In the previous example, a temporary object of type counter was created for the
exclusive purpose of providing a return value for the ++ operator. The next example
shows another approach to the same problem without using a temporary object.
 # include < iostream.h >
 class counter
 {
 private:unsigned int count;
 public:counter() {count = 0;}
 counter (int c) { count=c; }
 int get_count() { return count; }
 counter operator ++()
 {
 count++;
 return (*this);
 }
 };
 void main()
 {
 counter c1 ,c2;
 cout << "\n c1 =" << c1 .get_count() << "\n c2=" << c2.getcount();
 c1++; c2=c1++;
 cout << "\nc1 =" << c1 .get_count() << "\n c2 = "<<
c2++.get_count();
 }
 this is a special pointer that points to the current object. It has been used here to
return the incremented value without creating a new object.

Overloading unary minus

class space {
int x; int y; int z;
public:
void getdata(int a,int b,int c);
void display();
void operator-(); };
//overload unary minus
void space :: getdata(int a,int b,int c)
{
x=a;
y=b;
z=c; }
void space:: display() {
cout << x <<" ";
cout << y <<" ";
cout << z << " "; }
void space :: operator-() {
x=-x;
y=-y;
z=-z; }
main() {
space S;
S.getdata(10,-20,30);
cout << "S= ";
S.display();
-S; //activates operator-()
//function
cout << "S=";
S.display();
return 0; }

OUTPUT:
S=10 -20 30
S=-10 20 -30

 In the above program a – operator when used as a unary, takes just one operand.
This operator changes the sign of an operand when applied to a basic data item.
 Here the function operator-() takes no argument.
 It changes the sign of data members of the object S.
 Since the function is a member function of same class, It can directly access the
members of the object which activated it.
 The statement like
 S2= -S1 will not work. Because, the function operator-() does not return any value.

Limitation of Increment Operators

 When applied to basic data types prefix and postfix operators are two different
operators. But when they are overloaded there is no distinction between prefix and
postfix notation.
 The expression c2 = c1 ++ has exactly the same effect as c2 = ++c1;
 In both cases c1 is assigned to c2. If you need the basic data type implementation
for the overloaded function, then it requires two overloaded operators, one for prefix
and one for postfix.
 operator ++(); //prefix. operator ++ (int); // postfix.
 The second declaration uses a dummy int argument, which is set to 0 automatically
by the postfix ++ operator. This extra argument allows the compiler to distinguish
the two forms.

Overloading Arithmetic Operators

 The functional notation [ex: C=sum (A+B)] can be replaced by a natural looking
expression.
 Ex: C= A+B;
 By overloading the + operator using an operator+() function.

Example
class complex
{
float x,y;
public:
complex(){}//c1
complex(float real,float img)//c2
{
x=real;
y=img;
}
complex opertaor+(complex);
void display();
};
complex complex::operator+(complex c)
{
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return(temp);
}
void complex::display()
{
cout << "x << "+j" << y;
}
main()
{
complex c1,c2,c3;
c1=complex(2.5,3.5);
c2=complex(1.6,2.7);
c3=c1+c2;
cout << "C1= "; C1.display();
cout << "C2= "; C2.display();
cout << "C3= "; C3.display();
return 0;

Overloading Comparison Operators

 Just as the arithmetic operators have been overloaded, the relational operators can
also be overloaded.
 The following example shows how to overload the 'Equal to' (==) operator.

Overload == and +

const int sz=80;


enum boolean{false,true};
class string
{
char str[80];
public:
string(){strcpy(str," ");}
string(char s[ ]) {strcpy(str,s);}
void display() {cout << str;}
void getstr() { gets(str);}
boolean operator==(string ss)
{
return
(strcmp(str,ss.str)==0)?true:false;
}
string operator +(string ss)
{
return strcat(str,ss.str);
}
};
void main() {
string s1,s2,s3;
int ch;
cout << "enter the first string\n";
s1.getstr();
cout << "enter second string\n";
s2.getstr();
do { cout << "Menu";
cout << "1.compare two strings\n";
cout << "2.concatinate two strings\n";
cout << "enter your choice\n";
cin >> ch;
switch(ch) {
case 1:if(s1==s2)
cout << "strings are equal\n";
else
cout << "strings are not equal\n";
break;
case 2:s3=s1+s2;
cout << "concatenated string is\n";
s3.display();
break; }
}while(ch==1);
getch(); }

Overloading Assignment Operator

 In the following example, you shall overload the += operator. This operator
combines the addition and assignment into one step.
Example

class distance
{ private : int feet;
float inches;
public : distance () { feet = 0; inches = 0.0;}
distance (int ft, float in) { feet =ft; inches = in; }
void get_dist()
{ cout << "\n Enter feet:"; cin >> feet;
cout << "\n Enter Inches:"; cin >> inches; }
void show_dist()
{ cout << feet << "\'-" <<<="" '\"="" ';="" }="" void="" operator=""
+="(distance);" function="" prototype="" };="" distance="" ::="" d2)=""
declared="" externally="" {="" feet="" inches="" if="" (inches=""> =12.0)
{ inches - = 12.0; feet++; }
}
void main()
{ distance dist1; distance dist2(11,6.25);
dist1 .get_dist(); cout << "\ndistl ="; dist1 .show_dist();
cout << "\ndist2 = "; dist2.showdist();
dist1 += dist2; cout << "\n After addition";
cout << "\ndistl = "; dist1 .showdist();
}

 In the operator += ( ) function in the above example, the object that takes on the
value of the sum is the object of which the function is a member.

Data Conversion

 The = operator will assign a value from one variable to another. In statements like
i1 = i2; where i1 and i2 are integer variables or user defined objects.
 Thus, assignments between types - Basic or userdefined, are handled by the
compiler without any special instructions.

Conversions between Basic Types

 To convert between basic types you use 'casting'. Casting provides explicit
conversion between data types.
 Implicit conversion occurs during evaluation of mixed expressions. Example int
x,y; float z; x = (x + y) / z;
 Since z is a float data type, (x+y) is converted to float implicitly by the compiler.
The result is stored in x by implicit conversion to int.

Conversions between Objects and Basic Types

 The conversion between object and Basic data types needs a separate routine to
define the conversion.
 The following example shows how to convert between a basic data type and a user
defined data type.
Example
//Conversion from Object to Basic type
# include < iostream.h >
#include < conio.h >
const float MTF = 3.280833;
class distance
{
private: int feet; float inches;
public: distance()
{feet = 0; inches = 0.0;}
distance (int ft, float in)
{feet = ft; inches = in;}
distance(float meters)
{ float fltfeet = MTF * meters;
feet = int (fltfeet);
inches = 12 * (fltfeet - feet);
}

void showdist()
{ cout << feet << " feet " << " "<< inches << " inches " ;}
operator float ()
{ float f=inches/12;
f= f+float (feet);
return f/MTF;
}
};
void main() {
distance dist1 = 2.35; //constructor used
clrscr();
cout << "\n\n distl = ";
dist1 .showdist();
distance dist2(5,10.25);
float meters = float(dist2);
//uses conversion function to convert
cout << "\n\n dist2 = " << meters << " meters ";
meters = dist1; //uses Conversion function
cout << "\n\n dist1 = " << meters << " meters ";
getch();
}

From Basic To User-Defined

 The constructor, distance (float meters) converts a basic data type (float) to user
defined data type (distance).
 This function is called when an object of type distance is created with a single
argument.
 The function assumes this argument represents meters.
 It converts the argument to feet and inches, and assigns the resulting values to the
objects data members.

From User-Defined To Basic

 Here, you overload the cast operator, thus creating a conversion function.
 The function in the example which achieves this is the operator float() function.
 This operator takes the value of the distance object of which it is a member,
converts this value to a float value representing meters and returns this value.
 This operator is called by the statement, meters = float (dist2);
 The statement meters = dist1 also has the same effect.
 Here, the compiler starts by looking for an overloaded = operator. But, when it
doesn't find one and it sees the conversion function, it uses that instead.
Conversions between Objects of Different Classes

 Conversions between objects of different classes can be done in the same way as the
conversion between basic and user-defined types. This topic can be studied under
two sections:
 Conversion routine in source object.
 Conversion routine in destination object (Constructor with one argument in
destination object.)

Routine In Source Object

 When the conversion routine is in the source class, it is implemented as a


conversion function.
 Example shows the conversion between class Rec and class polar. Example

//Conversion between objects of different classes: Routine in Source


Object
# include < iostream.h >
# include < math.h >
#include < conio.h >
class rec
{
private:
double xco, yco;
public: rec() { xco = 0.0; yco = 0.0;}
rec(double x, double y)
{ xco = x; yco = y;}
void display()
{ cout << "(" << xco << "," << yco << ")";}
}; // end of rec class declaration
class polar
{
private: double radius, angle;
public:
polar() { radius = 0.0; angle = 0.0; }
polar(double r, double a)
{ radius = r; angle = a;}
void display()
{ cout << "(" << radius << "," << angle << ")";}
operator rec()
{
double x = radius * cos(angle);
double y = radius * sin (angle);
return rec(x,y);
}
}; // end of polar class declaration
void main()
{
clrscr();
rec rec1;
polar pol1 (10.0,0.785398);
rec1= pol1;
cout << "\n pol1 = ";
pol1.display();
cout << "\n recl = ";
rec1.display();
getch();
}

 In the example shown in the previous slide the statement rec1 = pol1; needs
conversion as rec1 and pol1 are objects of two different classes. The conversion
function is written as a member function of the polar class (i.e. source object).

Routine In Destination Object

 In the previous example conversion routine was defined in the source class as its
member function.
 The following example illustrates how to define the routine in the destination class.
 For this, use a constructor with one argument.
Example

//Conversion between objects of two different classes: Routine in


destination object
# include < iostream.h >
# include < math.h >
# include < conio.h >
class polar
{
private:
double radius,angle;
public:
polar()
{ radius = 0.0; angle = 0.0;}
polar (double r, double a)
{ radius = r; angle = a; }
void display()
{ cout << "(" << radius << "," << angle << ")";}
double getr()
{ return radius; }
double geta()
{ return angle; }
};
class rec {
private:
double xco, yco;
public: rec() { xco = 0.0; yco= 0.0; }
rec(double x, double y)
{ xco = x; yco = y; }
void display(){ cout << "(" << xco << "," << yco << ")"; }
rec(polar p)
{
float r = p.getr();
float a = p.geta();
xco = r * cos(a); yco = r * sin(a); }
};
void main()

{
clrscr();
rec rec1;
polar pol1 (10.0,0.785398);
rec1= pol1;
cout << "\n\n pol1 = ";
pol1.display();
cout << "\n\n rec1 = ";
rec1.display();
getch();
}

 The conversion routine is a one-argument constructor, from the rec class rec(polar
p).
 The function sets the object of which it is a member to the rectangular coordinates
that correspond to the polar coordinates of the object received as an argument.
 To perform the conversion, this constructor must be able to access the data values of
the polar object sent as an argument. The polar class contains the two routines to
allow this double getr(); and double geta();.
 The statement rec1 = pol1 in main() of example invokes the one -argument
constructor and the conversion is done.

Precautions While Overloading

 Use similar meaning: Do not overload i+1 operator for subtraction. It can be
confusing to the user.
 Use similar syntax: For better readability and reusability, try to use syntax similar to
the existing usage of the operator. Restrict the number: Too many overloaded
operators causes the listing to be less readable instead of more.
 Avoid ambiguity: Avoid doing the same conversion in more than one way as the
compiler will not know which function to use.
 Not all operators can be overloaded: Operators that cannot be overloaded are:
 :: - Scope resolution operator
 ?: - Conditional operator
 .*- Pointer to member operator
. - Dot operator

Inheritance

 Reusability is an important feature of OOP.


 It is always nice if we could reuse something that already exists rather than trying to
create the same all over again.
 It would not only save time and money but also increases the productivity.
 For instance, the reuse of a class that already been tested, debugged and used many
times can save us the effort of developing and testing the same again.
 C++ supports the concept of reusability.
 The C++ classes can be reused in several ways.
 Once a class has been written and tested, it can be adapted by other programmers to
suit their requirements.
 This is basically done by creating new classes, reusing the properties of the existing
ones.
 The mechanism of deriving a new class from an old class is called inheritance.
 The old class is referred to as the base class and the new one is called the derived
class.

Derived Class and Base Class

 A derived class is like an ordinary class, except that its definition is based on one or
more existing classes, called base classes.
 A derived class can share selected properties (function as well as data members) of
its base classes, but makes no changes to the definition of any of its base classes.
 A derived class can itself be the base class of another derived class.
 The inheritance relationship between the classes of a program is called a class
hierarchy.
 A derived class is also called a subclass, because it becomes a subordinate of the
base class in the hierarchy.
 Similarly, a base class may be called a superclass, because from it many other
classes may be derived.
 Syntax for derive class is
 class derived_c1ass_name: public/private/protected
 base_class_name
 We will define two classes counter and countdown
 1. class counter //base class
 2. { protected: unsigned int count; //Note: not private
 3. public : counter () {count = 0;}
 4. counter (int c) {count = c;}
 5. int getcount() {return count;}
 6. counter operator ++ () { count++; return counter(count); }
 7. };
 8. class countdown : public counter // Derived class
 9. { public: counter operator -- ()
 10. {count--; return counter(count); }
 11. };
 12. void main()
 13. { countdown c1;
 14. cout << "\n c1 =" << c1.getcount()
 15. c1++; c1++; c1++;
 16. cout << "\n c1 = " << c1.getcount();
 17. c1--; c1--;
 18. cout << "\n c1 = " << c1.getcount();
 19. }
 In line no.- 8. A derived class header includes the base classes from which it is
derived. A colon separates the two. Here, counter is specified to be the base class
from which countdown is derived. The keyword public before counter specifies that
counter is used as a public base class.

Private, Public, and Protected Base Class

 A base class may be specified to be private, public, or protected. Unless so


specified, the base class is assumed to be private:
 class A
 {
 private: int x; void Fx (void);
 public: int y; void Fy (void);
 protected: int z; void Fz (void);
 };
 1. class B : A { }; //A is a private base class of B
 2. class C : private A { }; //A is a private base class of C
 3. class D : public A { }; //A is a public base class of D
 4. class E : protected A { }; //A is a protected base class of E

 The behavior of these is as follows for line
 1 & 2: All the public and protected member function of base class A
become private members of the derived class B. So y, Fy, z, and Fz
 all become private members of B and C.
 3: All public members of a public base class become public in
derived class. All protected member of public base class become
protected
 in derived class. So, y and Fy become public members of D, and z
and Fz become protected members of D.
 4: The public and protected members of a protected base class A
become protected members of the derived class. So, y, Fy, z, and Fz
 become protected members of E.
 It is also possible to individually exempt a base class member
from the access changes specified by a derived class, so that it
 retains its original access characteristics. To do this, the
exempted member is fully named in the derived class under its
original
 access characteristic. For example:
 class C : private A {
 //…
 public: A::Fy; //makes Fy a public member of C
 protected: A::z; //makes z a protected member of C
 };

Constructors and Destructors

 Again consider the same two classes: counter & countdown. The counter class has
got two constructor counter () and counter (int c). The countdown class which has
been derived from the counter class has also got two constructor countdown (),
countdown (int c). See the following code
 # include < iostream.h >
 class counter
 { protected: unsigned int count;
 public : counter() { count = 0; }
 counter(int c) { count = c;}
 ……………………………
 };
 class countdown : public counter
 { public: countdown () : counter() { };
 countdown (int c) : counter (c) { };
 ……………………………….
 };
 void main() {
 countdown c1; countdown c2(100);
 cout << "c1 = " << c1 .getcount();
 cout << "c2 =" << c2.getcount();
 c1--; c1++; c1++;
 c2--; c2--;
 countdown c3 = c2--;
 cout << "c1 =" << c1 .getcount() << endl;
 cout << "c2 =" << c2.getcount() << endl;
 cout << "c3 =" << c3.getcount() << endl;
 }
 In this example you can see that the declaration of a constructor in the derived class
is as
 countdown( ): counter( ) {} and countdown(int x ) counter(x) { }.
 Actually the derived class constructors call the base class constructors.
 Any additional statements can be written inside the braces.
 In this example, the derived class constructors do nothing additional, so there are no
statements within the braces.
 Here, the value of count is passed to the derived class constructor which in turn
passes it to the base class constructor which does the actual initialization.
Click here for constructor example

//Conversion from Object to Basic type


# include < iostream.h >
#include < conio.h >
const float MTF = 3.280833;
class distance
{
private: int feet; float inches;
public: distance()
{feet = 0; inches = 0.0;}
distance (int ft, float in)
{feet = ft; inches = in;}
distance(float meters)
{ float fltfeet = MTF * meters;
feet = int (fltfeet);
inches = 12 * (fltfeet - feet);
}

void showdist()
{ cout << feet << " feet " << " "<< inches << " inches " ;}
operator float ()
{ float f=inches/12;
f= f+float (feet);
return f/MTF;
}
};
void main() {
distance dist1 = 2.35; //constructor used
clrscr();
cout << "\n\n distl = ";
dist1 .showdist();
distance dist2(5,10.25);
float meters = float(dist2);
//uses conversion function to convert
cout << "\n\n dist2 = " << meters << " meters ";
meters = dist1; //uses Conversion function
cout << "\n\n dist1 = " << meters << " meters ";
getch();
}

Overriding Member Functions


 The derived class can have member functions, which has the same name as the
member functions of the base class.
 The following example shows the manipulation of data storage object called stack.
 The stack is basically an array of integers.
 The restriction here is that the data is stored and removed from the same end.
Hence, the last data stored is the first to be removed. Hence, you say that it follows
LIFO (Last in first out) logic.
 You use the function push( ) for storing values into the stack and pop( ) to remove
data values from the stack.
 In the example, the base class has two functions - push( ) to store values into the
stack and pop( ) to remove values from the stack. Since the stack is an array of
integers, an erroneous result is obtained if you try to push values into a full stack
(add values to the array after the maximum index) or pop values from an empty
stack (No values have been stored in the stack). Hence top must have values in the
range 0 to MAX.
 But, you do not check the value of top in the base class. Hence, a second class
stack2 is derived from the class stack. So, stack is the base class and stack2 is the
derived class.
Example
 The derived class stack2 has the same functions as the base class.
 When the push() function is called by an object of the derived class, the function
described in the derived class is executed.
 Hence, you say the derived class member functions override the base class member
functions. The range of top is checked in the member function of the derived class
and hence the correct values can be obtained.
 If the value of top is out of range, an error message is given and the program is
terminated.
 Note that the derived class functions call the base class functions through the
statements stack:: push(var) and return stack:: pop();
 The scope resolution operator (::) is used for this purpose. Using this operator
allows us to specify exactly what class the called function is a member of.

 Since a derived class may provide data members on top of those of its base class,
the role of the constructor and destructor is to, respectively, initialize and destroy
these additional members.
 When an object of a derived class is created, the base class constructor is applied to
it first, followed by the derived class constructor.
 When the object is destroyed, the destructor of the derived class is applied first,
followed by the base class destructor.
 In other words, constructors are applied in order of derivation and destructors are
applied in the reverse order. For example, consider a class C derived from B which
is in turn derived from A. Following figure illustrates how an object c of type C is
created and destroyed.
 class A{/*... */};
 class B: public A{/*…*/}
 class C: public B (/* .. */}

Containership

 In Inheritance, if a class B is derived from class A, B has all characteristics of A in


addition to its own.
 Hence you can say that "B is a kind of A". So, inheritance is sometimes called as a
"kind of" relationship.
 There is another kind of relationship called a "has a" relationship, or containership.
In OOP, this relation occurs when you have an object of class A in class B as
shown.
 For example
 class A { };
 class B {A a; // A is contained in B
 };
Click Here For example

//Sample Program to demonstrate Containership


#include < iostream.h >
#include < conio.h >
#include < iomanip.h >
#include< stdio.h >
const int len=80;
class employee
{
private:
char name[len];
int number;
public:
void get_data()
{
cout << "\n Enter employee name: ";
cin >> name;
cout << "\n Enter employee number: ";
cin >> number;
}
void put_data()
{
cout << " \n\n Employee name: " << name;
cout << " \n\n Employee number: " << number;
}
};
class manager
{
private:
char dept[len];
int numemp;
employee emp;
public:
void get_data()
{
emp.get_data();
cout << " \n Enter department: ";
cin >> dept;
cout << "\n Enter number of employees: ";
cin >> numemp;
}
void put_data()
{
emp.put_data();
cout << " \n\n Department: " << dept;
cout << " \n\n Number of employees: " << numemp;
}
};
class scientist
{
private:
int pubs,year;
employee emp;
public:
void get_data()
{
emp.get_data();
cout << " \n Number of publications: ";
cin >> pubs;
cout << " \n Year of publication: ";
cin >> year;
}
void put_data()
{
emp.put_data();
cout << "\n\n Number of publications: " << pubs;
cout << "\n\n Year of publication: "<< year;
}
};
void main()
{
manager m1;
scientist s1;
int ch;
clrscr();
do
{
cout << "\n 1.manager\n 2.scientist\n";
cout << "\n Enter your choice: ";
cin >> ch;
switch(ch)
{
case 1:
cout << "\n Manager data:\n";
m1.get_data();
cout << "\n Manager data:\n";
m1.put_data();
break;
case 2:cout << " \n Scientist data:\n";
s1.get_data();
cout << " \n Scientist data:\n";
s1.put_data();
break;
}
cout << "\n\n To continue Press 1 -> ";
cin >> ch;
}
while(ch==1);
getch();
}

Class Hierarchies

 In all the previous examples, inheritance has been used to add functionality to an
existing class.
 Inheritance can also be used as a part of the original design to a program.
 Then the base class has the highest level in the class hierarchies.
 The classes derived from the base class get the second level of hierarchy.
 The classes derived from the derived classes get the third level and so on.
Click here for example

//sample program for Class Hierarchies


# include < iostream.h >
class employee
{
private: char name[80];
unsigned long number;
public:
void getdata()
{
cout << "\n Enter last name";
cin.getline(name,80);
cout << "\n Enter number"; cin >> number;
}
void dispdata()
{
cout << "\n Name:" << name ;
cout << "\n Number : "<< number; }
}; // class employee ends here
class manager: public employee
{
private: char title[80];
public : void getdata()
{
employee :: getdata( );
cout << "title: "; cin.getline(title,80);
}
void dispdata()
{
employee :: dispdata( );
cout << "\n Title :" << title << endl;
}
};

class scientist: public employee


{
private: int papers;
public : void getdata()
{
employee :: getdata();
cout << "Enter papers published :";
cin >> papers;
}
void dispdata()
{
employee :: dispdata();
cout << "\n Number of publications :" << papers;
}
};
void main()
{
manager m1;
scientist S1;
cout << endl << "Manager"; m1.getdata();
cout << "\nData "; m1.dispdata();
cout << endl << "Scientist ";
s1.getdata();
cout << "\nData ";s1.dispdata();
}

Multiple Level of Inheritance

 Classes can be derived from derived classes also. The following segment illustrates
this.
 class A
 { };
 class B: public A //First level of inheritance
 { };
 class C: public B //Second level of inheritance
 { };
 Hence B is derived from A and C is derived from B. Now, you have two levels of
inheritance.
Click here for Example

//Sample Program to demonstrate Multilevel Inheritance


# include < iostream.h >
# include < string.h >
#include < conio.h >
class student {
private: int id; char name[20];
public: student(){
id = 0; strcpy(name,"\0"); }
student(int i, char *n)
{
id = i;
strcpy(name,n);
}
void getdata()
{
cout << "\n Enter Roll Number: ";
cin >> id;
cout <<"\n Enter Name: ";
cin >> name;
}
void dispdata()
{
cout << "\n Roll Number= " << id << "\n Name= " << name;}

};

class marks: public student {


protected: int mks[3];
public:
marks()
{
mks[0]=mks[1]=mks[2]=0;
}
marks(int i,char *n,int a,int b,int c): student(i,n)
{
mks[0]=a; mks[1]=b; mks[2]=c;
}
void getdata() {
student::getdata();
int i;
for(i=0;i<3;i++)
{
cout << "\n Enter marks for subject " << i+1 <<" -> ";
cin >> mks[i];}
}
void dispdata()
{
student::dispdata();
for(int i=0;i < 3;i++)
{
cout << "\n Marks in subject " << i+1;
cout << " is " << mks[i];}
}
};
class report: public marks
//second level of Inheritance
{
private: int tot;
public: report() {tot=0;}
report(int i,char *n,int a,int b,int c):
marks(i,n,a,b,c) { }
void getdata()
{
marks::getdata();
}
void dispdata()
{
float avg;
tot=mks[0]+mks[1]+mks[2];
avg=tot/3.0;
marks::dispdata();
cout << "\n Total="<< tot;
cout << "\n Average = " << avg << endl;
}
};
void main()
{
clrscr();
report r1(9701,"Poornima",76,87,90);
cout << "\n Student Details are: \n";
r1.dispdata();
r1.getdata();
cout << "\n Student Details are: \n";
r1.dispdata();
getch();
}
 In the above example, the student class is the base class.
 The marks class is derived from student and forms the first level of inheritance.
 The report class is derived from marks class.
 This forms the second level of inheritance as the report class is derived from marks,
which is derived from student.
 The data member mks of marks class is specified as protected because the total is
calculated in the reports class

Multiple Inheritance

 A class can be derived from more than one base class. This is called multiple
inheritance. The syntax is as follows:
 class A { }; //Base Class A
 class B { }; // Base Class B
 class C: public A, public B { };

// C is derived from A and B

 The base classes from which C is derived are listed following the colon on C's
specification separated by commas.
Click here for Example

//Sample Program to demonstrate the usage of Multiple Inheritance


#include < iostream.h >
#include < conio.h >
#include < stdio.h >
class personal
{
protected:
char name[20];
char add[45];
char gender;
public:
void get_info()
{
flushall();
cout << "\n Enter name: ";
gets(name);
cout << "\n Enter address: ";
gets(add);
cout << "\n Enter gender: ";
cin >> gender;
}
void put_info()
{
cout << "\n Entered name: " << name;
cout << "\n Entered address: " << add;
cout << "\n Entered gender: " << gender;
}
};
class physical
{
protected:
int ht,wt;
char bl[2];
public:
void get_detail()
{
cout << "\n Enter height: ";
cin >> ht;
cout << "\n Enter weight: ";
cin >> wt;
cout << "\n Enter blood group: ";
cin >> bl;
}
void put_detail()
{
cout << "\n Entered height: "<<<="" "\n="" entered="" weight:="" "<>
empno;
cout << "\n Enter salary: ";
cin >> sal;
cout << "\n Enter dept: ";
cin >> dept;

}
void disp_details()
{
cout << "\n Entered employee no: " << empno;
cout <<"\n Entered department: " << dept;
cout << "\n Entered salary: " << sal;
}
void increment()
{
long int incr=0;
if(gender=='m')
{
if(dept=='s'||dept=='p')
{
incr=(sal*10)/100;
sal=sal+incr;
cout << "\n Increment in salary: " << incr;
cout << "\n Incremented salary: " << sal;
}
}
else
incr=0;
if(gender=='f')
{
if(dept=='s'||dept=='p')
{
incr=(sal*11)/100;
sal=sal+incr;
cout<<"\n Increment in salary: "<<<"\n="" incremented="" salary:=""
"<<<="" "="" 1.input="" values="" \n="" 2.display="" values\n="" ";=""
enter="" your="" choice:="" flushall();="" scanf("%d",&choice);=""
switch(choice)="" case="" 1:="" s.get_info();="" s.get_detail();=""
s.read_details();="" break;="" 2:="" s.put_info();="" s.put_detail();=""
s.disp_details();="" s.increment();="" "\n="" you="" want="" to=""
continue?press="" (y="" n):="" cin="">> ch;
ch=getchar();
}
while(ch=='y' || ch=='Y');
getch();
}

Ambiguity in Multiple Inheritance

 In multiple Inheritance, when two base class (Say, A & B) have got same functions
name (Say, show()) and an object of derived class (Say C, inherited from A & B)
calls that function, then compiler shows an error showing ambiguous function call.
 This error can be corrected by specifying clearly by writing base class name in the
calling statement.

Pointer & Dynamic memory

 Pointer: A pointer is simply the address of a memory location and provides an


indirect way of accessing data in memory. A pointer variable is defined to 'point to'
data of a specific type. For example
 int *ptr1;//pointer to an int
 char *ptr2;//pointer to char
 The value of a pointer variable is the address to which it points. For example, given
the definitions int num; we can write: ptr1 = #
 The symbol & is the address operator;
 it takes a variable as argument and returns the memory address of that variable. The
effect of the above assignment is that the address of num is assigned to ptr1.
Therefore, we say that ptr1 points to num.
 In general, the type of a pointer must match the type of the data it is set to point to.
 A pointer of type void*, however, will match any type. This is useful for defining
pointers which may point to data of different types, or whose type is originally
unknown.
 A pointer may be cast (type converted) to another type. For example, ptr2 = (char*)
ptr1; converts ptr1 to char pointer before assigning it to ptr2.
 Regardless of its type, a pointer may be assigned the value 0 (called the null
pointer). The null pointer is used for initializing pointers, and for marking the end of
pointer-based data structures (e.g., linked lists).
Example for pointers

#include < iostream.h >


#include < conio.h >
class base{
int i;
public:
void set_i(int num){i=num;}
int get_i(){return i;}
};
class derived:public base{
int j;
public:
void set_j(int num) {j=num;}
int get_j(){return j;}
};
void main()
{
base *bp;
derived d;
clrscr();
bp=&d;
bp->set_i(77);
cout << "\n\n Using the member of base class: " << bp->get_i();
//cout << "\n\n Using the member of derived class: " << bp->get_j(); is
wrong
//bp->set_j(55); this is also wrong as using pointer of base class you
// can not access the members of derived class
getch();
}

Dynamic memory
 In addition to the program stack (which is used for storing global variables and
stack frames for function calls), another memory area, called the heap, is provided.
 The heap is used for dynamically allocating memory blocks during program
execution.
 As a result, it is also called dynamic memory. Similarly, the program stack is also
called static memory.
 Two operators are used for allocating and deallocating memory blocks on the heap.
The new operator takes a type as argument and allocated a memory block for an
object of that type. It returns a pointer to the block.
 For example,
 int *ptr = new int;
 char *str = new char[10];
 allocate, respectively, a block for storing a single integer and a block large enough
for storing an array of 10 characters.
 Memory allocated from the heap does not obey the same scope rules as normal
variables.
 For example, in
 void Foo(void)
 {
 char *str = new char[10];
 //..
 }
 when Foo returns, the local variable str is destroyed, but the memory block pointed
to by str is not. The latter remains allocated until explicitly released by the
programmer.
 The delete operator is used for releasing memory blocks allocated by new. It takes a
pointer as argument and releases the memory block to which it points.
 For example:
 delete ptr; // delete an object
 delete [ ] str; // delete an array of objects
 Note that when the block to be deleted is an array, an additional [ ] should be
included to indicate this.
 1. #include < string.h >
 2. char* CopyOf (const char *str)
 3. {
 4. char *copy = new char[strlen(str) + 1];
 5. strcpy(copy, str);
 6. return copy;
 7. }
 Annotation
1-This is the standard string header file which declares a variety of functions for
manipulating strings.
4 -The strlen function (declared in string.h) counts the characters in its string
argument up to (but excluding) the final null character. Because the null character is
not included in the count, we add 1 to the total and allocate an array of characters of
that size.
5 - The strcpy function (declared in string.h) copies its second argument to its first,
character by character, including the final null character.
New AND Delete Operators in Classes

 The new operator is used in the constructor so that the memory is allocated when
the object is created.
 The delete operator is usually used in the destructor to return the memory to the
operating system when the object is destroyed.
Example

# include < iostream.h >


# include < string.h >
class string
{
private: char *str;
public:
string (char *s)
{
int length = strlen(s);
Char *str = new char[length + 1];
strcpy(str, s);
}
~string()
{ delete str; }
void display()
{ cout << str; }
};
void main() {
string S1 = "Build Bright University";
cout << endl <<"s1 = ";
s1.display();
}

An Array of Pointers to Objects

 A common programming construction is an array of pointers to objects.


 This arrangement is more flexible than placing the objects themselves in an array.
Example

//Sample Program to demonstrate the use of Array and Pointer

# include < iostream.h >


#include < conio.h >
class person
{
protected: char name[40];
public:void setname(void)
{ cout << "\n\n Enter name: ";
cin >> name;
}
void printname(void)
{
cout << "\n Name is : " << name;
}
};
void main(void)
{
person* persptr[100];
int n=0;
char choice;
clrscr();
do {
persptr[n]=new person;
persptr[n]-> setname();
n++;
cout << "\n\n Enter another (y/n)? ";
cin >> choice;
} while(choice=='y');
for(int j=0;j < n;j++)
{
cout << "\n Number : " << j+1;
persptr[j]->printname();
}
getch();
}

 The main ( ) function defines an array, persprt, of 100 pointers to person. In a do


loop it then asks the user to enter a name.
 With this name it creates a person object using new, and stores a pointer to this
object in the array persptr.

Pointers to Derived Types

 In general, a pointer of one type cannot point to an object of a different type.


 However, there is an important exception to this rule that relates only to derived
classes.
 To begin, assume two classes called B and D. Further, assume that D is derived
from the base class B.
 In this situation, a pointer of type B may also point to an object of type D.
 More generally, a base class pointer can also be used as a pointer to an object of any
class derived from that base.
 Although a base class pointer can be used to point to a derived object, the opposite
is not true. A pointer of type D may not point to an object of type B
 Further, although you can use a base pointer to point to a derived object, you can
access only the members of the derived type that were imported from the base.
 That is, you won't be able to access any members added by the derived class. (You
can cast a base pointer into a derived pointer and gain full access to the entire
derived class, however.)
Example
#include < iostream.h >
#include < conio.h >
class base{
int i;
public:
void set_i(int num){i=num;}
int get_i(){return i;}
};
class derived:public base{
int j;
public:
void set_j(int num) {j=num;}
int get_j(){return j;}
};
void main()
{
base *bp;
derived d;
clrscr();
bp=&d;
bp->set_i(77);
cout << "\n\n Using the member of base class: " << bp->get_i();
//cout << "\n\n Using the member of derived class: " << bp->get_j(); is
wrong
//bp->set_j(55); this is also wrong as using pointer of base class you
// can not access the members of derived class
getch();
}

This Pointer

 The 'this' pointer is used as a pointer to the class object instance by the member
function.
 The address of the class instance is passed as an implicit parameter to the member
functions.
 It is a common knowledge that C++ keeps only one copy of each member function
and the data members are allocated memory for all of their instances.
 This kind of various instances of data is maintained by use of this pointer.

Important notes on this pointer

 this pointer stores the address of the class instance, to enable pointer access of the
members to the member functions of the class.
 this pointer is not counted for calculating the size of the object.
 this pointers are not accessible for static member functions.
 this pointers are not modifiable.
Example

# include < iostream.h >


class test
{
int data1;
public:
//function using this pointer
int getdata()
{
return this >> data1;
}
//function without using this pointer
void setdata(int newval)
{
data1=newval;
}
};
void main() {
test t1;
int n;
t1.setdata(17);
n=t1.getdata();
cout << "data1 = " << n;
}

Reference Variable

 A reference is a variable name that is a duplicate of an existing variable. It provides


a technique of creating more than one name to designate the same variable. The
syntax of creating or declaring a reference is:
 Data Type &ReferenceName = VariableName;
 To declare a reference, type the variable's name preceded by the same type as the
variable it is referring to.
 Between the data type and the reference name, type the ampersand operator "&".
 To specify what variable the reference is addressed to, use the assignment operator
"=" followed by the name of the variable.
 The referred to variable must exist already.
 You cannot declare a reference as: int &Mine;

 If you change the value of the variable, the compiler updates the value of the
reference so that both variables would hold the same value. In the same way, you
can modify the value of the reference, which would update the value of the referred
to variable.
 To access the reference, do not use the ampersand operator; just the name of the
reference is sufficient to the compiler.
Example
Passing References to Objects

 We have seen that when an object is passed as an argument to a function, a copy of


that object is made.
 When the function terminates, the copy's destructor is called. If for some reason you
do not want the destructor function to be called, simply pass the object by reference.
 When you pass by reference, no copy of the object is made.
 This means that no object used as a parameter is destroyed when the function
terminates, and the parameters destructor is not called.
Example
 Here is the output of this program: Constructing 1 -10 Destructing 1
 As you can see, only one call is made to C's destructor function.
 Had O been passed by value, a second object would have been created inside neg( ),
and the destructor would have been called a second time when that object was
destroyed at the time neg( ) terminated.
 As the code inside neg( illustrates, when you access a member of a class through a
reference, you use the dot operator.) The arrow operator is reserved for use with
pointers only.
 When passing parameters by reference, remember that changes to the object inside
the function affect the calling object.
 One other point: Passing all but the smallest objects by reference is faster than
passing them by value.
 Arguments are usually passed on the stack. Thus, large objects take a considerable
number of CPU cycles to push onto and pop from the stack.

Polymorphism

 Polymorphism is a major feature of object-oriented programming.


 By using polymorphism, you can create new objects that perform the same
functions as the base object but which perform one or more of these functions in a
different way.
 Polymorphism is supported by C++ both at compile time and at run time.
 Compile-time polymorphism is achieved by overloading functions and operators.
 Run-time polymorphism is accomplished by using inheritance and virtual functions.

Call Mechanism

Whenever a program has a C++ virtual function declared, a v-table is constructed for the
class.

The v-table consists of addresses to the virtual functions for classes and pointers to the
functions from each of the objects of the derived class.

Whenever there is a function call made to the C++ virtual function, the v-table is used to
resolve to the function address.

This is how the Dynamic binding happens during a virtual function call.

Pure Virtual Functions

 A virtual function that is declared but not defined in a base class is referred to as a
pure virtual function. Here is the general syntax for declaring a pure virtual
function: virtual type function_name(parameter_list)=0; virtual void display( )=0;
 Now when the base class uses a PURE virtual function, each descendent must
override that function, or you will get a compile error. This makes sense, because
the function has to be defined somewhere.
 Here's another definition for you: If a class contains a pure virtual function, that
class is called an abstract class.
 Because there is no definition for the function, you cannot create an object of that
class. You can however, declare pointers to it.
 Think of an abstract class as a general class that lays the foundation for descendent
classes that define their own methods. This is the heart of polymorphism - let each
class define its own operation.
Example

//Sample Program to demonstrate the concept of Pure Virtual function


#include < iostream.h >
#include < conio.h >

class A{
virtual void display()=0;
};
class B:public A
{
int j;
public:
void set()
{
j=66;
}
void display()
{
cout << "\n\n j is : " << j;
}
};
class C:public A
{
int m,n;
public:
void set(){m=23;n=33;}
void display()
{
cout << "\n\n m is : " << m << " n is : " << n;
}
};
void main()
{
class B b1;
class C c1;
clrscr();
b1.set();
b1.display();
c1.set();
c1.display();
getch();
}

Unit 4

Templates

 C++ templates provide a way to re-use source code as opposed to inheritance and
composition which provide a way to re-use object code.
 Templates are very useful when implementing generic constructs like vectors,
stacks, lists, queues which can be used with any arbitrary type.
 C++ provides two kinds of templates: function templates and class templates.
 One can use function templates and class templates to write generic functions and
generic class that can be used with arbitrary types.
 For example, one can write searching and sorting routines which can be used with
any arbitrary type.
 For example, a class template for an array class would enable us to create arrays of
various data types such as int array and float array.
 Similarly we can define a template for a function, say mul(), that would help us to
create various versions of mul() for multiplying int, float and double type values.

Example of class which creates an integer array


Class vector{
int *v;
int size;
Public:
vector(int m) { v= new int[size=m];
for (int i=0; i< size; i++) v[i]=0;
……}
Now if we want to create a float array again the class needed to be re-written. But if we use
templates to create an array, the same template can be made use of to create different types
of arrays

Syntax:
template <class T>
class classname
{
//
Different statements
};
Template
class vector
{
T *v; // type T vector
int size;
public:
vector(int m)
{ v=new T[size=m];
for(int i=0; i<="" pre="">

 Class template definition is similar to an ordinary class


definition except that prefix template and the use of type T.

 This prefix tells the compiler that we are going to declare a
template and use T as a type name in the declaration.

 Thus the vector has become the parameterized class with the type T
as its parameter.

 T may be substituted by any data type including the user defined
types.

 Now, we can create vectors for holding different data types.

Example:
vector v1(10);
// 10 element int vector
vector v2(25);
// 25 element float vector

Syntax:
Classname objectname(arglist);

Class Templates to a stack


 As we have just seen, a class template definition looks like a regular class
definition, except it is prefixed by the keyword template.
 For example, the following slide has definition of a class template for a Stack.

Class Templates

 T is a type parameter and it can be any type. For example, Stack<Token>, where
Token is a user defined class. T does not have to be a class type as implied by the
keyword class. For example, Stack<int> and Stack<Message*> are valid
instantiations, even though int and Message* are not "classes".
 While implementing class template member functions, the definitions are prefixed
by the keyword template.
 Using a class template is easy. Create the required classes by plugging in the actual
type for the type parameters.
 This process is commonly known as "Instantiating a class". Here is a sample driver
class that uses the Stack class template.

Output

Pushing elements onto fs


1.1 2.2 3.3 4.4 5.5
Stack Full.
Popping elements from fs
5.5 4.4 3.3 2.2 1.1
Stack Empty
Pushing elements onto is
12345678910
Stack Full
Popping elements from is
10987654321
Stack Empty
Function Templates

 To perform identical operations for each type of data compactly and conveniently,
use function templates.
 You can write a single function template definition.
 Based on the argument types provided in calls to the function, the compiler
automatically instantiates separate object code functions to handle each type of call
appropriately.
 Function templates are implemented like regular functions, except they are prefixed
with the keyword template. Here is a sample with a function template.
 //max function returns the maximum of the two elements
 template <class T>
 T max(T a, T b)
 {
 return a> b ? a : b;
 }
 Using function templates is very easy: just use them like regular functions.
 When the compiler sees an instantiation of the function template, for example: the
call max(10, 15) in function main, the compiler generates a function max(int, int).
Similarly the compiler generates definitions for max(char, char) and max(float,
float) in this case.

Click here for example

Program Output
max(10, 15)= 15
max('k', 's') = s
Max(10.1,15.2) = 15.2
#include < iostream.h >
#include < conio.h >
template < class T >
T max(T a,T b)
{
return a>b?a:b;
}
void main()
{
clrscr();
cout << "\n\n " << "max(10,15)= " << max(10,15) << endl;
cout << "\n\n " << "max('k','s')= " << max('k','s') << endl;
cout << "\n\n " << "max(10.1,15.1)= " << max(10.1,15.1) << endl;

getch ();
}
Parameter values for templates

 Besides the template arguments preceded by the class keywords that represent a
type, function templates and class templates can include other parameters that are
not types whenever they are also constant values, like for example values of
fundamental types.
 As an example see this class template that serves to store arrays:

 It is also possible to set default values for any template parameter just as it is done
with function parameters.
 Some possible template examples seen above:
 template < class T > // The most usual: one class parameter.
 template < class T, class U > //Two class parameters.

 template < class T, int N > //A class and an integer.

 template < class T=char > //With a default value.

 template < int Tfunc (int) > // A function as parameter

Exception Handling

 During the development of a program, there may be some cases where we do not
have the certainty that a piece of the code is going to work right, either because it
accesses resources that do not exist or because it gets out of an expected range, etc...
 These types of situations can be handled by exceptions and C++ provides three
keywords for it try, throw and catch.
 Syntax:
 try {
 //code to be tried
 throw exception;
 }
 catch (type exception)
 {
 //code to be executed in case of exception
}

Operation:

 The code within the try block is executed normally, In case that an exception takes
place, this code must use the throw keyword and a parameter to throw an exception.
The type of the parameter details the exception and can be of any valid type.
 If an exception has taken place, that is to say, if it has executed a throw instruction
within the try block, the catch block is executed receiving as parameter the
exception passed by throw.
Output

Exception: Out of range

 It is also possible to nest try-catch blocks within more external try blocks. In these
cases, we have the possibility that an internal catch block forwards the exception
received to the external level, for that the expression throw; with no arguments is
used. For example:
click here for Example1

// standard exceptions
//Program works in Command prompt of Visual Studio
#include < iostream >
#include < exception >
using namespace std;

class myexception: public exception


{
virtual const char* what() const throw()
{
return "My exception happened";
}
} myex;

int main () {
try
{
throw myex;
}
catch (exception& e)
{
cout << e.what() << endl;
}
return 0;
}
Example2

#include < iostream >


#include < exception >
using namespace std;
void main()
{
int a,b,c;
float d;

cout << "Enter the value of a:";


cin >> a;
cout << "Enter the value of b:";
cin >> b;
cout << "Enter the value of c:";
cin >> c;

try
{
if((a-b)!=0)
{
d=c/(a-b);
cout << "Result is:" << d;
}
else
{
throw(a-b);
}
}

catch(int i)
{
coutb << "Answer is infinite because a-b is:" << i;
}

Exceptions not caught

 If an exception is not caught by any catch statement because there is no catch


statement with a matching type, the special function terminate will be called.
 This function is generally defined so that it terminates the current process
immediately showing an "Abnormal termination" error message.
 Its format is: void terminate( );

Graphics in C++
 Graphics provide a visual way to see objects in action. Turbo C++ graphics
functions fall into two categories:
 those that work in the text mode
 those that work in the graphics mode
 The text mode graphic functions are concerned with placing text in certain areas of
the screen.
 The graphics-mode functions allow you to draw dots, lines, and shapes (like circles,
rectangles, and ellipse etc.), add color to lines and areas, and perform many other
graphics-related activities

Text - Mode Graphics Functions

 The text mode graphics functions need no preparation before use. You can start
using the functions without any prior initialization.
 window ( ) : This function takes four integer arguments that determine the left, top,
right, bottom coordinates of the window. Text written by certain functions will
appear inside the window, starting at the top left corner.
 Text too long for the width is automatically wrapped at the right edge. Text that
goes beyond the bottom of the window causes the contents of the window to scroll
upward. Syntax: window (int left, int top, int right, int bottom);
 This function does not draw a border around the window, it only confines text to a
rectangular area. So, you cannot see the window unless you start typing in, the
default coordinates for this function are the entire screen.
 cputs( ) This function writes a string of text to a window. Syntax: cputs (string);
 clrscr( ) This function erases the text window. When a window is not initialized,
this function can be used to erase the entire screen. Syntax: clrscr( );
 gotoxy( ) This library function positions the cursor within a text window.
 Since text is usually written starting at the cursor position, this allows us to position
text where you want it. Syntax: gotoxy (int x, int y);
 putch( ) This library function displays a single character at the cursor position.
Syntax: putch(char ch);
 Its parameter is the character to be displayed.

Graphics - Mode Graphics Functions

 In graphics mode the basic element is a pixel. Pixel is an abbreviation for picture
element.
 The number of pixels (resolution) on the screen depends on the hardware.
 Each pixel displays a single dot on the screen.
 Any pixel can be suppressed or illuminated.
 Graphics-mode programs use a completely different set of functions from text-mode
programs to put images on the screen.
 A graphics program must also perform some preliminary work to set the appropriate
graphics mode.

Setting up Graphics Mode


 detectgraph() : This function checks the system and returns two integer parameters:
a value representing the system's graphics driver and a value for the recommended
graphics mode if an adapter is installed.
 If a negative driver value is returned, it indicates the absence of a graphics adapter.
 The mode value is the highest resolution possible with that adapter. Syntax:
detectgraph(int *driver, int *mode);
 Example: Illustrates detectgraph Function # include
# include
main()
{
int g_dr, g_mode;
detectgraph(&g_dr,&g_mode);
if (g_dr < 0) cout << "No graphics Adapter\n";
}

 The function detectgraph can be called directly or indirectly. The function given
above makes a direct call to the function. The constant DETECT makes an indirect
call to the function. The above program could be rewritten as

 initgraph (): This library function must be executed before any other graphics mode
functions can be used.
 This requires the GRAPHICS.H header file. Syntax: initgraph(int *driver, int
*mode, char *path);
 where *driver is the graphics driver, given by an integer.
 *mode is the graphics mode, given by an integer.
 *path is a string indicating where the driver is available
 note:The Driver is a file extention BGI.Each type of hardware requires a different
driver.A null string ("") indicates sriver is in the current directory.

Position Functions

 getmaxx() and getmaxy() These functions return the maximum pixels in each row
(number of columns) and each column (number of rows) of the screen. The values
depend on the resolution of the screen. On a 640 X 200 resolution screen, getmaxx()
returns 639 and getmaxy() returns 199. Syntax:
 int getmaxx();
 int getmaxy();
 getx() and gety():The functions getx() and gety() return the current x and y
coordinates. Syntax:
 int getx();
 int gety();

Drawing Shapes

 The functions described below helps us to draw different shapes like line, circle, etc.
on the screen.
 line() This function is used to draw a line, it requires the starting coordinates and the
ending coordinates of the line. Syntax: line(start_col, start_row, end_col, end_row)
 Circle() This function requires three parameters - the x and the y values of the
center and the radius to draws a circle. Syntax: circle(center_col, center_row,
radius);
 Arc().This function draws a circular arc using that specified center, start and end
angles and radius. Syntax: arc(column, row, st_angle, end_angle, radius);
 ellipse() This function draws an ellipse or an elliptical arc using the specified center,
start and end angles and horizontal and vertical radius. Syntax: ellipse(cen_col,
cen_row, st_angle, end_angle, hor_rad, ver_rad);

Miscellaneous Functions

 setcolor() This function sets the current drawing color. Syntax: setcolor(int color);
 Setlinestyle() This sets the line width and style for drawing subsequent lines.
Syntax: setlinestyle(linestyle, upattern, thickness); where linestyle could be any one
of the values in

Values of Linestyle for Setlinestyle function

Constant Value Draws a


Solid_Line 0 solid line
Dotted_Line 1 dotted line
Center_Line 2 center line
DASHED_LINE 3 line of hyphenes
Userbit_Line 4 user defined style

 upattern
Should contain a sequence of on and off bits representing a pattern for drawing the
line. If linestyle is not USERBIT_LINE the value of upattern is ignored.
 Thickness
Could be NORM_WIDTH, value 1 (normal width) or THICK_WIDTH, value 3
(three times as thick).

Filling Enclosed Regions

 Following functions are useful for filling the shapes which have been just discussed.
 floodfill() This function fills a bounded region with colour.
Syntax: floodfill(column, row, border);
 setfillstyle() This function sets the fill pattern and color.
Syntax: setfillstyle(pattern, color);

Values for Patterns of setfillstyle function

Constant Pattern Value


EMPTY_FILL Background Colour 0
SOLID_FILL Foreground Colour 1
LINE_FILL Horizontal Lines 2
LTSLASH_FILL /// Pattern(Thin Lines) 3
SLASH_FILL /// Pattern(Thick Lines) 4
BKSLASH_FILL \\\ Pattern(Thin Lines) 5
LTBKSLASH_FILL \\\ Pattern(Thick Lines) 6
HATCH_FILL Light cross Hatch 7
XHATCH_FILL Heavy cross Hatch 8
INTERLEAVE_FILL Interleaving Lines 9
WIDE_DOT_FILL Sparse Dots 10
CLOSE_DOT_FILL Densely Packed Dots 11
USER_FILL User Defined Pattern 12

Cursor Control

 moveto() This function moves the cursor to the specified location in the graphics
screen.
Syntax: moveto(column, row);
Text Output:

 outtext( )and outtextxy() Both these functions are used to display text on the screen
in the graphics mode. The function outtext() displays the text at current cursor
position while the outtextxy() function positions the cursor and displays the given
text.
Syntax: outtext(text_string);
 Settextstylel() This function is used to specify the font, direction and character size
to be used for subsequent text output in graphics mode.
Syntax: settextstyle(font, direction, char_size);
Where font can be any of the values from Table. All these fonts are from special
files, with extension .CHR, during run time.

Valid Font Values

Constant Value Displays


DEFAULT_FONT 0 Letters in default font
TRIPLEX_FONT 1 Stroked triplex letters
SMALL_FONT 2 Stroked small letters
SANS_SERIF_FONT 3 Stroked sans serif letters
GOTHIC_FONT 4 Stroked gothic letters

 direction can be any of the values from the table.

Values for Direction

Constant Value Displays text


HORIZ_DIR 0 Horizontally, left aligned
VERT_DIR 1 Vertically, right aligned

 char_size
magnifies all stroked letters specified no of times (unless it is 0) i.e. if char_size is
2, magnification is 2 times. Maximum magnification is 10.
 getpixel() and putpixel()
The getpixel() function returns an integer to represent the color of the pixel at the
co-ordinates specified. Syntax: getpixel(column, row);
 Clearing Graphics Screen
cleardevice(): This function clears the graphics screen and positions the cursor at 0,
0.
Syntax cleardevice();
 Ending Graphics Mode
closegraph()
This function must be used when no more graphics routines are to be used.
 It releases the memory reserved by graphics system and restores the screen to the
video mode that was active before initgraph()
Syntax closegraph();

some graphic functions

Here are some graphics programs

graphics in c++
#include < stdio.h >
#include < graphics.h >
#include < stdlib.h >
#include < conio.h >

char *dname[]={"requests detection",


"a CGA",
"an MCGA",
"an EGA",
"a 64K EGA",
"a monochrome EGA",
"an IBM 8514",
"a Herculer monochrome"
"an At&T 6300PC",
"a VGA",
"an IBM 3270 PC"
};
int main()
{
int gdriver,gmode,errorcode;
detectgraph(&gdriver, &gmode);
errorcode=graphresult();
if (errorcode < 0){
printf("Graphic error: %s\n",grapherrormsg(errorcode));
printf("Press any key to halt");
getch();
exit(1);
}
clrscr();
printf("Driver %s :",dname[gdriver]);
printf("Press any key to halt");
// getch();
int gd=DETECT,gm;
initgraph(&gd, &gm,"D:/TC11-11-/BGI");
circle(330,180,100);
getch();
closegraph();
restorecrtmode();
return(0);
}

graphics in c++

#include < iostream.h >


#include < graphics.h >
#include < conio.h >
#include < stdlib.h >
#include < stdio.hn >
void main()
{
int g_dr,g_mode;
int x, y;
g_dr=DETECT;
if(g_dr<0) cout << " No graphics Adapter\n ";
cout << "g_dr: " << g_dr << "g_mode: " << g_mode;
initgraph(&g_dr, &g_mode, "D:/tc11-11-/BGI");
x=getmaxx()/2;
y=getmaxy()/2;
setcolor(RED);
setlinestyle(SOLID_LINE,0,NORM_WIDTH);
rectangle(0,5,635,165);
circle(x,y,100);
setfillstyle(WIDE_DOT_FILL,1);
floodfill(50,50,RED);
setfillstyle(SLASH_FILL,3);
floodfill(x,y,RED);
settextstyle(GOTHIC_FONT,HORIZ_DIR,6);
outtextxy(50,50,"SIEM RIEP");
settextstyle(SANS_SERIF_FONT,VERT_DIR,5);
outtext("MSc-Computer Science");
getch();
cleardevice();
moveto(getmaxx()/2,getmaxy()/2);
settextjustify(CENTER_TEXT,CENTER_TEXT);
setcolor(YELLOW);
outtext("My Frineds ");
getch();
closegraph();
}

Unit 5
Stream

 A Stream is a general name given to a flow of data.


 Each stream is associated with a particular class, which contains member functions
and definitions for dealing with that particular kind of data flow.
 Stream Class Hierarchy: The ios class is the base class. It contains many constants
and member functions common to input and output operations.
 E.g. showpoint and fixed flags used for numeric output. The ios class derives two
classes - istream and ostream.
 The istream class includes all functions for input of data.
 It includes member functions like get(), getline(), read().
 The overloaded extraction operator>> is a member of the istream class.

 The cin object, representing the standard input stream, usually directed from the
keyboard, is a predefined object of the istream_withassign class, which is derived
from the istream class.
 The ostream class includes all functions for output of data.
 It includes member functions like put(), putline() and write().
 The overloaded insertion operator << is a member of the ostream class.
 The cout object, representing the standard output stream, usually directed to the
video display, is a predefined object of the ostream_withassign class, which is
derived from the ostream class.

 The iostream class is derived from both istream and ostream class by multiple
inheritance.
 The iostream_withassign class is derived from iostream class.
 The classes used for input and output to the video display and keyboard are declared
in the header file iostream.h.
 The classes used for disk I/O are included in the file fstream.h.
 fstream.h incorporates iostream.h. Therefore, you need not explicitly open
iostream.h.
 The hierarchy of the classes included in the iostream and fstream classes is shown in
the form of a diagram.

 ASCII files are those files created by storing each character. They can also be called
as text files.
 Binary files are those files created by storing a block of memory (array of structure
or class) . These are also called non-ASCII files.

Manipulating ASCII files

 To transfer a string to the file, you declare an object to be a member of the ofstream
class and initialize it to the filename that you want to use.
 This initialization sets aside various resources for the file and accesses the file of
that name on the disk.
 You can use the insertion operator << to output text to the file.
 As in C, there is no need to open or close the file. This is automatically done by the
constructors and the destructors of the ostream class.
 To see the text in the file you can display it with the DOS TYPE command or open
the file in any editor.

Reading An ASCII String File

 To obtain a string to the file, you declare an object to be a member of the ifstream
class and initialize it to the filename that you want to use.
 To read a string, you can use the istream member function getline(variable, length);
This is illustrated in the example given below.

Character Input /Output

 To manipulate a file, character by character, you use the member functions put() for
output to file or get() for input from file.

Example for Creating A file using put()


Example for reading A file using get()
Detecting End-of-File
 The ifstream objects have values that can be tested for various error conditions.
 If a condition is true, the object returns a zero value, otherwise it returns nonzero
value.
 One of these conditions is the end of file (EOF). The above program checks for the
end of file in a while loop.
Click here for example

#include < fstream.h >


void main()
{
ofstream tstfile("out.dat");
char txt[]="Intellingent Students";
int i=0;
while(txt[i])
tstfile.put(txt[i++]);
tstfile.close();
}
fstream

 Till now, you created an object for either reading from or writing to the file.
 You can also create a file for operating in more than one modes.
 The advantage of using more than one mode is that you can read and write from the
file without having to close and open the file each time in either read or write mode.
 If you want to use the file for both input and output simultaneously, you declare the
a stream to be an object of fstream class.
 To associate the file with the stream, you have to open the file using the open()
member function in the required modes.
 In the open() function you include several mode bits to specify certain aspects of the
file object you are opening. The options are given in the table.

 E.g. open("data.dat",ios::in | ios::out | ios::app);


Click here for example
 This will open the file, data.dat for input or output or append modes. The in and out
modes are included because you want to use the file for both reading and writing.
The app mode is used to retain the previous contents of the file.
 The vertical bar between the flags cause the bits representing these flags to be ORed
together bitwise, so that several flags can apply simultaneously.
 After opening the file, the necessary manipulations can be done. Finally, the file has
to be closed using the close() member function. Click here for example

//Sample Program to demonstrate usage of files


#include < fstream.h >
void main()
{
ofstream outfile("out.txt");
outfile << "Build Bright University" << endl;
outfile << "Siem Riep " << endl;
}

#include < fstream.h >


#include < conio.h >
class student{
private:
int no;
char name[25];
float fee;
public:
void getdata()
{
cout << "\n\n Roll Number : ";
cin >> no;
cout << "\n\n Name : ";
cin >> name;
cout << "\n\n Fees : ";
cin >> fee;
}
void putdata()
{
cout << "\n\n Roll Number : " << no;
cout << "\n\n Name : " << name;
cout << "\n\n Fees : " << fee;

}
};
void main()
{
student s1;
fstream stdfile;
clrscr();
stdfile.open("std.dat",ios::in|ios::binary);
stdfile.read((char *)&s1,sizeof(s1));
//s1.putdata();
while(stdfile)
{
s1.putdata();
stdfile.read((char *)&s1,sizeof(s1));

}
stdfile.close();
getch();
}

Positioning File Pointers

 When you can open a file in more than one mode using the fstream class, it is not
necessary to close the file and open it again when you need to switch from one
mode to the other.
 But if you are writing and reading in different positions of the file then, the stream
pointers have to be positioned appropriately.
 Each file object has associated with it two integer values called the get pointer and
the put pointer.
 These are also called the current get position and the current put position, or simply
the current position.
 These values specify the byte number in the file where reading or writing will take
place.
Functions To Move The File Pointer

 seekg() and seekp()


 seekg() - for get pointer
 seekp() - for put pointer
 These functions take two arguments.
 The first argument is the relative offset i.e. the number of bytes the file pointer has
to be moved. (+ for forward and - for backward.)
 The second argument is the position of the file pointer from where the offset is to be
considered. The default argument for this is the beg (beginning of the file). It can
take values ios::beg (beginning), ios::end (end of file), and ios::cur (current pointer
position).
 .g.: seekg(-5, ios::end); moves the get pointer 5 bytes backward from the end of the
file.

THE tellg() or tellp() FUNCTION

 These functions return the current position of the get or put pointer in bytes.
 The following example illustrates the use of seekg() function. The seekp() function
is also used in a similar manner whenever necessary.
Click here for example

#include < fstream.h >


#include < conio.h >
#include < io.h >
class student{
private:
int no;
char name[25];
float fee;
public:
void getdata()
{
cout << "\n\n Roll Number : " ;
cin >> no;
cout << "\n\n Name : ";
cin >> name;
cout << "\n\n Fees : ";
cin >> fee;
}
void putdata()
{
cout << "\n\n Roll Number : " << no;
cout << "\n\n Name : " << name;
cout << "\n\n Fees : " << fee;

}
};
void main()
{
student s1;
fstream stdfile;
clrscr();
stdfile.open("student.dat",ios::out|ios::in|ios::binary);
char ch;
do
{
s1.getdata();
stdfile.write((char *)&s1,sizeof(s1));
cout<<"\n Continue? y/n : ";
cin>>ch;
} while (ch=='Y'||ch=='y');
clrscr();
stdfile.seekg(0,ios::end); //moves the get pointer to the end of file
int endposition=stdfile.tellg(); //returns current position of get or
put pointer in bytes
int n=endposition/sizeof(s1); //So you will get the total number of
records
cout << "\n\n There are " << n <<" records in the file \n";
int num;
cout << "\n\n Enter the number of the record which want to read : ";
cin >> num;
int position=(num-1)*sizeof(s1);
stdfile.seekg(position);
stdfile.read((char *)&s1,sizeof(s1));
s1.putdata();
getch();
}

Objects That Read And Write Themselves

 Sometimes it makes sense to let each member of a class read and write itself to a
file.
 In this example you can add member functions diskout ( ) and diskin () to the
person class. These functions allow a person object to write itself to a disk and read
itself back in.

I/O Manipulators

 Up till now, we have accepted the default output formatting.


 C++ defines a set of manipulators which are used to modify the state of iostream
objects.
 These control how data is formatted.
 They are defined in the include file, < ios >.
 It is not usually necessary to explicitly include this file because it is included
indirectly via the use of other includes such as < iostream > or < fstream >.
 The manipulators in the above table modify the state of the iostream object. This
means that once used on an iostream object they will affect all subsequent input or
output done with the object.

Manipulators

Setting Output Width:

 setw(w) - sets output or input width to w; requires < iomanip > to be included.
 width(w) - a member function of the iostream classes. Filling White Space:
 setfill(ch) - fills white space in output fields with ch; requires < iomanip > to be
included. fill(ch) is a member function of the iostream classes. Setting Precision:
 setprecision(n) - sets the display of floating point numbers at precision n. This does
not effect the way floating point numbers are handled during calculations in your
program.
Click here for Example

//Sample Program to demonstrate the usage of manipulator


#include < iostream.h >
#include < iomanip.h >
#include < conio.h >
void main()
{
long pop1=2425785,pop2=47,pop3=9761;
clrscr();
cout << setw(10) << "LOCATION" << setw(17) << "POPULATION" << endl << "--
----------------------" << endl;
cout << setw(10) << "portcity" << setw(17) << pop1 << endl << endl;
cout << setw(10) << "Hightown" << setw(17) << pop2 << endl<< endl;
cout << setw(10) << "Newtown" << setw(17) << pop3 << endl;
getch();
}

Redirection in C++

 Redirection is a technique that allows the user considerable flexibility in the way the
programs are used.
 Output from a program is normally to the standard output file and this output can be
redirected to a secondary storage file or to the printer.
 Similarly, input redirection can be used to read data from a secondary storage file
instead of the standard input file, the keyboard.
 The following example implements the DOS TYPE command with the output being
redirected to the printer.
 To implement this using C++, open an object of ostream for printer which is shown
below. Example

//Sample Program to open a file from command line argument and send the
output to the Printer
#include < fstream.h >
#include < process.h >
void main(int argc, char *argv[])
{
if (argc < =1)
{
cout << "\n\n Enter the file name ";
exit (1);
}
ifstream infile(argv[1]);

if (!infile)
{
cout << endl << "Can Not open the file" << argv[1];
exit(1);
}
char ch;

ofstream outfile;
outfile.open("PRN");
while(infile)
{
infile.get(ch);
outfile << ch;
}
outfile.close();
}

Graphics in C Language

In a C program, first step is to initialize the graphics drivers on the computer. This is done
using the initgraph method provided in graphics.h library. In the next few pages we will
discuss graphics.h library in details. Important functions in graphic.h library will be
discussed in details and samples programs will be provided to show the power of C
programming language.

We will restrict our discussion on Graphics in C Language to 16 bit C programming and


MS DOS environment and 640×480 VGA monitor.

Graphics mode Initialization


First of all we have to call the initgraph function that will initialize the graphics mode on
the computer. initigraph has the following prototype.

1 void initgraph(int far *graphdriver, int far *graphmode, char far *pathtodriver);

Initgraph initializes the graphics system by loading the graphics driver from disk (or
validating a registered driver) then putting the system into graphics mode. Initgraph also
resets all graphics settings (color, palette, current position, viewport, etc.) to their defaults,
then resets graphresult to 0.

*graphdriver
Integer that specifies the graphics driver to be used. You can give graphdriver a value using
a constant of the graphics_drivers enumeration type whcih is listed in graphics.h. Normally
we use value as “0” (requests auto-detect). Other values are 1 to 10 and description of each
enumeration type is listed here.

*graphmode
Integer that specifies the initial graphics mode (unless *graphdriver = DETECT). If
*graphdriver = DETECT, initgraph sets *graphmode to the highest resolution available for
the detected driver. You can give *graphmode a value using a constant of the
graphics_modes enumeration type and description of each enumeration type is listed here.

*pathtodriver
Specifies the directory path where initgraph looks for graphics drivers (*.BGI) first.

1. If they’re not there, initgraph looks in the current directory.


2. If pathtodriver is null, the driver files must be in the current directory.

*graphdriver and *graphmode must be set to valid graphics_drivers and graphics_mode


values or you’ll get unpredictable results. (The exception is graphdriver = DETECT.)

After a call to initgraph, *graphdriver is set to the current graphics driver, and *graphmode
is set to the current graphics mode. You can tell initgraph to use a particular graphics driver
and mode, or to auto detect the attached video adapter at run time and pick the
corresponding driver. If you tell initgraph to auto detect, it calls detectgraph to select a
graphics driver and mode.

Normally, initgraph loads a graphics driver by allocating memory for the driver (through
_graphgetmem), then loading the appropriate .BGI file from disk. As an alternative to this
dynamic loading scheme, you can link a graphics driver file (or several of them) directly
into your executable program file.

Here is a simple program that initializes the graphics mode in C programming language and
print the line in graphics mode.

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int main(void)
{

/* request auto detection */


int gdriver = DETECT, gmode, errorcode;
/* initialize graphics mode */
initgraph(&gdriver, &gmode, "");

/* read result of initialization */


errorcode = graphresult();

if (errorcode != grOk) /* an error occurred */


{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* return with error code */
}

/* draw a line */
line(0, 0, getmaxx(), getmaxy());

/* clean up */
getch();
closegraph();
return 0;
}

The below program draws a circle in the current drawing color with its center at (150,150)
and the radius (100) given by radius.

/* Sample program to draw a circle*/


#include<graphics.h>
#include<conio.h>
main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,""); /* initialization of graphic mode */
circle(150,150,100);
getch();
closegraph(); /* Restore orignal screen mode */
}
/* End of program */

Normally the screen which we see in DOS/Command Mode is in the text mode which
means it is meant for text only. And for graphics we need to initialize graphics mode using
initgraph() method defined in graphics.h?.

circle(x coordinate ,y coordinate , radius);


The circle command takes a X coordinate which means Vertical axis and Y coordinate
which means Horizontal axis. And the last one is the radius of the circle.

closegraph();

This function unloads the graphics drivers and returns the screen back to text mode.

/*A program to draw a space with stars*/


#include<graphics.h>
#include<stdio.h>

main()
{
int gd=DETECT,gm;
int i,x,y;
initgraph(&gd,&gm,"");
line(0,0,640,0);
line(0,0,0,480);
line(639,0,639,480);
line(639,479,0,479);
for(i=0;i<=1000;i++)
{
x=rand()%639;
y=rand()%480;
putpixel(x,y,15);
}
getch();
closegraph();
}
/* End of program */

Here a sample program to illustrate how to use BARS which are used for visual statistics.
The bar is filled using the current fill pattern and fill color. Bar method accepts parameters
i.e. left, top, right and bottom. The setfillstyle() method can be used to fill the bar with a
different color or pattern.

#include <graphics.h>
#include <conio.h>

main() {
int gd=DETECT,gm,maxx,maxy,x,y,button;
initgraph(&gd,&gm,"");
line(80,150,200,150);
line(80,150,80,50);
settextstyle(1,HORIZ_DIR,1);
outtextxy(100,153,"<-X axis");
settextstyle(1,VERT_DIR,1);
outtextxy(60,50,"<-Y axis");
bar(100,100,120,150);
bar(130,120,150,150);
getch();
closegraph();
}

Minimum Distance between a Point and a


Line
This article describes the technique and gives the solution to finding the shortest distance
from a point to a line or line segment. The equation of a line defined through two points P1
(x1,y1) and P2 (x2,y2) is

P = P1 + u (P2 – P1)

The point P3 (x3,y3) is closest to the line at the tangent to the line which passes through
P3, that is, the dot product of the tangent and line is 0, thus

(P3 – P) dot (P2 – P1) = 0

Substituting the equation of the line gives

[P3 – P1 – u(P2 – P1)] dot (P2 – P1) = 0

Solving this gives the value of u

Substituting this into the equation of the line gives the point of intersection (x,y) of the
tangent as

x = x1 + u (x2 – x1)
y = y1 + u (y2 – y1)

The distance therefore between the point P3 and the line is the distance between (x,y)
above and P3.

Notes
 The only special testing for a software implementation is to ensure that P1 and P2
are not coincident (denominator in the equation for u is 0)
 If the distance of the point to a line segment is required then it is only necessary to
test that u lies between 0 and 1.
 The solution is similar in higher dimensions.

Source code

//================
//
// DistancePointLine Unit Test
// Copyright (c) 2002, All rights reserved
//
// Damian Coventry
// Tuesday, 16 July 2002
//
// Implementation of theory by Paul Bourke
//
//================
#include <stdio.h>
#include <math.h>
typedef struct tagXYZ
{
float X, Y, Z;
}
XYZ;
float Magnitude( XYZ *Point1, XYZ *Point2 )
{
XYZ Vector;
Vector.X = Point2->X - Point1->X;
Vector.Y = Point2->Y - Point1->Y;
Vector.Z = Point2->Z - Point1->Z;
return (float)sqrt( Vector.X * Vector.X + Vector.Y * Vector.Y + Vector.Z * Vector.Z );
}
int DistancePointLine( XYZ *Point, XYZ *LineStart, XYZ *LineEnd, float *Distance )
{
float LineMag;
float U;
XYZ Intersection;
LineMag = Magnitude( LineEnd, LineStart );
U = ( ( ( Point->X - LineStart->X ) * ( LineEnd->X - LineStart->X ) ) +
( ( Point->Y - LineStart->Y ) * ( LineEnd->Y - LineStart->Y ) ) +
( ( Point->Z - LineStart->Z ) * ( LineEnd->Z - LineStart->Z ) ) ) /
( LineMag * LineMag );

if( U < 0.0f || U > 1.0f )


return 0; // closest point does not fall within the line segment
Intersection.X = LineStart->X + U * ( LineEnd->X - LineStart->X );
Intersection.Y = LineStart->Y + U * ( LineEnd->Y - LineStart->Y );
Intersection.Z = LineStart->Z + U * ( LineEnd->Z - LineStart->Z );
*Distance = Magnitude( Point, &Intersection );
return 1;
}
void main( void )
{
XYZ LineStart, LineEnd, Point;
float Distance;
LineStart.X = 50.0f; LineStart.Y = 80.0f; LineStart.Z = 300.0f;
LineEnd.X = 50.0f; LineEnd.Y = -800.0f; LineEnd.Z = 1000.0f;
Point.X = 20.0f; Point.Y = 1000.0f; Point.Z = 400.0f;
if( DistancePointLine( &Point, &LineStart, &LineEnd, &Distance ) )
printf( "closest point falls within line segment, distance = %f\n", Distance );
else
printf( "closest point does not fall within line segment\n" );
LineStart.X = 0.0f; LineStart.Y = 0.0f; LineStart.Z = 50.0f;
LineEnd.X = 0.0f; LineEnd.Y = 0.0f; LineEnd.Z = -50.0f;
Point.X = 10.0f; Point.Y = 50.0f; Point.Z = 10.0f;
if( DistancePointLine( &Point, &LineStart, &LineEnd, &Distance ) )
printf( "closest point falls within line segment, distance = %f\n", Distance );
else
printf( "closest point does not fall within line segment\n" );
}

The shortest line between two lines in 3D


Two lines in 3 dimensions generally don’t intersect at a point, they may be parallel (no
intersections) or they may be coincident (infinite intersections) but most often only their
projection onto a plane intersect.. When they don’t exactly intersect at a point they can be
connected by a line segment, the shortest line segment is unique and is often considered to
be their intersection in 3D.

The following will show how to compute this shortest line segment that joins two lines in
3D, it will as a biproduct identify parrallel lines. In what follows a line will be defined by
two points lying on it, a point on line “a” defined by points P1 and P2 has an equation

Pa = P1 + mua (P2 –
P1)

similarly a point on a second line “b” defined by points


P4 and P4 will be written as
Pb = P3 + mub
(P4 – P3)

The values of mua and mub range from negative to positive infinity. The line segments between P1
P2 and P3 P4 have their corresponding mu
between 0 and 1.

There are two approaches to finding the shortest line segment between lines “a” and “b”.
The first is to write down the length of the line segment joining the two lines and then find
the minimum. That is, minimize the following

|| Pb – Pa ||2

Substituting the equations of the lines gives

|| P1 – P3 + mua (P2 –
P1) – mub (P4 – P3) ||2

The above can then be expanded out in the (x,y,z) components. There are conditions to be
met at the minimum, the derivative with respect to mua and mub must be zero. Note: it is
easy to convince
oneself that the above function only has one minima and no other minima or maxima.
These two equations can then be solved for mua and mub, the actual intersection points
found by substituting the values of mu into the original equations of the line.

An alternative approach but one that gives the exact same equations is to realize that the
shortest line segment between the two lines will be perpendicular to the two lines. This
allows us to write two equations for the dot product as

(Pa – Pb) dot (P2 – P1) = 0

(Pa – Pb) dot (P4 – P3) = 0

Expanding these given the equation of the lines

( P1 – P3 + mua (P2 –
P1) – mub (P4 – P3) ) dot
(P2 – P1) = 0

( P1 – P3 + mua (P2 –
P1) – mub (P4 – P3) ) dot
(P4 – P3) = 0

Expanding these in terms of the coordinates (x,y,z) is a nightmare but the

result is as follows
d1321 + mua d2121 – mub
d4321 = 0

d1343 + mua d4321 – mub


d4343 = 0

where

dmnop = (xm – xn)(xo –


xp) + (ym – yn)(yo – yp)
+ (zm – zn)(zo – zp)

Note that dmnop = dopmn

Finally, solving for mua gives

mua = ( d1343 d4321 – d1321


d4343 ) / ( d2121 d4343 – d4321
d4321 )

and backsubstituting gives mub

mub = ( d1343 + mua d4321 )


/ d4343

typedef struct {
double x,y,z;
} XYZ;

/*
Calculate the line segment PaPb that is the shortest route between
two lines P1P2 and P3P4. Calculate also the values of mua and mub where
Pa = P1 + mua (P2 - P1)
Pb = P3 + mub (P4 - P3)
Return FALSE if no solution exists.
*/
int LineLineIntersect(
XYZ p1,XYZ p2,XYZ p3,XYZ p4,XYZ *pa,XYZ *pb,
double *mua, double *mub)
{
XYZ p13,p43,p21;
double d1343,d4321,d1321,d4343,d2121;
double numer,denom;

p13.x = p1.x - p3.x;


p13.y = p1.y - p3.y;
p13.z = p1.z - p3.z;
p43.x = p4.x - p3.x;
p43.y = p4.y - p3.y;
p43.z = p4.z - p3.z;
if (ABS(p43.x) < EPS && ABS(p43.y) < EPS && ABS(p43.z) < EPS)
return(FALSE);
p21.x = p2.x - p1.x;
p21.y = p2.y - p1.y;
p21.z = p2.z - p1.z;
if (ABS(p21.x) < EPS && ABS(p21.y) < EPS && ABS(p21.z) < EPS)
return(FALSE);

d1343 = p13.x * p43.x + p13.y * p43.y + p13.z * p43.z;


d4321 = p43.x * p21.x + p43.y * p21.y + p43.z * p21.z;
d1321 = p13.x * p21.x + p13.y * p21.y + p13.z * p21.z;
d4343 = p43.x * p43.x + p43.y * p43.y + p43.z * p43.z;
d2121 = p21.x * p21.x + p21.y * p21.y + p21.z * p21.z;

denom = d2121 * d4343 - d4321 * d4321;


if (ABS(denom) < EPS)
return(FALSE);
numer = d1343 * d4321 - d1321 * d4343;

*mua = numer / denom;


*mub = (d1343 + d4321 * (*mua)) / d4343;

pa->x = p1.x + *mua * p21.x;


pa->y = p1.y + *mua * p21.y;
pa->z = p1.z + *mua * p21.z;
pb->x = p3.x + *mub * p43.x;
pb->y = p3.y + *mub * p43.y;
pb->z = p3.z + *mub * p43.z;

return(TRUE);
}

Graphics Libraray in C/C++


Programming
Graphics library provided by borland C is most widely used library for graphics
programming. Mostly this graphics library is restricted to be used under 16 bit C
programming and MS DOS environment. As discussed earlier that first of all you need to
initialize the graphics drivers on the computer. This is done using the initgraph() method
provided in graphics.h library. graphics.h is used to include the reference to the graphics
library, but actual graphics library can be found in lib directory with the name of
graphics.lib. In Dev C++ there is no default graphics library, but there are some third party
graphics libraries to be used with Dev C++. You can download this library here, after
downloading the library you can read through the manual and copy all the files at their
desired locations. Also you will have to add reference to this graphics library in your
projects or programs.

Adding the graphics.h in your C programs


If you are using borland C/C++ compiler to program, then you will have to follow these
simple steps in ordered to get your C or C++ program running in graphics mode.

 First you will need to add #include reference at the top of you C/C++ program.
 Next step is to initialize the graphics environment. This can be achieved using the
function

void far initgraph(int far *driver, int far *mode, char far *path)

 Here path is the actual path to the graphics library. You can give the path to this
library by giving the complete path, like “C:\\TC\\BGI”, where BGI is the graphics
library directory.
 After initializing the graphics mode you can check for any error which may happen
while initializing the graphics mode. The function used to find out any errors is int
errorcode = graphresult() which returns the error code for the specific error.
 If you pass this errorcode to grapherrormsg() function the, it will return the
complete description of the error message. And if the errorcode==grOk the you are
on the way to develop your first graphics program using C/C++.

If you are using Dev C++ compiler for your graphics programs then you can follow these
simple steps to configure your environment for graphics programming.

Simple graphics functions


I am providing few functions here with some description to make you start with graphics
programming. I have posted the complete description with a demo C/C++ graphics program
at my C and C++ Programming Blog. You can find my blog here, there are three parts of
the posts about graphics.h library. First part of the post discusses the necessary functions to
get the program ready to draw shapes and styles, second post provides description of
necessary functions to draw different shapes and fill them with colors or textures. And the
last part is a sample program to demonstrate that how you can write programs in graphics
mode.

Function circle

Draws the circle on the screen using a point(x,y) and the radius for the circle.
void far circle(int x, int y, int radius)

Function line

This function draws a line in the graphics mode.

void far line(int startx, int starty, int endx, int endy)

Function outtext

Displays a text string on the grpahics screen.

void far outtext(char far *str)

Function outtextxy

Displays the text at a specified position in graphics mode.

void far outtext(int x, int y, char *str)

Function rectangle

Draws a rectangular box on the screen using the points given in the parameters.

void far rectangle(int left, int top, int right, int bottom)

You might also like