C PROGRAMMING AND DATA STRUCTURE
UNIT-I C PROGRAMMING FUNDAMENTALS
Data Types -Variables -Operations -Expressions and Statements - Conditional
Statements - Functions - Recursive Functions - Arrays -Single and Multi-
Dimensional Arrays.
UNIT II C PROGRAMMING - ADVANCED FEATURES
Structures - Union - Enumerated Data Types - Pointers: Pointers to Variables,
Arrays and Functions - File Handling - Pre-processor Directives.
UNIT III LINEAR DATA STRUCTURES
Abstract Data Types (ADTs) - List ADT - Array-Based Implementation -
Linked List - Doubly- Linked Lists - Circular Linked List - Stack ADT -
Implementation of Stack -Applications - Queue ADT -Priority Queues - Queue
Implementation - Applications.
UNIT IV NON-LINEAR DATA STRUCTURES
Trees - Binary Trees -Tree Traversals - Expression Trees - Binary Search Tree -
Hashing - Hash Functions - Separate Chaining - Open Addressing - Linear
Probing- Quadratic Probing - Double Hashing -Rehashing.
UNIT V SORTING AND SEARCHING TECHNIQUES
Insertion Sort - Quick Sort -Heap Sort - Merge Sort -Linear Search - Binary
Search.
UNIT-I C PROGRAMMING FUNDAMENTALS
Data Types -Variables -Operations -Expressions and Statements - Conditional
Statements - Functions - Recursive Functions - Arrays -Single and Multi-
Dimensional Arrays.
Data Types
The C language supports different types of data. Each data may be
represented differently within the computer memory. Typical memory
requirements for each data will determine the possible range of values
for that data type.
The varieties of data types available allow the programmer to select the
type appropriate to the needs of the application as well as the machine.
C supports three categories of data types:
1. Primary data type
2. Derived data type
3. User defined data type.
Data types
Primitive data type Derived data type User defined data type
Integer Arrays Enumerated
Float Function Structures
Character Pointer Unions
Typedef
Primary Data Types
C compiler supports the following four Fundamental/ Primary/
Primitive/ Basic/ Built-in data types:
o Character: Character data type is used to store a character.
A variable of character data type allocated only one byte of
memory and can store only one character. Keyword char is
used to declare variables of type character. The range of
character (char) data type is -128 to 127.
For Example: char ch = ‘A’;
o Integer: Integer data type is used to store a value of
numeric type. Keyword int is used to declare variables of
integer type. The memory size of a variable of integer data
type is dependent on the operating system. For example the
size of integer data type in a 32 bit computer is 4 bytes
whereas size of integer data type in 16 bit computer is 2
bytes.
For Example: int count = 10;
o Float: Floating point data type is used to store a value of
decimal values. The memory size of a variable of floating
point data type is dependent on the Operating System.
Keyword float is used to declare variables of floating data
type. For example the size of a floating point data type in a
16 bit computer is 4 bytes.
For Example: float rate = 5.6;
o Double: Double data type is similar to floating data type
except that it provides up to ten digits of precision and
occupies eight bytes of memory.
For Example: double d = 11676.2435676542;
Derived data types
Derived data types are derived from the collection of primary data
types. C supports the following derived data types.
Arrays
o Array is a collection of variables of same data type that are
referenced by a common name.
o Syntax: <Datatype><Variable Name>[index];
Example: int a[10];
Functions
o A function is a self-contained program segment (block of
statements) that carries out some specific, well defined
task. Syntax for function prototype
<return datatype>function name (forma) arg,
formal arg2 ... formal arg n);
Syntax for function definition
<return datatype>function name (parameter list)
parameter declarations
{
body of the function;
return (expression);
}
Example
void swap (int x, int y)
{ int z;
z = x;
x = y;
y = z;
}
Pointers
o Pointer is a variable which stores the address of another
variable.
Example
int *p; // declaration of pointer
int x; // declaration of variable
p=&x; // pointer variable stores the address of x variable.
x=5 ; // x variable assigned with value 5
User defined data types
The user defined data types enable a program to invent his own data
types and define what values it can taken on. Thus these data types
can help a programmer to reducing programming errors.
C supports the following user defined data types.
o Structures A structure is a single entity representing a
collection of data items of different data types.
Example
struct student
{
int roll_no;
char fname[25];
char branch[15];
int marks;
}s1;
o Unions A union is a data type in ‘c’ which allows overlay
of more than one variable in the same memory area.
Example
union emp
{
char name[20];
char eno[10];
} union emp e1;
o Enumerated data type A enumerated data type is a set of
values represented by identifiers called enumeration
constants. It is a user-defined data type and the general
format of this data type is
enum name {number 1, number 2, ... number n};
In above format enum is a keyword, name is given by the
programmer by the identifier rules. number 1, number 2, ...
number n are the member of enumerated datatype.
o Type definition “type definition” that allows user to
define an identifier that would represent a data type using
an existing data type.
Syntax:
typedef type identifier;
typedef<existing_datatype><new_user_defined_datatype>
Example
typedef int number;
typedef long big_ number;
typedef float decimal;
number visitors = 25;
big_number population = 12500000;
decimal radius = 3.5
Variables and Declaration
Variables are identifiers whose value changes during the execution of the
program. Variables specify the name and type information. The compiler
allocates memory for a particular variable based on the type.
Variables can be modified using the variable name or address of the
variable. The variable name must be chosen in a meaningful way. The
declaration of the variable must be done before it can be used in the
program.
The general syntax of the variable declaration is given below.
datatype : var1, var2, ….,varn;
where datatype : may be any data type
var1, var2 : variable name separated by a comma
Examples
1. int sum, count;
2. int rollno;
3. float int_rate;
4. double avg, netsal;
5. char char;
Variable Initialization
Assigning a relevant value to a variable for the first time in a program is
known as initialization. Sometimes a variable may be initialized on its
declaration itself. Variables can be initialized with a constant value or
expression.
Syntax:
datatype variablename = expression; (or) datatype variablename = constant;
Example
1. int c = 10, d = c + 5;
2. float rate = 12.5;
3. char ch = ‘Y’;
4. int count = 0 , sum = 0;
5. float pi = 3.14;
CONSTANT AND VOLATILE VARIABLE
Constant variable
If we want that the value of a certain variable remain the same or remain
unchanged during the execution of a program, then it can be done only by
declaring the variable as a constant.
The keyword constant is then added before the declaration. It tells the
compiler that the variable is a constant. Thus, constant declared variables
are protected from modification.
Example
const int a = 20;
where, const is a keyword, a is a variable name and 20 is a constant value. The
compiler protects the value of ‘a’ from modification. The user cannot assign any
value to a; by scanf ( ) statement the value can be replaced.
Volatile variable
The volatile variables are those variables that are changed at any time by
any other external program or the same program. The syntax is as
follows.
Example
volatile int b;
where volatile is a keyword and b is a variable If the value of a variable in the
current program is to be maintained constant and desired not to be changed by
any other external operation, then the declaration of the variable will be as
follows;
volatile const b = 20;
OPERATORS IN C
Operator: An operator is a symbol that specifies an operation to be performed
on operands. Eg: x= a+b; where + is an operator.
Operands: An operand is an entity on which an operation is to be performed.
An operand can be a variable name, a constant, a function call or a macro name.
Eg. x= a+b; where x, a, b are the operands.
Expression: An expression is a sequence of operands and operators that
specifies the computations of a value. An expression is made up of one or more
operands. Eg. x= a+b.
Types of Operators
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Increment and Decrement Operators
6. Conditional Operators (Ternary Operators)
7. Bitwise Operators
8. Special Operators
1.Arithmetic Operators
Addition, subtraction, multiplication, division and modulo are the
arithmetic operations.
The arithmetic operators are used for numerical calculations between two
Constants
table
Example:
void main()
int a=5, b=4, c;
c=a-b;
printf(“%d”, c);
2.Relational Operators
Relational operators are used to compare two or more operands.
Operands may be variable, constant or expression
Table
Syntax
AE1 operator AE2
where, AE- Arithmetic Expression or Variable or Value.
These operators provide the relationship between two expressions.
If the condition is true it returns a value 1, otherwise it returns 0.
.
3. Logical Operators
Logical Operators are used to combine the result of two or more
conditions.
The logical relationship between the two expressions is checked with
logical operators.
After checking the condition, it provides logical true (1) or false (0)
table
&& - This operator is usually used in situation where two or more
expressions must be true.
Syntax:
(exp1) && (exp2)
|| – This is used in situation, where at least one expression is true.
Syntax:
(exp1) || (exp2)
! – This operator reverses the value of the expression it operates on.
(i.e.,) it makes a true expression false and false expression true.
Syntax:
!(exp1)
4.Assignment Operator
Assignment Operator are used to assign constant or a value of a variable
or an expression to another variable.
Syntax
variable =expression (or) value;
Example
x=10;
x=a+b;
x=y;
5.Increment and Decrement Operators (Unary Operators)
The ‘++’ adds one to the variable and ‘--‘subtracts one from the variable.
These operators are called unary operators.
table
6.Conditional Operator (or) Ternary Operator
Conditional operator checks the condition itself and executes the
statement depending on the condition.
Syntax
condition?exp1:exp2;
Example
void main()
{
int a=5,b=3,big;
big=a>b?a:b;
printf(“big is…%d”,big);
}
Output
big is…5
7.Bitwise Operators
Bitwise operators are used to manipulate the data at bit level.
It operates on integers only.
It may not be applied to float or real.
table
8.The Special Operator
C language supports some of the special operators given below
table
EXPRESSIONS AND STATEMENTS
Expressions
An expression represents data item such as variables, constants and are
interconnected with operators as per the syntax of the language.
An expression is evaluated using assignment operators.
Syntax
Variable = expression;
Example: 1
x=a*b-c;
In example 1, the expression evaluated from left to right. After the
evaluation of the expression the final value is assigned to the variable
from right to left.
Example: 2
a++;
In example 2, the value of variable a is incremented by 1, i.e, this
expression is equivalent to a = a + 1.
Statements
A statement is an instruction given to the computer to perform an action.
There are three different types of statements in C:
1. Expression Statements
2. Compound Statements
3. Control Statements
Expression Statement
An expression statement or simple statement consists of an expression
followed by a semicolon (;).
Example
a=100;
b=20;
c=a/b;
Compound Statement
A compound statement also called a block, consists of several individual
statements enclosed within a pair of braces { }.
Example
{
a=3;
b=10;
c=a+b;
}
Control Statement
A single statement or a block of statements can be executed depending
upon a condition using control statements like if, if-else, etc.
Example
a=10;
if (a>5)
{
b= a+10;
}
CONDITIONAL STATEMENTS
The conditional statement requires the programmer to specify one or
more conditions to be evaluated or tested by the program, along with a
statement or statements to be executed if the condition is determined to be
true, and optionally, other statements to be executed if the condition is
determined to be false.
In a conditional statement, the flow of execution may be transferred from
one part to another part based on the output of the conditional test carried
out. It has been further classified into selective and loop constructs.
In a selective constructs, the statements are selected for execution based
on the output of the conditional test given by an expression. It supports
the following constructs such as
1.Simple if
2.if-else
3.ifelse-if
4.nested-if and
5.switch case statement.
In loop constructs, the block of statements will be executed repeatedly
until the condition is true else the loop will be terminated. It supports the
following constructs such as
1.For
2.While and
3.Dowhile loops.
Simple If statement
Syntax
if (expression)
{
block of statements;
}
In this statement, if the expression is true, the block of statements are
executed otherwise false and it comes out of the if condition.
diagram
/*Program to find the given number is divisible by 2 */
#include<stdio.h>
void main()
{
int n;
printf(“\n Enter the number”);
scanf(“%d”,&n);
if(n%2==0)
{
printf(“\n The given number%d is divisible by 2", n);
}
getch();
}
Output
Enter the number : 10
The given number 10 is divisible by 2
If –else statement
syntax
if(expression)
{
block of statements1;
}
else
{
block of statements2;
}
diagram
In this statement, if the expression is true the block of statements1 will be
executed, otherwise the block of statements2 will be executed.
/* Program to find the given number is positive or negative */
#include<stdio.h>
void main()
{
int n;
printf(“\n Enter the number:”);
scanf(“%d”,&n);
if(n>0)
{
printf(“\n The given number %d is positive”, n);
}
else
{
printf(“\n The given number %d is negative”, n);
}
}
Output
Enter the number : 5
The given number 5 is positive