Experiment 5: Storage Class 1. Write A Program Which Demonstrates The Use of 4 Storage Classes
Experiment 5: Storage Class 1. Write A Program Which Demonstrates The Use of 4 Storage Classes
CSE
externStorageClass();
staticStorageClass();
printf("\n\nStorage Classes demonstrated");
return 0;
}
Output:-
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("integer: ");
scanf("%d", &samp.i);
printf("\n");
printf("\nEnter a value for character: " );
scanf("%s", &samp.c);
printf("Character : %c", samp.c);
printf("\n\ninteger: ");
scanf("%d", &samp.i);
printf("Integer : %d", samp.i);
Output:-
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
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
}
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
fputc(temp [i],q);
}// END FOR I
fclose(p);
fclose(q);
printf("\n File written in reverse order");
return 0;
}// end main
Output:-
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
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: