Bsc I Year I Sem Notes 2021-22
Bsc I Year I Sem Notes 2021-22
Chapter - 1
Computer Fundamentals
Q). What is computer? what are the characteristics of computer &
limitations of a computer?
Computer:- The term "computer" is derived from the word " compute"
which means to calculate.
computer is a group of electronic devices used for "data processing". Data
processing involves following four steps.
1. Reading data.
2. Storing data
3. Manipulating data
4. Delivering the data in the form of output.
characteristics of computers:-
1. Speed:- Computer processes the data at an un-imaginable speed. The
speed of the computer is in Nano-seconds.
1. Micro computers
2. Mini computers
3. Mainframe computers
4. Super Computers.
classification of computers:
1. Micro Computers: Personal computer is also named as Micro
Computer. Micro computers are comparatively small, Generally these are
found in classrooms, offices & homes. They include desktop, handheld,
Notebook computers.
Q) Anatomy of a Computer?
A computer can accept input, process or store data and produce
output according to a set of instructions which are fed into it.
A computer system can be divided into two components they are
Hardware and Software.
Hardware: Hardware is a physical components of a computer that
includes all mechanical, electrical, electronic and magnetic parts
attached to it. A computer consists of the following major
components:
Input and output devices
Central processing unit
Memory unit
Interface unit.
Software: Software is a set of programs where program is set of
instructions . Software's are 2 types
System software
Application Software.
System Software:
The software that is used for the purpose of a system can be called
as system software. This is mainly used to run the computer and manage
the functionality of the computer. Different types of software's are given
below.
Q) Memory Hierarchy?
Memory unit is the storage area in the computer. Computers can store
very huge amount of the data. Computers can store not only text, but
also images, graphics, videos, audio. All these are stored in the form of
bits (0 or 1). The computer memory is measured in terms of number of
bits.
1. Primary Memory:-
Primary memory is also called as main memory.
Primary memory holds only those data and instructions on which
the computer is currently working.
It is volatile (data gets lost when power is switch off.
2.DRAM: Dynamic RAM has memory cells with a paired transistor and
capacitor. It requires constant refreshing.
2. ROM: 1. ROM stands for Read only memory.
2. The data stored in ROM cannot be changed and it is static.
3. When we switch off the system it will not affect the data in the ROM.
4. ROM contains a special program called "BIOS" (Basic Input Output
System).
5. ROM Memory is used to start up your computer.
6. It is permanently in-built, at the time of its manufacturing.
Types of ROM:
1)PROM: Programmable Read only memory on which data can be written
only once. Once programmed, PROM becomes ROM. PROM is a nonvolatile
memory.
2.EPROM (Erasable Programmable Read only Memory): In EPROM,
the information can be erased and reprogrammed using special PROM. A
EPROM can be programmed many times. But an ultraviolet light is used to
erase the contents of the EPROM.
3.EEPROM ( Electrically Erasable Programmable Read only
Memory): EEPROM is recently developed type of memory. It does not
require ultraviolet light to erase its content. It can be erased by exposing
it to an electrical charge.
Cache Memory: 1. It acts as a buffer between the CPU and main
memory.
2. Cache memory is faster than main memory.
3. It consumes less access time as compared to main memory.
4. When we enter some data, the data first moves into RAM & then CPU. It
is time consuming to collect the data every time from RAM. To avoid this,
Cache memory is included.
Classification of languages:
Computer
Languages
Low Level Language: Low level language is only language which can be
understood by the computer. Binary Language is an example of Low
Level Language. Low Level Language is also known as Machine Language.
The binary language contains only two symbols 1&0. A computer can
directly understand the binary language . CPU directly understands the
binary language instructions, it does not require any translator.
Advantages:
A computer can easily understand low level language.
Low level language instructions are executed directly without any
translator.
Low level language instructions require very less time for their
execution.
Disadvantages:
Low level language instructions are very difficult to use and
understand.
In low level language, there is more chance for errors and it is very
difficult to find errors, debug and modify.
Advantages:
Writing instructions in middle level language is easier than writing
instructions in low level language.
Easy to understand, find errors and modify.
Disadvantages:
Assembly language needs to be translated into low level language.
Assembly language executes slower compared to low level
language.
High Level Language: High level language is a computer language which
can understood by the users. Every high level language have set of
predefined words known as keywords and set of rules known as syntax to
create instructions. We use compiler or interpreter to convert high level
language to low level language.
Advantages:
Writing instructions in high level language is more easier.
High level language is more readable and understandable.
Easy to understand, create programs, find errors and modify.
Disadvantages:
High level language needs to be translated to low level language.
high level language executes slower compared to middle and low
level languages.
Ans) There are three basic phases occurred when we execute any C
program.
Preprocessing
Compiling
Linking
Preprocessing Phase:
A Pre-processors a program that accepts C code with preprocessing
statements and produces a pure form of C code that contains no
preprocessing statements ( like #include).
Compilation Phase:
The compiler accepts a preprocessed output file from the
preprocessor and produces a special file called object file. Object file
contains machine code generated from the program.
Linking Phase:
The link phase is implemented by the linker. The Linker is a process
that accepts as input object files and libraries to produce the final
executable program.
Step 4:- Check Result (Alt +F5): After running the program, the result is
placed into User Screen. We use the shortcut key Alt +F5 to open the
user screen and check the result.
Chapter 3
Algorithms
Q) What is an Algorithm and analysis of an Algorithm?
Algorithm: A step by step method of solving a problem or
making decisions. It is used for solving certain mathematical
problems.
An algorithm is a finite set of well-defined instructions for
accomplishing some task. Which given an initial state. Will
terminate in a defined end state.
Characteristics of Algorithm:
1. Input: Every algorithm must take zero or more number of
input values from the external.
2. Output: Every algorithm must produce an output as
result.
3. Definiteness: Each step of an algorithm must be
precisely defined; the actions to be carried out must be
rigorously and unambiguously specified for each case .
4. Finiteness: For all different cases the algorithm must
produce result within finite number of steps.
5. Effectiveness: Every instruction must be basic enough to
be carried out and also must be feasible.
DIFFERENT WAYS OF STATING ALGORITHMS:
Two popular tools used the representation of algorithms
1. Pseudo Code
2. Flowchart
3. Step form
Pseudo Code: is a method of describing computer
algorithms using a combination of natural language and
programming language.
Flow Chart: are written with program flow from the top of a
page to the bottom. Each command is placed in a box of the
appropriate shape and arrows are used to flow of program.
STEP-FORM ALGORITHM:
In the step form representation, the procedure of solving a problem
is stated with written statements. Each statement solves a part of
problem and these together completes the solution. The step-form
uses just normal language to define each procedure. Every
statement , that defines an action, is logically related preceding
statement.
Q) Strategy for Designing Algorithm?
Now that the meaning of algorithm and data has been
understood, strategies can be devised for designing algorithms.
Investigation step:
Identify the output needed.
Identify the input variables available.
Identify the major decisions and conditions.
Chapter - 4
Basics of C
Ans) Before 1960’s COBOL, FORTRAN languages are used COBOL for
commercial application. FORTRAN for Engineering & scientific
applications. At this stage people started thinking that instead of learning
& using so many languages. Each for a different purpose why not use only
one language which can program. All possible applications then
international committee come out with one language called ALGOL-60 but
it never became popular because it is too abstract & too general CPL was
introduced. C language was developed by “ Dennis Ritche” in 1972 by
introducing the features of B & BCPL adding some more features in
1972 at AT & T’s BEEL Laboratory of USA.
History of C:
Year Language Purpose
1960 ALGOL Special purpose
1963 CPL Special Purpose
1967 BCPL Special Purpose
1970 B Special Purpose
1972 C All Purpose
Example:
#include<stdio.h>
main()
{
printf("Hellow world");
getch();
return();
}
Output:
Hello world
void main(): This is the line from where the execution of program starts.
The main function starts the execution of any C program.
return 0: This command terminates the c program and returns the null
value, that is 0.
Documentation section
Link section
Definition section
Global Declaration
Main()
{
Declaration part;
Executable Part;
}
Sub Program Section
{
Statements
}
Link Section:-
The link section provides instruction the compiler to link the library
functions from library to the main() function. Linking is a process of
putting together the library files and functions that are required by the
program.
Ex:- If the program is using “exp” then the object code of the program
should be brought from the system given by <math.h> and should be
linked to the main program.
#include<stdio.h> instructs the compiler to search for a file “stdio.h”
from the system library and the contents of the header becomes the part
of source file.
#include<stdio.h>
#define pi 3.14
Main()
{
……….;
……….;
}
immediately following the main function indicates that the function main()
has no arguments or parameters.
The main function contains two parts
1. Declaration pat.
2. Execution part.
Declaration part:-
The declaration part declares all the variables used in the
executable part and there must be at least one statement in the
executable part. These two parts must appear between an opening brace
and should end with a closing brace. Hence the program execution begins
at the opening brace and ends with a closing brace.
Ex:-
#include<stdio.h> link section
#define pi 3.14 definition section
Main() main section
{
int a; declaration part
float b;
: executable part
}
Identifier:-
Constants/Literals:
Constant is a value that can be stored in the memory and cannot be
changed during the execution of the program. There are three types of
constants.
constants
Q) What is variable?
A variable is a user defined word it has some storage area
depends on data type and it’s value will be changed during the
program execution.
The declaration of variable is
Data type variable1,variable2,……,variable;
Rules:
1. Integer Variable:-
Rules of integer variable
a) It should be the value.
b) It can’t accept any decimal values and special character.
c) The integer variable can occupies 2 bytes of memory location.
d) The range is -32,768 to +32,767.
e) It can take automatically positive sign.
DATATYPES
CHARACTER UNIONS
3.Derived data type:- When created derived data type then that derived
data types derived in memory location. The derived data type are arrays,
functions, pointers .
Array: Set of related data items is called Array. It can share a common
name. The Array variable start from 0th location in memory. The array
each cell can be created be separate address value.
Function: A Function is reusable block of statements, which is used to
perform a specific task. Generally C program is divided into basic building
blocks called C functions. C function contains set of instructions enclosed
by " { }" which performs specific operations in c program.
Pointer: A pointer is a variable. It contains the memory address of the
another variable and is declared as the pointer type.
4.Empty data type:- which data type are created by empty space in
memory location that data types are called empty data type.
Ans) Which is operate any two operands that type of symbol is called
operator.
The operators are used in programs to manipulate the data throw the
variables.
The following operators are provided in c language.
1. Arithmetic operator.
2. Relational operator.
3. Logical operator
4. Assignment operator
5. Increment and decrement operator.
6. Conditional operator.
7. Bitwise operator.
8. Special operator.
Symbol Meaning
&& AND
|| OR
! NOT
When two conditions are combined by logical AND then the resultant
condition becomes true if both conditions are true, otherwise the condition
is false.
When the two conditions are combined by the logical operator OR then
the result condition is true if any one of the condition is true otherwise the
resultant condition is false.
4.Assignment operators:-
An assignment operators is used to assign a value or
expression to a variable. The most commonly used assignment operator
is “=”.
Syntax:-
Variable=Value(Or) Expression;
Ex:-
int i=5;
float a=5.5;
int c=a+b;
C contains the following assignment operators +=,-=,*=,/=,%= etc,
6.Conditional operators:-
It is also called ternary (?:) . Because it uses three expressions.
Syntax:-
(exp1 ? exp2 : exp3);
exp1 is evaluated first; if exp1 is true then exp2 is evaluated other
wise it evaluate exp3.
7.Bitwise Operators:-
C supports some special operators known as bitwise operators for
manipulation of data at bit level. The bitwise operators are
Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
<< Left Shift
>> Right Shift
~ One’s
Complement
8. Special Operators:-
C supports some special operators such as comma operator, size of
operator, pointer operator(& and * ) and member selection operator(, and
->).
Ans) If the expressions has more than one operator has it must be
necessary to give the precedence to each operator has the facility for
giving the precedence to each operator. Each operator precedence and
associativity of each operator are given below.
+ Addition
- Subtraction 4 Left to right
Ans) C has the facility to mix the different type of variables in expression.
In this type of operation one data type is converted into another data
type.
Unit - II
Chapter - 5
Input - Output Functions
Ex:-
1. int a,b;
scanf(“%d%d”,&a,&B);
2. float x,y;
char p;
scanf(“%f%f%c”,&x,&y,&p);
3. char name[10];
scanf(“%s”,name);
Output Function:
The output functions are useful for us to obtain the results from the
computers. The user can get the results on the standard output device
VDU & later stored on disk .
printf() function:-
This function is used to display the formatted output data items on the
standard output device. The general form is
Printf(“control string”, argument list);
The control list is the user defined formatted codes and this may
also be a string constant . The argument list contains the set of data items
to display the proper format defined by the control string.
Ex:- 1. printf(“ PC SOFTWARE & C “);
2.printf(“%d%d%d%f”,a,b,c,area);
1.getch() function
2.getchar() function
3.gets() function
getch() Function:-
This is a function used as input statement to read only alpha
numeric character from the standard input device say keyboard.
Syntax:- getch();
Each time when this function is called it returns the next input character.
Ex:- d=getch();
The character accepted from keyboard assigned to variable “d”.
getchar() function:-
Single character can be entered into the computer using ‘C’ library
function getchar(). It returns a single character from a standard input
device the function does not required any arguments.
Syntax:- character variable=getchar();
Ex:- char c;
C=getchar();
gets():-
The gets () function is used to read a string from the keyboard until
a carriage return key or new line key is pressed. At the end the function
appends a “null” terminator.
Syntax:-
gets (variable name);
Ex:-
gets(s);
gets(name);
Where name is the array storing the string. It is necessary to define name
as a array of characters.
1.putch() function
2.putchar() function
3.puts() function
Putch() function:-
Putchar():-
This function used to display any alphabetic character. The format
of putchar function is
Syntax:
putchar(variable name);
Ex:-
answer=’y’;
putchar(answer);
Puts():-
The function puts() outputs the string to the standard output device.
Syntax:-
Puts(s);
Ex:-
main()
{
char line[80];
gets(line);
puts(line);
}
Chapter -6
Control Structures
Q) what are the control structures in “c” ?
Ans) Control structures are 3 types
1. sequential statements
2. conditional statements
3. Control statement (looping statements)
1.Sequential statements:-
The sequential statements will be executed one by one in the
given program.
2.Conditional statements:-
1. simple if statement.
2. if… else statement
3. if else …else if statement
4. nested if statement.
5. switch statement.
1. Simple if statement:-
General form:-
If (condition) if(condition)
Statement; {
Statement1;
(OR) Statement2;
. block
.
.
Statement n;
}
Flow Chart of IF
true
Test Condition
block
false
Next statement
If .. else statement:-
The general form is
If(condition) if(condition)
Statement; (OR) {
else Statement 1;
statement; : block 1
Statement n;
}
Else
{
Statement 1;
: block2
Statement n;
}
entry
false True
Test Condition
Block 2 Block1
Next statement
if( condition 1)
statement 1;
else if( condition 2)
statement 2;
else if( condition 3)
statement 3;
:
:
else
statement n;
(OR)
if(condition 1)
block1;
else if(condition 2)
block 2;
else if(condition 3)
block 3;
:
:
else
block n;
In this form if the condition 1 is true block 1 is executed. If the
condition 2 is true block 2 is executed and on when all the conditions
are false default is executed. A block may contain single or group of
statements are executed with in the braces.
(OR)
if (condition 1)
{
if (condition 2)
{
statement 1;
: block1
:
statement n;
}
else
{
statement 1;
: block2
:
statement n;
}
}
else
{
statement 1;
:
: block 3
statement n;
}
Switch statement: -
General form is
switch (expression)
{
case constant 1: statement;
break;
case constant2: statement;
break;
:
:
:
case constant n: statement;
break;
default: statement;
}
The switch statements is multi way conditional statement .where
expression is an integer expression or character type expression and
constant1, constant2….constant n are the constants or constant
expression called case labels.
If the switch statement is executed , the value of the
expression is successively compared against the values of
constant1,constant2,…constant n. If a case is found whose value
matches with the constant of the expression the block of statement
that follows the ‘case ‘ are executed.
While(condition)
{
Statement 1;
: body of the loop
:
Statement n;
}
Do..While Statement:-
The general form is
do
{
Statement 1;
: body of the loop
:
Statement n;
} while(condition);
For loop: For loop is another entry control loop . The general form of
for statement is
For(initialization; condition; increment/decrement)
{
Body of the loop;
}
The execution of for statements the initialization of control value is
done first. The value of control variable is tested by using test condition. If
the condition is true then the body of the loop will be executed then the
value of control variable is incremented. This new value again tested
using the test condition if it is true again the body of loop is executed. If
the condition is false control comes out of the loop. The for statement
allowed –ve increment.
Ex:- for(i=0;i>=0;i--)
Printf(“%d”,i);
Ex:
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
Count:
printf("%d\n",i);
i++;
if(i<10)
{
goto Count;
}
getch();
}
Continue:-The continue statement works somewhat like break
statement. But instead of forcing termination, continue forces the next
iteration of the loop to take place, skipping any code in between. For
example the following routine displays only positive numbers.
continue
Then the loop are nested, then the break would only exit from
the loop containing it, i.e. the break will exit form that loop. The given
example shows the use of break statement in a switch statement.
yes
break Outside
loop
Ex:-
switch(choice)
{
case ‘r’: printf(“red \n”);
break;
case ‘w’: printf(“white \n”);
break;
}
3. In while loop first condition is 3.In do-while loop first the body of
tested. the loop is executed and then
condition is tested.
4.If the condition is true then 4.If the condition is true again the
the body of the loop will be body of the loop will be executed.
executed
5.The condition is false control 5. If the condition false control
transfers to the statement transfers to the statement followed
followed by the while by do-while statement.
statement.
6.while(test expression) 6.do
{ {
Statement; Statement;
Statement; Statement;
………….; ………….;
} }while(test expression);
7. No semi colon following the 7. Semi colon following the while
while statement. statement at the bottom.
Chapter - 7
ARRAYS & STRINGS
Ans) Set of related data items is called Array. It can share a common
name. The Array variable start from 0th location in memory. The array
each cell can be created be separate address value.
One Dimensional array:- A list of items can be given one variable name
using only one subscript such a variable is called a single dimensional
array.
Syntax:-
Data type array name[array size];
Ex:- Char name[10];
Here name is one dimensional character array containing 10 elements
these are name[0],name[1],…,name[9];
General form:-
Data type array name[size]={list of values};
Compiler sees the character string, it ends the string with additional null
character.
We can initialize or read the values to an array by using for loop.
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
General form:-
Data type array name[row size][column size];
Ex:- int a[3][4];
Multidimensional arrays:-
Initialization of a string:-
Char name[5]={‘r’,’a’,’m’,’a’};
(or)
Char name[5]={“rama”};
/* Write a program to read and display any string from the key board */
#include<stdio.h>
Void main()
{
Char s[10];
Clrscr();
Printf(“enter any string \n”);l
Scanf(“%s”,s);
Getch();
}
‘C’ library supports a large no. of string handling functions that can be
used to carry out many of the string manipulations. We are using the
string handling functions in our program we must define <string.h> as the
header file most commonly used string handling functions are as follows.
General form:-
Strcat(string1,string2);
General Form:-
Strcpy(string1,string2);
General form:-
Strcmp(string1,stirng2)==0)
General form:-
Strlen(string1);
General form:-Strrev(string1);
Example Program:
#include<stdio.h>
#include<conio.h>
void main()
{
char s1[20],s2[20];
intl;
clrscr();
printf("enter any 2 strings \n");
scanf("%s%s",s1,s2);
l=strlen(s1);
strcat(s1,s2);
printf("Length of string s1=%d\n",s1);
printf("After concatenation s1=%s\n",s1);
strrev(s1);
printf("Reversing the string s1=%s",s1);
getch();
}
This function is used to make an upper case character into the lower case
character. This function takes a character in the form of parameter as a
character and returns a character. The character which will return lower-
case
Example Program:
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void amin()
{
char a,b,c,d,e,f,g,h,i,j;
a='A';
b='B';
c='C';
d='D';
e='E';
f=tolower(a);
g=tolower(b);
h=tolower(c);
i=tolower(d);
j=tolower(e);
printf("%c is %c \n",a,f);
printf("%c is %c \n",b,g);
printf("%c is %c \n",c,h);
printf("%c is %c \n",d,i);
printf("%c is %c \n",e,j);
getch();
}
UNIT - III
Chapter -8
FUNCTIONS
Advantages of functions:-
C functions to avoid rewriting same logic / code again and again in a
program.
There is no limit in calling c functions to make use of same
functionality whenever required.
We can call functions any number of times in a program and from
any place in a program
The use of user defined functions allows a large program to be
broken down into a number of smaller self contained components.
Program debugging is easier, if C Program contain functions.
Once a function is called and debugged it can be generalized, tested
thoroughly and can be placed into a library so that it can be used
again and again by many programs.
It is easy to locate and isolate a function.
In C, all the system defined functions are defined inside the header
files like stdio.h, conio.h, math.h, string.h etc. For example the functions
printf(), Scanf() are defined in the header file called stdio.h. We must
include the respective header file using #include statements.
If we use a system defined functions like pow(), sqrt() in our
program we use math.h in our program.
Function Call:
This is the statement where the function is called and actually used . A
function can be called from main() function, other function in the program
or in the same function itself. A function can be called any number of
times in the program. When a function is called, the control of the
General form:-
return type Function name(arguments list)
argument declaration;
{
Local variable declaration;
function statements;
return statement;
}
The first line of function definition contains the type specification of the
value return by the function followed by the function name and a set of
arguments. An empty pair of parenthesis must follow the name of the
function definition does not include any arguments.
Ex:
#include<stdio.h>
int main()
{
int x,y,c;
int sum(int,int); // function prototype
clrscr();
printf("enter the x,y values \n");
scanf("%d%d", &x,&y);
c=sum(x,y); // function call
printf("Result c=%d", c);
getch();
}
int sum(int a, int b) // function definition
{
return(a+b);
}
main()
{
---------------;
function1();
}
function1()
{
--------------;
}
Example Program:-
#include<stdio.h>
void main()
{
void sum();
printf(“\n sum of 2 numbers\n”);
sum();
}
void sum()
{
int a,b;
printf(“enter any 2 numbers \n”);
scanf(“%d%d”,&a,&b);
printf(“sum=%d”,a+b);
}
back any value. The data communication between calling function and
called function with arguments but no return value.
main()
{
int x;
--------------;
function(x);
}
function(a)
int a;
{
--------------;
}
One way communication between function.
Example Program:-
#include<stdio.h>
void main()
{
int a,b;
void sum(int,int);
printf(“\n enter any 2 numbers\n”);
scanf(“%d%d”,&a,&b);
sum(a,b);
}
void sum(int x,int y)
{
printf(“sum=%d”,x+y);
}
main()
{
---------------;
function1();
}
function1()
{
--------------;
return(value);
}
Ex:-
#include<stdio.h>
void main()
{
int c;
int sum();
c=sum();
printf(“sum=%d”,c);
}
int sum()
{
int a.b;
printf(“\n enter any 2 numbers\n”);
scnaf(“%d%d”,&a,&b);
c=a+b;
return ( c );
}
ex:-
#include<stdio.h>
void main()
{
int a,b,c;
int sum(int ,int);
printf(“\n enter any 2 numbers\n”);
scanf(“%d%d”,&a,&b);
c=sum(a,b);
printf(“sum=%d”,c);
}
void sum(int a,int b)
{
return ( a+b );
}
EX:-
#include<stdio.h>
void main()
{
int a,b;
void sum(int,int);
printf(“\n enter any 2 numbers\n”);
scnaf(“%d%d”,&a,&b);
sum(a,b); //a,b are the actual parameters
}
void sum(int x,int y) //x,y are the formal parameters
{
printf(“sum=%d”,x+y);
}
Ans)
CALL-BY-VALUE:- When a function is called the Actual parameters are
assigned to formal parameters. The process of passing the actual value of
variables is known as call by value. In call by value if there is any change
occurs in the called function it cannot be effected to the calling function.
(i.e. if there is any change occurs in the formal parameters it cannot be
effected to the actual parameters.)
Example program
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
clrscr();
printf(“enter any 2 numbers\n”);
scanf(“%d%d”,&x,&y);
printf(“Before swapping x=%d\t y=%d\n”,x,y);
swap(x,y);
printf(“After swapping x=%d\t y=%d\n”,x,y);
}
swap(int a,int b)
{
int t;
t=a;
a=b;
b=t;
printf(“In the function swapping a=%d \t b=%d\n”,a,b);
}
Output:
Enter any 2 numbers
10
20
Before swapping x=10 y=20
In the function swapping a=20 b=10
After swapping x=10 y=20
The values a and b are not changed even though the are swapped.
Because the parameter passing is call by value.
CALL BY REFERENCE:
When a function is called the Actual parameters are assigned to formal
parameter. The process of calling the function using pointers to pass the
address of variable is known as call by reference. In call by reference if
there is any change occurs in the called function it can be effected to the
Example program:
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
clrscr();
printf(“enter any 2 numbers\n”);
scanf(“%d%d”,&x,&y);
printf(“Before swapping x=%d\t y=%d\n”,x,y);
swap(&x,&y);
printf(“After swapping x=%d\t y=%d\n”,x,y);
}
swap(int *a,int *b)
{
int t;
t=*a;
*a=*b;
*b=t;
printf(“In the function swapping a=%d \t b=%d\n”,*a,*b);
}
Output:
Enter any 2 numbers
10
20
Before swapping x=10 y=20
In the function swapping a=20 b=10
After swapping x=20 y=10
The values of a and b are changed because the parameter passing is call
by reference.
Q) Inline Functions?
Ans) c99 has added the keyword inline, which applies to functions. By
preceding a function declaration with inline, the compiler is instructed to
optimize calls to the function. Typically , this means that functions code
will be expanded inline, rather than called. Below is a definition of such
inline function.
inline int sum(int x, int y)
{
return(x+y);
}
The inline designation is only a hint to the compiler, suggesting that calls
to the inline function should be as fast as possible. The name comes from
a compiler optimization called inline expansion, where by a call to
function is replaced by copy of the function body.
Ex:-
#include<stdio.h>
#include<conio.h>
void main()
{
long int n,f;
long int fact(int);
clrscr();
printf(“enter any number \n”);
scanf(“%d”,&n);
f=fact(n);
printf(“factorial of given number =%ld \n”,f);
getch();
}
long int fact (long int n)
{
int f1=1,i;
for(i=0;i<=n;i++)
f1=f1*i;
return(f1);
}
1.Automatic
2.Static
3.Register
4.Extern
Automatic Storage class:- The local variables which are declared inside
the function are called automatic variable. All the variables declared
without any storage class specifier are assumed to be automatic variable.
The scope of the variable is inside the function in which they are declared.
We can use the same automatic variable in different function because
after the execution of the function, all automatic variables are disposed
off. The Keyword used for automatic storage class is auto.
#include<stdio.h>
void main()
{
int no;
printf(“enter the number for table”);
for(i=1;i<=10;i++)
func(no);
}
func(int no)
{
auto int step=0;
step=step+no;
printf(“%d\n”,step);
}
Output:-
4
4
4
4
4
4
4
4
4
4
#include<stdio.h>
void main()
{
int no;
printf(“enter the number for table”);
for(i=1;i<=10;i++)
func(no);
}
func(int no)
{
auto int step=0;
step=step+no;
printf(“%d\n”,step);
}
Output:-
4
8
12
16
20
24
28
32
36
40
Register Storage Class:- Register variable are used for storing the
value of variable into CPU registers. Access time of automatic variable is
more than accessing the register variable. Cpu register take less time &
for fast processing. The Keyword used for Register storage class is
“Register”.
#include<stdio.h>
Main()
{
Register int I;
For(i=0;i<200;i++)
Printf(“%d \n”,i);
}
External Storage Class:- The variables which are used in many
functions can be declared as external variable. The scope of the variable
is global. These variables are available to all functions in the program.
These are declared before the main() function. The Keyword used for
external storage class is extern.
Syntax:-
Extern datatype variable_name;
#include<stdio.h>
int no,i;
main()
{
void fact(int);
clrscr();
printf(“enter the number \n”);
scanf(“%d”,&no);
fact(no);
}
void fact(int no)
{
int fact=1;
while(no>1)
{
fact=fact*no;
no--;
}
printf(“factorial of %d =%d \n”,no,fact);
}
Ex:
#include<stdio.h>
#include<conio.h>
void main( )
{
float avg, age[ ]={ 23.4,55,22.6,3,40.5,18};
float average(float age[]);
clrscr();
avg=average(age);
printf("Average age=%2f", avg);
getch();
}
float average(float age[ ])
{
int j;
float avg,sum=0.0;
for(i=0;i<6;i++)
{
sum=sum+age[i];
}
avg=sum/6;
return avg;
}
Chapter -9
POINTERS
Q)What is pointer? How it can defined explain about the pointer in
‘c’language?
A) Pointer:-
int a=5;
int *p;
p=&a;
int *p;
p=&a;
then
*p=5;
Advantages of pointers:-
Pointer reduces the length and complexity of a program.
Pointers are more efficient in handling the data tables.
Pointers increase the execution speed.
The use of a pointer array to character strings results in saving of
data storage space in memory.
Pointer reduce the length and complexity of a program.
Pointer is used for saving memory space.
The Two dimensional and multi dimensional array represented is
easy with pointers.
Pointer data manipulation is done with address, so the execution
time is faster.
a Variable name
are allocated to the array elements. The base address of array is the
address of the 0th element of the array. The name of array also gives the
base address of the array.
Ex:-
int arr[4]={5,10,15,20};
int *A;
A=arr;
Or
A=&arr[0]
Every element of this array is a pointer variable. It is same as array
but it contains the collection of address.
Ex:-
#include<Stdio.h>
Void main()
{
int *arr[3];
int a=5,b=6,c=7,I;
arr[0]=&a;
arr[1]=&b;
arr[2]=&c;
for(i=0;i<3;i++)
{
printf(“address=%u\t”,arr[i]);
printf(“value =%d \n”,*(arr[i]);
}
}
Pointers to Strings:-
A character array occupies consecutive locations. As we know that
at the end of the string a null character (\0) stores in the character array.
name can be used like a pointer, it can be treated as string pointer
array.
Ex:
#include<stdio.h>
#include<conio.h>
void main()
{
char *name[]={"Govt", "College"};
int i;
for(i=0;i<2;i++)
printf("%s \n", name[i]);
}
Pointer to Pointer:-
Pointer is a variable that contains the address of the another variable.
Similarly another pointer variable can store the address of this pointer
variable. Hence we can say this is a pointer to pointer variable.
Ex:-
#include<stdio.h>
Void main()
{
Int a=5;
Int *b;
Int **C;
B=&a;
c=&b;
Printf(“Value of a=%d\n”,a);
Printf(“Value of a=%d\n”,*b);
Printf(“Value of a=%d\n”,*(&a));
Printf(“Value of a=%d\n”,**c);
Printf(“Address of a=%u\n”,b);
Printf(“Address of a=%u\n”,&a);
Printf(“Address of a=%u\n”,*c);
Printf(“Address of b=%u\n”,c);
}
Size of():-
Size of is an unary operator this operator gives the size of its
argument in terms of bytes. The argument can be variable, array or any
data type. This operator also gives the size of any structure.
Ex:-
Size of (int);
This gives the bytes occupied by the int data type, that is 2.
Ex:-
/* program to understand the size of operator*/
# include<stdio.h>
main()
{
struct
{
char name[10];
int age;
float sal;
}rec;
int arr[10];
printf(“size of structure =%d”, size of(rec));
printf(“size of int = %d”, size of(int));
printf(“size of array =%d”, size of (char));
}
Free():--
For efficient use of memory space we can also release the memory
space that is not required. We can use the free functions for releasing the
memory space.
EX:- free(ptr);
Realloc():- There is a two possibility for that we want to change the size
of the block. In first case, we want more memory space in comparison to
allocated memory space.
In second case, the allocated memory is more than the required memory
space for changing the size of memory block we can use the function
realloc this is known as reallocation of memory.
Ptr = malloc (specified space);
The statement allocate the memory of the specified size and the base
address of memory block are stored in pointer variable ptr.
If we want to change the size of memory block
Ptr = recalloc (ptr , new size);
CHAPTER -10
USER DEFINED DATA TYPES
STRUCTURES & UNIONS
Ex:- consider a book consisting of fields: name, author, pages and cost.
Then a structure to hold the information is as follows
struct class
{
char title[20];
char author[10];
int pages;
float cost;
};
struct book
{
char title[20];
char author[10];
int pages;
float cost;
}b1,b2, b3;
Accessing a structure:-
The link between a member and a variable is established
using the member operator, which is also known as ‘dot’ or ‘period’
operator.
Ex:-
book1.pages=250;
book 1.cost=75.50;
We can also use scanf() function to give values through the keyboard.
Struct rec
{
Char name [10];
Int age;
Char add[20];
} person [10];
{
Char name[20];
Int age;
Struct dob
{
Int day;
Int month;
Int year;
}birthday;
Char address[20];
} emp;
Here struct rec is the structure for the employee, record and struct dob
represent the birth date of that employee.
UNIONS
Union unionname
{
Member 1;
Member 2;
------
-------
Member n;
}variable name;
Ex:-
union emp
{
Int age;
Char code[4];
}e1;
Structures Unions
Enum enum-tag
{
Member 1;
Member 2;
…………..;
Member n;
};
Where enum is a keyword and member 1, member 2……… is integer
identifier this can also be defined as
Enum enum-tag
{
Member 1;
Member 2;
} variable;
Here variable is an enumeration variable. This variables can also
be defined as
Enum enum-tag variable;
Ex:-- enum month(jan, feb, mar, apr, may, june);