CS3353C PROGRAMMING AND DATA STRUCTURES
CS3353C PROGRAMMING AND DATA STRUCTURES
3 0 0 3
COURSE OBJECTIVES:
To introduce the basics of C programming language.
To learn the concepts of advanced features of C.
To understand the concepts of ADTs and linear data structures.
To know the concepts of non-linear data structure and hashing.
To familiarize the concepts of sorting and searching techniques.
1. Introduction
Keywords:
Keywords are the standard words in C.
• These are basically the reserved words which have special meaning. The
meaning of keywords cannot be changed.
• All keywords are written in lower case.
do if static while
Identifiers :
• Identifier is a collection of alphanumeric characters in which the first
character must not be numeric.
Variables
• A variable is nothing but a name given to a storage area that our programs can
manipulate. Each variable in C has a specific type, which determines the size
and layout of the variable's memory; the range of values that can be stored
within that memory; and the set of operations that can be applied to the
variable.
• The name of a variable can be composed of letters, digits, and the underscore
character. It must begin with either a letter or an underscore. Upper and
lowercase letters are distinct because C is case-sensitive. There are following
basic variable types −
Type Description
• 1) The first letter of the variable must not be digit or any special
character.
• 4) The length of the variable name can be any but only first 31
characters are recognized.
• Count,tax_id,INDEX,Xyz,brick01
Constants
Definition of constant: The specific alphabetical or numerical value that
never gets changed during the processing of the instructions is called
as constant.
• The constants are given some names and are referred by the names.
• Example: The most commonly used constant is PI. Once the value to this
constant is assigned then it does not get changed.ohcorl tugi
• Generally all the letters in the name of the constant are capital.
Header Files
The header files contain standard library functions. For using these library
functions directly in the program, it is necessary to include the header file at
the beginning of the program.
stdio.h: It is standard input output header file. In this file the functions
for printf, scanf, fprintf, fscanf are defined. These functions deal with input
and output functions.
conio.h: It is Console Input Output header file. In this file the typical
functions such as clrscr() is defined. By using clrscr(), the console (output)
screen gets cleared.
alloc.h: This header file is used when a a function for allocating the
memory dynamically, such as malloc() is used in the program.
1.1.Data Types
• Data types specify the type of data we enter in our program.
• In C there are some predefined set of data types which are also called
as primitive data types.
(1) integer type: These data types are used to store whole number. (i.e. the
number without fraction).
(3) char type: This data type is used to store the character value.
Example:
void main()
{
int a=5, b=4, c;
c=a-b;
printf(“%d”, c);
}
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.
These operators are used in decision making process. They are generally used in
conditional or control statement.
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).
Operators Descriptions Example Return Value
&& Logical AND 5>3 && 1
5<10
|| Logical OR 8>5 || 8<2 1
!= Logical NOT 8!=8 0
&& - 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)
Program 1.4
/* Program using Logical operators */
#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr( );
printf("\n Condition : Return values ");
printf("\n 5<=8 && 4>2: %5d",5<=8 && 4>2);
printf("\n 5>=3 || 6<8: %5d",5>=3 || 6<8);
printf("\n !(7==7): %5d",!(7==7));
getch( );
}
Output
Condition : Return values
5<=8 && 4>2 : 1
5>=3 || 6<8 : 1
!(7==7) : 0
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;
Program 1.5
/* Program using Assignment and Short-hand Assignment operators */
#include<stdio.h>
#include<conio.h
> void main( )
{
int a=20,b=10,c=15,d=25,e=34,x=5;
clrscr( );
printf("\n Value of a=
%d",a); printf("\n Value of
b=%d",b); a+=x;
Output
Value of a = 20
Value of b = 10
Value of a = 25
Value of b = 5
Value of c = 75
Value of d = 5
Value of e = 4
Increment and Decrement Operators (Unary Operators)
The ‘++’ adds one to the variable and ‘--‘subtracts one from the variable. These
operators are called unary operators.
Operator Meaning
++X Pre increment
--X Pre decrement
X++ Post increment
X-- Post decrement
Pre-increment operator
These operators increment the value of a variable first and then perform
other actions.
Program 1.6
#include <stdio.h>
void main()
{
int a,b;
a=10;
b=++a;
printf(“a=%d”,a);
printf(“b=%d”,b);
}
output: a=11 b=11
#include <stdio.h>
void main()
{
int a,b;
a=10;
b=—a; printf(“a=
%d”,a);
printf(“b=%d”,b);
}
Output:
a=9 b=9
Post-increment operator
This operator perform other actions first and then increment the value of a
variable.
Program 1.7
#include <stdio.h>
void main()
{
int a,b;
a=10;
b=a++;
printf(“a=%d”,a);
printf(“b=%d”,b);
}
Output:
a=11 b=10
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
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.
Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
<< Shift left
>> Shift right
~ One’s complement
a) Comma operator(,):
The comma operator is used to separate the statement elements such as
variables, constants or expression etc.,
This operator is used to link the related expression together.
Such expression can be evaluated from left to right and the value of right most
expression is the value of combined expression.
Example:
val=(a=3,b=9,c=77,a+c);
Where,
First assigns the value 3 to a
Second assigns the value 9 to b
Third assigns the value 77 to c
Last assigns the value 80.
b) The sizeof() operator:
The sizeof() is a unary operator that returns the length in bytes of the specified
variable and it is very useful to find the bytes occupied by the specified variable
in memory.
Syntax:
sizeof(var);
Example:
void main()
{
int a;
printf(“size of variable a is…%d”, sizeof(a));
}
Output:
size of variable a is…….2
c) Pointer operator:
& : This symbol specifies the address of the variable.
* : This symbol specifies the value of the variable.
d) Member selection operator:
. and — >: These symbols are used to access the elements from a structure.
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.
Conditional Branching Statement
Selection Statement
Simple If statement
The syntax for a simple if statement is
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.
Program 1.12
/* 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.
Conditional Expression
The ternary operator is used to form a conditional expression. It uses three
operands and hence it is called as a ternary operator. The syntax for a
conditional expression is:
<expression-1> ? <expression-2> : <expression-3>;
In this method if expression-1 is true then expression-2 is evaluated, otherwise
expression-3 is evaluated.
Fig. 1.9 Flowchart for a Conditional expression
Program 1.14
/* Program to find biggest of two given numbers */
#include<stdio.h>
void main()
{
int x,y,z;
printf(“\n Enter the value of x and y:”);
scanf(“%d%d”,&x,&y);
z = ((x>y)?x:y);
printf(“The biggest value is %d”,z);
getch();
}
Output
Enter the value of x and y: 5 10
The biggest value is 10
If-else-if statement
The syntax for the if-else-if statement is
if(expression1)
{
statements1;
}
else if(expression2)
{
statements2;
}
else if(expression3)
{
statements3;
}
else
{
statements4;
}
In this statement, if the expression1 is true, statements1 will be executed,
otherwise the expression2 is evaluated, if it is true then statements2 is executed,
otherwise the expression3 is evaluated, if it is true then statements3 is executed,
otherwise statements4 is executed.