0% found this document useful (0 votes)
76 views22 pages

Experiment 5: Storage Class 1. Write A Program Which Demonstrates The Use of 4 Storage Classes

The document describes experiments related to data structures in C programming. It includes programs to demonstrate storage classes, structures, unions, typedef, enumeration constants, stacks, and queues using linked lists. The storage class experiment shows the use of auto, register, extern and static storage classes. The structure experiment implements a library management system using structures. The union experiment accepts different data types in a union. The typedef and enumeration experiment defines weekday constants. The stack and queue experiments implement basic push, pop and enqueue, dequeue operations on these data structures.

Uploaded by

Sanjana S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views22 pages

Experiment 5: Storage Class 1. Write A Program Which Demonstrates The Use of 4 Storage Classes

The document describes experiments related to data structures in C programming. It includes programs to demonstrate storage classes, structures, unions, typedef, enumeration constants, stacks, and queues using linked lists. The storage class experiment shows the use of auto, register, extern and static storage classes. The structure experiment implements a library management system using structures. The union experiment accepts different data types in a union. The typedef and enumeration experiment defines weekday constants. The stack and queue experiments implement basic push, pop and enqueue, dequeue operations on these data structures.

Uploaded by

Sanjana S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Roll no: 77 Batch: S4 Class: SY.

CSE

Experiment 5: Storage Class


1. Write a program which demonstrates the use of 4 Storage Classes.
#include <stdio.h>
int x;
void autoStorageClass()
{
int a = 32;
printf("Value of the variable'a'declared as auto:%d\n",a);
printf("--------------------------------\n");
}
void registerStorageClass()
{
register char b = 'G';
printf("Value of the variable 'b' declared as register: %d\n",b);
printf("--------------------------------\n");
}
void externStorageClass()
{
extern int x;
printf("Value of the variable 'x' declared as extern: %d\n",x);
x = 2;
printf("Modified value of 'x' declared as extern: %d\n",x);
printf("--------------------------------\n");
}
void staticStorageClass()
{
int i = 0;
printf("Declaring 'y' as static inside the loop.\n");
printf("\nLoop started:\n");
for (i = 1; i < 5; i++)
{
static int y = 5;
int p = 10;
y++;
p++;
printf("\nThe value of 'y' declared as static in %d "" %d\n",i, y);
printf("The value of non-static variable 'p'in %d is %d\n",i, p);
}
printf("\nLoop ended:\n");
printf("--------------------------------");
}
int main()
{
printf("\n\tDemonstrate Storage Classes\n\n");
autoStorageClass();
registerStorageClass();
Roll no: 77 Batch: S4 Class: SY. CSE

externStorageClass();
staticStorageClass();
printf("\n\nStorage Classes demonstrated");
return 0;
}
Output:-
Roll no: 77 Batch: S4 Class: SY. CSE

Experiment 6: Structures, Union, Typedef, Enumeration Constants


1. Write a program for library management using Structures in C.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct library // Create Structure of Library
{
char book_name[20];
char author[20];
int pages;
float price;
};
int main()
{
struct library lib[100]; // Create a instance
char ar_nm[30], bk_nm[30];
int i, input, count; /*Keep the track of no of books available in the library */
i = input = count = 0;
while (input != 5) {
printf("\n1.Add book information\n2.Display book information\n3.List all books of given
author\n4.List the count of books in the library\n5.Exit");
printf("\nEnter one of the above: "); /*Enter the book details*/
scanf("%d", &input);
switch (input) {
case 1:
printf("Enter book name = ");
scanf("%s", lib[i].book_name);
printf("Enter author name = ");
scanf("%s", lib[i].author);
printf("Enter pages = ");
scanf("%d", &lib[i].pages);
printf("Enter price = ");
scanf("%f", &lib[i].price);
count++;
break;
case 2:
printf("Entered the following information\n");
for (i = 0; i < count; i++)
{
printf("\nbook name = %s",lib[i].book_name);
printf("\nauthor name = %s",lib[i].author);
printf("\npages = %d",lib[i].pages);
printf("\nprice = %f\n",lib[i].price);
} break;
case 3: // Take the author name as input
printf("\nEnter author name :");
Roll no: 77 Batch: S4 Class: SY. CSE

scanf("%s", ar_nm);
for (i = 0; i < count; i++)
{
if (strcmp(ar_nm,lib[i].author)== 0)
printf("\n%4s %4s %4d\t %4f\n",
lib[i].book_name,
lib[i].author,
lib[i].pages,
lib[i].price);
}
break;
case 4: // Print total count
printf("\n No of books in library : %d\n",count);
break;
case 5:
exit(0); } }
return 0;
}

Output:-
Roll no: 77 Batch: S4 Class: SY. CSE

2. Write a program that inputs value of type char, short, int and long and stores the values
in union variables of type union integer.
#include <stdio.h>
#include <conio.h>
union integer
{
char c;
int short s;
int i;
int long b;
};
int main()
{
union integer samp;
printf("\nEnter a value for character: " );
scanf("%c", &samp.c);

printf("short integer: ");


scanf("%hd", &samp.s);

printf("integer: ");
scanf("%d", &samp.i);

printf("long integer: ");


scanf("%ld", &samp.b);

printf("\nCharacter : %c", samp.c);


printf("\nShort integer: %hd", samp.s);
printf("\nInteger : %d", samp.i);
printf("\nLong Integer : %ld",samp.b);

printf("\n");
printf("\nEnter a value for character: " );
scanf("%s", &samp.c);
printf("Character : %c", samp.c);

printf("\n\nshort integer: ");


scanf("%hd", &samp.s);
printf("Short integer: %hd", samp.s);

printf("\n\ninteger: ");
scanf("%d", &samp.i);
printf("Integer : %d", samp.i);

printf("\n\nlong integer: ");


scanf("%ld", &samp.b);
Roll no: 77 Batch: S4 Class: SY. CSE

printf("Long Integer : %ld",samp.b);


printf("\n");
return 0;
}
Output: -
Roll no: 77 Batch: S4 Class: SY. CSE

3. Write a program to demonstrate the use of Typedef, Enumeration Constants.


#include <stdio.h>
typedef enum
{
monday=1,
tuesday,
wednesday,
thursday,
friday,
saturday,
sunday
} WEEKDAY;
int main()
{
WEEKDAY day = sunday;
printf("\nMonday :- %d\n",monday);
printf("Tuesday :- %d\n",tuesday);
printf("Wednesday:- %d\n",wednesday);
printf("Thursday :- %d\n",thursday);
printf("Friday :- %d\n",friday);
printf("Saturday :- %d\n",saturday);
printf("Sunday :- %d\n",sunday);
return 0;
}

Output:-
Roll no: 77 Batch: S4 Class: SY. CSE

Experiment 7: Implementation of STACK


1. Write a program which provides three options: push a value onto the stack (function
push), pop a value off the stack (function pop) and terminate the program.
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push();
void pop();
void display();
int main() {
top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t--------------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
printf("\n\t EXIT POINT ");
break;
default:
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}
}
while(choice!=4);
return 0;
}
void push() {
if(top==4)
printf("\n\tSTACK is over flow");
else
printf(" Enter a value to be pushed:");
scanf("%d",&x);
Roll no: 77 Batch: S4 Class: SY. CSE

top++;
stack[top]=x;
}
void pop() {
if(top==-1)
printf("\n\t Stack is under flow");
else
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
void display() {
if(top==0)
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
else
printf("\n The STACK is empty");
}
Output:
Roll no: 77 Batch: S4 Class: SY. CSE

Experiment 8: Implementation of QUEUE


1. Write a program that creates and manipulates a Queue of with Add & Remove functions.
#include <stdio.h>
# define SIZE 100
void enqueue();
void dequeue();
void show();
int q[SIZE];
int Rear = - 1;
int Front = - 1;
main() {
int ch;
while (1) {
printf("1.Enqueue Operation\n2.Dequeue Operation\n3.Display the Queue\n4.Exit\n");
printf("Enter your choice of operations : ");
scanf("%d", &ch);
switch (ch) {
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
show();
break;
case 4:
exit(1);
default:
printf("Incorrect choice \n");
}
}
}
void enqueue() {
int item;
if (Rear == SIZE - 1) {
printf("Overflow \n"); }
else {
if (Front == - 1)
Front = 0;
printf("Element to be inserted in the Queue\n : ");
scanf("%d", &item);
Rear = Rear + 1;
q[Rear] = item; }
}
Roll no: 77 Batch: S4 Class: SY. CSE

void dequeue() {
if (Front == - 1 || Front > Rear) {
printf("Underflow \n");
return ; }
else {
printf("Element deleted from the Queue: %d\n", q[Front]);
Front = Front + 1; }
}
void show() {
if (Front == - 1) {
printf("Empty Queue \n"); }
else {
printf("Queue: \n");
for (int i = Front; i <= Rear; i++)
printf("%d ", q[i]);
printf("\n");
}
}
Output:-
Roll no: 77 Batch: S4 Class: SY. CSE

Experiment 9: Implementation of LINKED LIST


1. Write a program to demonstrate working of linked list.
#include<stdlib.h>
#include <stdio.h>
void create();
void display();
void insert_begin();
void delete_begin();
struct node {
int info;
struct node *next;
};
struct node *start=NULL;
int main() {
int choice;
while(1) {
printf("\n1.Create \n2.Display \n3.Insert at the beginning \n4.Delete from beginning\n5.Exit");
printf("\nEnter your choice:");
scanf("%d",&choice);
switch(choice) {
case 1:
create();
break;
case 2:
display();
break;
case 3:
insert_begin();
break;
case 4:
delete_begin();
break;
case 5:
exit(0);
break;
default:
printf("\n invalid Choice:\n");
break; }
}
return 0; }
void create() {
struct node *temp,*ptr;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL) {
printf("\nOut of Memory Space:\n");
exit(0);
Roll no: 77 Batch: S4 Class: SY. CSE

}
printf("Enter the data value for the node:");
scanf("%d",&temp->info);
temp->next=NULL;
if(start==NULL) {
start=temp; }
else {
ptr=start;
while(ptr->next!=NULL) {
ptr=ptr->next;
}
ptr->next=temp;
} }
void display() {
struct node *ptr;
if(start==NULL) {
printf("\nList is empty:\n");
return;
}
else {
ptr=start;
printf("[start]=>");
while(ptr!=NULL) {
printf("%d=>",ptr->info );
ptr=ptr->next ;
}
printf("[null]");
} }
void insert_begin() {
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL) {
printf("\nOut of Memory Space:\n");
return;
}
printf("Enter the data value for the node:" );
scanf("%d",&temp->info);
temp->next =NULL;
if(start==NULL) {
start=temp; }
else {
temp->next=start;
start=temp;
}
}
Roll no: 77 Batch: S4 Class: SY. CSE

void delete_begin() {
struct node *ptr;
if(ptr==NULL) {
printf("\nList is Empty:");
return;
}
else {
ptr=start;
start=start->next ;
printf("\nThe deleted element is :%d=>",ptr->info);
free(ptr);
}
printf("[null]");
}

Output:-
Roll no: 77 Batch: S4 Class: SY. CSE

Experiment 10: File I/O


1. Write a program that takes 2 command-line arguments that are file names, reads the
characters from the 1st file one at a time and writes the characters in reverse order to the
2nd file.
#include<stdio.h>
int main()
{
FILE*p;
char ch;
char source[31];
int a=0,b=0,c=0,d=0;
//INPUT FILE NAME
printf("\n Enter name of source file:");
gets(source);
// OPEN SOURCE FILE FOR READING
p=fopen(source,"r");// r for read
// CHECK FOR FAILURE
if(p == 0)
{
printf("\n Cannot open file");
return 1;
}// END IF
// DISPLAY FILE CHAR BY CHAR

ch=fgetc(p);//READ THE FIRST CHAR

while(ch!=EOF) // EOF-> END OF FILE


{
a++;//CHARACTER COUNTER
if(ch>='a'&& ch<='z')
{
b++;// ALPHABETS COUNTER
}
else if(ch>='0' && ch<='9')
{
c++;// DIGITS COUNTER
}
else if(ch == '\n')
{
d++;// LINES COUNTER
}
ch=fgetc(p);// READ NEXT CHAR
}// END WHILE

// DISPLAY ALL COUNTERS


printf("\n number of chars:%d",a);
Roll no: 77 Batch: S4 Class: SY. CSE

printf("\n number of alphabets:%d",b);


printf("\n number of digits:%d",c);
printf("\n number of lines:%d",d);
// CLOSE THE FILE TO SAVE CHANGES/ TO FREE MEMORY
fclose(p);
return 0;
}// end main
/*TO REVERSE CHARCHTER*/
#include<stdio.h>
int main(int argc, char*argv[])
{
FILE*p,*q;
char ch;
char temp[5000];
int i,n;
if(argc!=3)
{
printf("\n Format is: a <source file> <target file>");
return 1;
}// END IF
p=fopen(argv[1],"r");
if(p == 0)
{
printf("\n cannot open source file");
return 1;
}// END IF
q=fopen(argv[2],"w");
if(q==0)
{
printf("\n cannot open target file");
return 1;
}// END IF
// READ FROM SOURCE FILE INTO TEMP ARRAY
i=0;
ch=fgetc(p);
while(ch!=EOF)
{
temp[i]=ch;
i++;
ch=fgetc(p);
}// END WHILE
// WRITE TEMP ARRAY IN REVERSE ORDER INTO TARGET FILE
n=i-1;
for(i=n;i>=0;i--)
{
Roll no: 77 Batch: S4 Class: SY. CSE

fputc(temp [i],q);
}// END FOR I
fclose(p);
fclose(q);
printf("\n File written in reverse order");
return 0;
}// end main

Output:-

For reverse character:


Roll no: 77 Batch: S4 Class: SY. CSE

2. Program that takes a single command-line argument. The arguments should be the name
of a text file containing integers, and the program should sum the integers in the file.
#include<stdio.h>
int main(int argc , char*argv[])
{
FILE*p;
int x,sum;
int result;
if(argc!=2)
{
printf("\n Format is: sumoffile <filename>");
return 1;
}// END IF ELSE
p=fopen(argv[1],"r"); // OPEN FILE FOR READING
if(p == 0) // CHECK USER GIVE THE FILE
{
printf("\n cannot open file");
return 1;
}// END IF
sum=0; // READ INTEGERS FROM THE FILE AND FINDS SUM
while(1){
result=fscanf(p , "%d",&x);
if(result == 0)
{
printf("\n invalid character found");
break;
}// END IF
if(result == EOF)
{
break;
} // END IF
sum=sum+x;
}// END WHILE
printf("\n sum is %d",sum);
fclose(p);
return 0;
}// end main

Output:-
Roll no: 77 Batch: S4 Class: SY. CSE

Experiment 11: Implementation of GRAPH


3. Write a program to represent a Graph by using an Adjacency Matrix method.
#include<stdio.h>
#define V 5
void init(int arr[][V]) { //init matrix to 0
int i,j;
for(i = 0; i < V; i++)
for(j = 0; j < V; j++)
arr[i][j] = 0;
}
void addEdge(int arr[][V],int src, int dest)
{ //Add edge. set arr[src][dest] = 1
arr[src][dest] = 1;
}
void printAdjMatrix(int arr[][V]) {
int i, j;
for(i = 0; i < V; i++)
{
for(j = 0; j < V; j++)
{
printf("%d ", arr[i][j]);
}
printf("\n");
}
}
int main() { //print the adjMatrix
int adjMatrix[V][V];
init(adjMatrix);
addEdge(adjMatrix,0,1);
addEdge(adjMatrix,0,2);
addEdge(adjMatrix,0,3);
addEdge(adjMatrix,1,3);
addEdge(adjMatrix,1,4);
addEdge(adjMatrix,2,3);
addEdge(adjMatrix,3,4);
printAdjMatrix(adjMatrix);
return 0;
}
Output:-
Roll no: 77 Batch: S4 Class: SY. CSE

Experiment 12: Implementation of TREE


1. Write a program to demonstrate Tree traversal in C.
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
typedef struct BST
{
int data;
Struct BST *lchild, *rchild;
} node;

void insert(node *, node *);


void inorder(node *);
void preorder(node *);
void postorder(node *);
int main()
{
int choice;
Node *new_node, *root, *tmp, *parent;
Node *get_node();
Root = NULL;
clrscr();
printf(“\nprogram for binary search tree “);
while(1)
{
printf(“\n1.create”);
printf(“\n2.recursive traversals”);
printf(“\n3.exit”);
printf(“\nenter your choice :”);
scanf(“%d”, &choice);
switch (choice)
{
case 1:
new_node = get_node();
printf(“\nenter the element “);
scanf(“%d”, &new_node->data);
if (root == null) /* tree is not created */
root = new_node;
else
insert(root, new_node);
break;
case 2:
if (root == null)
printf(“tree is not created”);
else {
printf(“\nthe inorder display : “);
Roll no: 77 Batch: S4 Class: SY. CSE

inorder(root);
printf(“\nthe preorder display : “);
preorder (root);
printf(“\nthe postorder display : “);
postorder (root);
}
break;
case 3:
printf(“\t\t\t-----terminated-----\n”);
break;
default:
printf(“you have wrong choice”);
}
}
}
Node *get_node() /*Get new Node */
{
Node *temp;
temp = (node *) malloc(sizeof(node));
temp->lchild = null;
temp->rchild = null;
return temp;
}
void insert(node *root, node *new_node) /* This function is for creating a binary search tree*/
{
if (new_node->data < root->data)
{
if (root->lchild == null)
root->lchild = new_node;
else
insert(root->lchild, new_node);
}
if (new_node->data > root->data)
{
if (root->rchild == null)
root->rchild = new_node;
else
insert(root->rchild, new_node);
}
}
void inorder(node *temp) /*This function displays the tree in inorder fashion */
{
if (temp != null)
{
inorder(temp->lchild);
printf(“%2d”, temp->data);
Roll no: 77 Batch: S4 Class: SY. CSE

inorder(temp->rchild);
}
}
void preorder(node *temp) /* This function displays the tree in preorder fashion */
{
if (temp != null)
{
printf(“%2d”, temp->data);
preorder(temp->lchild);
preorder(temp->rchild);
}
}
void postorder(node *temp) /*This function displays the tree in postorder fashion*/
{
if (temp != null)
{
postorder(temp->lchild);
postorder(temp->rchild);
printf(“%2d”, temp->data);
}
}
Output:

You might also like