C Unit 4
C Unit 4
PART-A
1. How the nested structure can define in C, explain with syntax and example?
(CO4) (Apr/May 2024)
A structure that contains another structure as its member is called a nested structure.
Syntax
struct name_1
{
member 1;
member 2;
.
.
member n;
};
struct name_2
{
member_1;
member_2;
.
.
member_n;
};
struct structure_name variable_name;
Example
struct DOB
{
int day;
int month;
int year;
};
struct student
{
int roll_no;
char name [100];
float fees;
struct DOB date;
};
struct student stud1;
2. What will be the output of the following code? (CO4) (Apr/May 2024)
#include<stdio.h>
main()
{
typedef int a;
a b=2,c=8,d;
d=(b*2)/2+8;
printf(“%d”,d);
}
Output
d = (2 * 2) / 2 + 8 = 4 / 2 + 8 = 2 + 8 = 10
10
structure Union
A structure is a user-defined data type that A union is a special data type that allows to
can be used to group the different types into store different data types in the same
a single type. memory location
Every member has its own memory. All members use the same memory.
The struct keyword is used to define the The union keyword is used to define the
structure. union.
All members of structure can be initialized Only the first member of union can be
initialized
Syntax Syntax
struct structure_name union union_name
{ {
structure element 1; union element 1;
structure element 2; union element 2;
. .
. .
. .
. .
structure element n; union element n;
}structure_variable_name; } union_variable_name;
5. Illustrate with an example for each, the following operators with regard to pointers and structures.
&,*,.,->(CO4) (Nov/Dec 2023)
* (Dereference Operator) - Access the value pointed to by the pointer.
int x = 10;
int *ptr = &x;
& (Address-of Operator) - Get the memory address of a variable or structure member.
sint *ptr = &x;
-> (Arrow Operator) - Access members of a structure via a pointer to that structure.
ptr->x, ptr->y
. (Dot Operator) - Access members of a structure directly.
struct point p = {5, 6};
printf("x: %d, y: %d\n", p.x, p.y);
Array Structure
Syntax
Syntax
struct structure_name{ data_type1 ele1;
data_type array_name[size];
data_type2 ele2; };
Array elements are accessed by their index Structure elements are accessed by dot
number. operator.
11.Define the structure called ID_card to hold the details of the student.
(CO4) (Apr/May 2016)
struct ID_card
{
char Reg_no[20];
char name[50];
int Grade;
char mobile[15];
char address[50];
int age;
}b1;
Accessing structure elements
b1.Reg_no[i]
b1.name[i]
b1. Grade[i]
b1. Mobile[i]
b1.address[i]
b1.age[i]
13. What is the output of the following code fragment? (CO4) (Apr/May 2019)
struct point
{
int x;
int y;
};struct point origin,*pp;
main()
{
pp=&origin;
printf(“origin is(%d%d)\n”,(*pp).x,pp->y);
}
Output
Origin is (0,0)
Example
struct node
{
int data;
struct node *next;
}
float fees;
};
struct student s[30];
20. Write the C program to read and print the student details.
#include <stdio.h>
struct Student
{
char name[50];
int roll_no;
};
int main()
{
struct Student s;
// Input student details
printf("Enter student name: ");
scanf("%s", s.name);
printf("Enter roll number: ");
scanf("%d", &s.roll_no);
// Output student details
printf("\nStudent Details:\n");
printf("Name: %s\n", s.name);
printf("Roll Number: %d\n", s.roll_no);
return 0;
}
Part-B
1. Discuss the concepts,advantages of self-referential structure and explain with an example program.
(CO4) (Apr/May 2024)
Self Referential Structure
Structures pointing to the same type of structures. A self-referential structure in C is a structure that
contains a pointer to itself. This means that a structure type has a member which is a pointer to the same type
of structure. This is often used for creating data structures like linked lists, trees, and other types of recursive
data structures.
Syntax
struct node{
int data1;
char data2;
struct node* link;
};
Program
#include <stdio.h>
struct node {
int data1;
char data2;
struct node* link;
};
int main()
{
struct node ob1; // Node1
// Initialization
ob1.link = NULL;
ob1.data1 = 10;
ob1.data2 = 20;
struct node ob2; // Node2
ob2.link = NULL;
ob2.data1 = 30;
ob2.data2 = 40;
// Linking ob1 and ob2
ob1.link = &ob2;
// Accessing data members of ob2 using ob1
printf("%d", ob1.link->data1);
printf("\n%d", ob1.link->data2);
return 0;
}
Output
30
40
Advantages
Dynamic memory Allocation
Efficient insertion and Deletion
Easy to extend
Flexible data representation
Recursive data modeling
{
int day;
int month;
int year;
};
struct student
{
int roll_no;
char name [100];
float fees;
struct DOB date;
};
struct student stud1;
clrscr();
printf("\n Enter the roll number: ");
scanf("%d", &stud1.roll_no);
printf("\n Enter the name: ");
scanf("%s", stud1.name);
printf("\n Enter the fees: ");
scanf("%f", &stud1.fees);
printf("\n Enter the DOB: ");
scanf("%d %d %d", &stud1.date.day, &stud1.date.month, &stud1.date. year);
printf("\n *** STUDENT'S DETAILS ***");
printf("\n ROLL No. = %d", stud1.roll_no);
printf("\n NAME %%s", stud1.name);
printf("\n FEES = %f", stud1. fees);
printf("\n DOB %d %d %d", stud1. date.day, stud1.date.month, stud1.date. year);
getch();
return 0;
}
Output
Enter the roll number: 01
Enter the name: Rahul
Enter the fees: 45000
Enter the DOB: 25 09 1991
3. List the operations on Linked List. Explain insertion at end deletion at beginning of a singly linked
list. (CO4) (Nov/Dec 2023)
Singly linked list
It is a linear data structure.It is a type of list.In singly linked list,each node in the list store the content
of the node and a pointer or reference to the next node in the list.The node stores the address of the next node
in sequence.A singly linked list allows traversal of data only in one direction.
The following diagram represents the structure of a singly linked list:
4. Consider a structure ‘furniture’ that includes the information about furniture in shop. write a
function call statement that has the argument as a pointer to the structure and number of furniture.
Also provide the corresponding function definition statement that receives the arguments.
(CO4) (Nov/Dec 2023)
(i) Write the function body that prints the details of ‘n’ furniture
(ii) Use ‘typedef’ to define ‘furniture’ as array of structures
Program
#include <stdio.h>
typedef struct
{
char name[50];
float price;
int quantity;
} Furniture;
void displayFurniture(Furniture *f, int n);
int main()
{
int n, i;
printf("Enter the number of furniture items: ");
scanf("%d", &n);
Furniture items[n];
for (i = 0; i < n; i++)
{
printf("\nEnter details of furniture %d:\n", i + 1);
printf("Name: ");
scanf(" %s", items[i].name); // Reads a line of input
printf("Price: ");
scanf("%f", &items[i].price);
printf("Quantity: ");
scanf("%d", &items[i].quantity);
}
displayFurniture(items, n);
return 0;
}
void displayFurniture(Furniture *f, int n)
{
printf("\nFurniture Details:\n");
for (int i = 0; i < n; i++)
{
printf("\nFurniture %d:\n", i + 1);
printf("Name: %s\n", f[i].name);
printf("Price: %.2f\n", f[i].price);
printf("Quantity: %d\n", f[i].quantity);
}
}
Output
Enter the number of furniture items: 2
Furniture Details:
Furniture 1:
Name: Chair
Price: 49.99
Quantity: 10
Furniture 2:
Name: Table
Price: 120.50
Quantity: 5
5.Difference between structure and union with example. (CO4) (Apr/May 2023)
Parameter Structure Union
Definition A structure is a user-defined A union is a user-defined
data type that groups data type that allows storing
different data types into a different data types at the
single entity. same memory location.
Keyword The keyword struct is used The keyword union is used
to define a structure to define a union
Size The size is the sum of the The size is equal to the size
sizes of all members, with of the largest member, with
padding if necessary. possible padding.
Memory Allocation Each member within a Memory allocated is shared
structure is allocated unique by individual members of
storage area of location. union.
Accessing Members Individual member can be Only one member can be
accessed at a time accessed at a time.
Syntax struct name { union name {
member1 definition; member1 definition;
member2 definition; member2 definition;
… …
memberN definition; memberN definition;
}; };
Program
#include <stdio.h>
union unionJob
{
//defining a union
char name[32];
float salary;
int workerNo;
} uJob;
struct structJob
{
char name[32];
float salary;
int workerNo;
} sJob;
int main()
{
printf("size of union = %d bytes", sizeof(uJob));
malloc() is a function that creates one calloc() is a function that assigns a specified number
1. block of memory of a fixed size. of blocks of memory to a single variable.
6. Syntax : void* malloc(size_t size); Syntax : void* calloc(size_t num, size_t size);
7. Write a C program to create mark sheet for students using self-referential structure.
(CO4) (Nov/Dec 2022)
Program
#include <stdio.h>
struct Student
{
char name[50];
int rollNo;
float marks[5]; // Marks for 5 subjects
float totalMarks;
float percentage;
struct Student *next;
};
int main()
{
int n;
printf("Enter the number of students: ");
scanf("%d", &n);
struct Student students[n];
for (int i = 0; i < n; i++)
{
struct Student *current = &students[i];
current->next = NULL; // Initialize next pointer to NULL
printf("\nEnter student %d name: ", i + 1);
scanf(" %[^\n]s", current->name);
printf("Enter roll number: ");
scanf("%d", ¤t->rollNo);
printf("Enter marks for 5 subjects:\n");
current->totalMarks = 0; // Initialize total marks to 0
for (int j = 0; j < 5; j++)
{
printf("Subject %d: ", j + 1);
scanf("%f", ¤t->marks[j]);
current->totalMarks += current->marks[j];
}
current->percentage = (current->totalMarks / 500) * 100;
}
printf("\n---- Student Mark Sheet ----\n");
for (int i = 0; i < n; i++)
{
struct Student *current = &students[i];
printf("\nName: %s\n", current->name);
printf("Roll Number: %d\n", current->rollNo);
printf("Marks: ");
for (int j = 0; j < 5; j++)
{
printf("%f ", current->marks[j]);
}
printf("\nTotal Marks: %f\n", current->totalMarks);
printf("Percentage: %f\n", current->percentage);
}
return 0;
}
Output
Enter the number of students: 2
8. Write a C program using structures to prepare employee pay roll of a company. The number of
records is created based on the user input. (CO4) (Nov/Dec 2019)
Program
#include<stdio.h>
#include<conio.h>
struct emp
{
int empno ;
char name[10] ;
int bpay, allow, ded, npay ;
} e[10] ;
void main()
{
int i, n ;
printf("Enter the number of employees : ") ;
scanf("%d", &n) ;
for(i = 0 ; i < n ; i++)
{
printf("\nEnter the employee number : ") ;
scanf("%d", &e[i].empno) ;
printf("\nEnter the name : ") ;
scanf("%s", e[i].name) ;
printf("\nEnter the basic pay, allowances & deductions : ") ;
scanf("%d %d %d", &e[i].bpay, &e[i].allow, &e[i].ded) ;
e[i].npay = e[i].bpay + e[i].allow - e[i].ded ;
}
printf("\nEmp. No. Name \t Bpay \t Allow \t Ded \t Npay \n\n") ;
for(i = 0 ; i < n ; i++)
{
printf("%d \t %s \t %d \t %d \t %d \t %d \n", e[i].empno,e[i].name, e[i].bpay, e[i].allow, e[i].ded,
e[i].npay) ;
}
getch() ;
}
Output
Enter the number of employees: 2
Enter the employee number: 101
Enter the name: Alice
Enter the basic pay, allowances & deductions: 5000 2000 500
Enter the employee number: 102
Enter the name: Bob
Enter the basic pay, allowances & deductions: 6000 1500 700
9. Define a structure called book with book name, author name and price. Write a C program to read
the details of book name, author name and price of 200 books in a library and display the total cost of
the books and book details whose price is above Rs.500.(CO4) (Apr/May 2018)
Program
#include <stdio.h>
#define MAX_BOOKS 200
// Define the structure for book
struct book
{
char book_name[100];
char author_name[100];
float price;
};
int main()
{
struct book books[MAX_BOOKS];
float total_cost = 0;
int i;
// Input details for 200 books
for(i = 0; i < MAX_BOOKS; i++)
{
printf("Enter details for book %d:\n", i + 1);
10. Explain about pointers and structure with an example program. (CO4)(Apr/May 2012)
Structure can be created and accessed using pointers. When we have a pointer of structure type we use
to access the structure member.
Syntax
struct structure_name
{
data member;
};
struct structure_name *ptr;
int i,n;
printf("Enter the number of students: ");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
ptr[i] = (struct student *)malloc(sizeof(struct student));
printf("\nEnter data for student %d:\n", i + 1);
printf("Enter roll number: ");
scanf("%d", &ptr[i]->rollno);
printf("Enter name: ");
scanf("%s", ptr[i]->name);
printf("Enter department: ");
scanf("%s", ptr[i]->dept);
}
// Display student details
printf("\nDetails of students:\n");
for (i = 0; i < n; i++)
{
printf("Student %d:\n", i + 1);
printf("Roll Number: %d\n", ptr[i]->rollno);
printf("Name: %s\n", ptr[i]->name);
printf("Department: %s\n", ptr[i]->dept);
}
return 0;
}
Output:
Enter the number of students: 2
Enter data for student 1:
Enter roll number: 101
Enter name: Alice
Enter department: CS
Enter data for student 2:
Enter roll number: 102
Enter name: Bob
Enter department: IT
Details of students:
Student 1:
Roll Number: 101
Name: Alice
Department: CS
Student 2:
Roll Number: 102
Name: Bob
Department: IT