Data Structure PDF
Data Structure PDF
using C
DR GAURAV KUMAR
ASST. PROF, CEA, GLA UNIVERSITY
C Programing
Quick Revision
Text Book- 1. Let Us C by Yashavant Kanetkar
2. The C Programming Language by Dennis Ritchie
Reference
#include <stdio.h>
int main()
return 0;
}
Output- Hello, World!
printf("Hello, World!"); printf() is a library function to send formatted output to the screen.
return 0; return 0; statement is the "Exit status" of the program. In simple terms, the
program ends with this statement
}
C programming is a
a general-purpose
procedural
system.
In procedural language, variables and function prototypes must be declared before being used.
01 02 03
Easy to learn Structured language Handle low-level activities
04 05
Produces efficient programs Compiled on a variety of computer platforms
01 02 03 04
Operating Systems Language Compilers Assemblers Text Editors
05 06 07 08 09
Print Spoolers Network Drivers Databases Language Interpreters Modern Programs
output in C language.
printf("enter a number:");
Example
scanf("%d", &number);
int main() {
// calculating sum
return 0;
Each variable in C has a specific type, which determines the size and
underscore character.
type variable_list;
int i, j, k;
Declaration char c, ch;
float f, salary;
a. int a2b;
b. int Abc;
c. int _abc;
d. int 2ab;
Answer is -
"lvalue" expressions.
a = 10;
lvalue rvalue
~Dr Gaurav Kumar, Asst. Prof, CEA, GLA University
Control Structure
Decision Making
If Statement
The if statement in C is used to
perform the operations based on
some specific condition.
If(number %3==0)
{
printf("number is not even number or number is divisible by
3 );
}
if(number%2==0)
{
printf("%d is even number",number);
}
else
{
printf("number is not even number);
}
if (condition)
statement;
else if (condition)
statement;
.
.
else
statement;
nested if statements or
If else-if ladder Statement
int main()
{
if (condition) int i = 20;
statement; if (i == 10)
else if (condition) printf("i is 10");
. printf("i is 15");
. else if (i == 20)
int main()
Syntax
{
int i = 20;
Exp1 ? Exp2 : Exp3;
int main()
{
int i = 20;
if (i == 10)
printf("i is 10");
i==10 ? printf ("i is 10") : printf ("i is 20");
else
printf("i is 20");
Problem Statement:
1. Area of circle
2. Area of square
3. Area of sphere
4. Area of Rectangle
Output??
case 3: printf("Choice is 3");
b. Choice is 2
default: printf("Choice other than 1, 2 and 3");
c. Error
case 2: printf("Choice is 2");
}
Output??
case 3: printf("Choice is 3");
Answer- b
~Dr Gaurav Kumar, Asst. Prof, CEA, GLA University
Switch Expression
Char ch = 'b';
Valid expressions for switch
switch (ch) switch(1+2+23)
{ switch(1*2+3%4)
case a: printf("Choice is a");
01 while loop
02 for loop
03 do...while loop
04 nested loops
Syntax
while(condition)
{
statement(s);
}
value of a: 10
int a = 10;
value of a: 11
value of a: 12
/* while loop execution */ Output value of a: 13
while( a < 20 )
value of a: 14
{ value of a: 15
printf("value of a: %d\n", a); value of a: 16
a++; value of a: 17
} value of a: 18
value of a: 19
Answer- infinite Loop ~Dr Gaurav Kumar, Asst. Prof, CEA, GLA University
Example of while loop
int a=10;
while( a < 20 )
{
printf("This is Data Structure Class\n",);
a++; // a=a+1;
}
Before adding a++, the output will be infinite loop
After adding a++, it will execute for 10 times only.
Syntax
for ( initialization; condition; increment )
{
statement(s);
}
A do...while loop is similar to a while loop, except the fact that it is guaranteed to
execute at least one time.
Syntax
do
{
statement(s);
} while( condition );
do value of a: 12
value of a: 13
{ Output
value of a: 14
printf("value of a: %d\n", a); value of a: 15
a = a + 1; value of a: 16
Syntax
for ( init; condition; increment )
{
for ( init; condition; increment )
{
statement(s);
}
statement(s);
}
~Dr Gaurav Kumar, Asst. Prof, CEA, GLA University
04 nested loops in C
Nested While Nested do...while
while(condition) do
{ {
while(condition) statement(s);
{ do
statement(s); {
statement(s);
}
}while( condition );
statement(s);
}while( condition );
}
Every C program has at least one function, which is main(), and program starts execution
from main.
{ {
Every C program has at least one function, which is main(), and program starts execution
from main.
Example
Task- Maximum of two numbers
Task- Addition of two numbers
int a, b, max;
int a, b, sum;
a=5, b=10;
a=5, b=10;
if(a>b)
sum= a + b;
printf("Max is %d", a);
printf("Sum is: %d", sum);
else
printf("Max is %d", b);
Example Example
int sum(int num1, int num2) display()
{ {
int result; //local variable printf(" I am in display function");
result= num1 + num2; }
return result;
}
optional
A function declaration tells the compiler about a function name and how
to call the function.
A called function performs a defined task and when its return statement is
executed or when its function-ending closing brace is reached, it returns
the program control back to the main program.
They can be used only by statements that are inside that function or
block of code.
int g;
int main () {
Call by value
Call by Reference
Formal Argument
A variable and its type as they appear in the
prototype of the function or method
Actual Argument
The variable appears in the function call in the calling
environment
any changes made to the formal argument doesnt effect the actual
Example argument
int a = 100;
printf( "Number is : %d", a);
printf( "Address of Number is : %d", &a)
/*& is referencing operator*/
printf( "Value of a is : %d", *(&a);
int a=100;
int *p; //pointer to an integer
p=&a;
The above statement will take the 4th element from the array
and assign the value to salary variable.
After the above operation, the ptr will point to the location 1004 (Size of an
integer is 4 bytes)
If ptr points to a character whose address is 1000, then the above operation
will point to location 1001 because the next character will be available at 1001.
Pointer to an array is also known as array pointer. We are using the pointer to
access the components of the array.
Title
Author
Subject
Book ID
member definition;
member definition;
...
member definition;
} [one or more structure variables];
struct Books {
char title[50];
char author[50]; struct Books Book1;
int book_id;
};
malloc()
calloc()
free()
realloc()
~Dr Gaurav Kumar, Asst. Prof, CEA, GLA University
C malloc() method
The “malloc” or “memory allocation” method in C is used to dynamically allocate a
single large block of memory with the specified size.
It returns a pointer of type void which can be cast into a pointer of any form.
It doesn’t Iniatialize memory at execution time so that it has initializes each block with
the default garbage value initially.
Syntax: free(ptr);
Syntax: