Unit 4 QB
Unit 4 QB
Question bank
struct structure_name {
data_type member1;
data_type member2;
...
};
3. Provide an example of a structure for storing details of a student.
struct Student {
char name[50];
int roll_no;
float marks;
};
5. What is the difference between dot operator (.) and arrow operator (->) for accessing structure
members?
Dot Operator (.):
The dot operator is used to access members of a structure using a structure variable (i.e., an instance
of a structure).
Synatx: structure_variable.member
struct Student {
char name[50];
int roll_no;
};
int main() {
struct Student student1 = {"John", 101};
printf("Name: %s\n", student1.name);
printf("Roll No: %d\n", student1.roll_no);
}
Each node of the list is represented by a structure that has a self-referenced pointer to the next node
struct Node {
int data; // Data field to store the value
struct Node* next; // Pointer to the next node (self-referenced structure)
};
9. Differentiate between single-linked and multi-linked with respect to structures.
Number of Pointers One pointer (next) to link Multiple pointers (e.g., next
nodes in a single direction and prev) or more, enabling
complex relationships
Memory Usage Uses less memory per node (1 Uses more memory per node
pointer) (multiple pointers)
Types of Links Links nodes in a single chain Links nodes across multiple
dimensions (e.g., doubly linked
list or multidimensional list)
10. Explain nested structures (embedded structures) and provide a suitable example.
A nested structure (also known as an embedded structure) is a structure that contains another
structure as one of its members.
Syntax:
struct OuterStructure {
struct InnerStructure inner; // Embedding another structure inside
// other members...
};
Example:
struct Address {
char street[100];
char city[50];
int postal_code;
};
struct Student {
char name[50];
int roll_no;
struct Address addr; // Nested structure
};
struct Employee {
char name[100];
int emp_id;
char department[50];
float salary;
};
In educational applications, structures can represent a student's grades across different subjects. It
makes it easier to track and manipulate a student's performance.
struct Grade {
char subject[50];
float grade;
};
Memory Allocation All members share the same memory Each member gets its own
location. memory location.
Size Size of the union is the size of the Size of the structure is the sum of
largest member. all member sizes, plus padding.
Usage Suitable for situations where only one Suitable for cases where all
member is used at a time. members need to be accessed
simultaneously.
Example Size union Data { int i; float f; char c; } → struct Data { int i; float f; char c; }
4 bytes. → 12 bytes (or 9 bytes with no
padding).
Memory Efficiency More memory-efficient because only Less memory-efficient because all
one member is used at a time. members are stored at once.
17. How would you access a member of a union using a pointer? Provide an example.
Memory Allocation All members share the same memory Each member gets its own
location. memory location.
Size Size of the union is the size of the Size of the structure is the sum of
largest member. all member sizes, plus padding.
19. Analyze the following code snippet and identify any errors:
struct Example {
int a;
float b;
} ex;
ex.a = 10;
printf("%d", b);
Answer:
a is the member of the structure. To access it, ex.a should be used in the printf statement.
struct Sample {
int id;
char name[20];
float salary;
};
In this structure, the total size without considering padding would be:
21. Write short notes on storage classes in C and their use in variable declarations.
storage classes determine the scope, lifetime, visibility, and memory location of variables. The four
primary storage classes are:
Auto
Register
Static
Extern
22. Explain the difference between auto and register storage classes.
Aspect auto Storage Class register Storage Class
Memory Location Stored in the stack memory of Stored in the CPU register (if
the program. available); otherwise, stored in
stack.
Default Behavior Default storage class for local Must be explicitly specified
variables. using the register keyword.
Lifetime Exists until the block in which Same as auto: exists until the
it is declared exits. block exits.
Addressing Can take the address using the Cannot take the address using
& operator. & (registers don't have memory
addresses).
int main() {
// Call the function multiple times
counterFunction(); // First call
counterFunction(); // Second call
counterFunction(); // Third call
return 0;
}
24. What is the purpose of the extern keyword in C? Write a code snippet to illustrate its usage.
The extern keyword in C is used to declare a global variable or function that is defined in another file
or scope. It allows variables or functions to be shared across multiple files in a program.
File1.c:
int a=10;
void message()
{
printf("Hello from File1");
}
File2.c:
#include <stdio.h>
extern int a; //a is in external file
extern void message();
int main()
{
printf("%d",a);
message();
return 0;
}
Write a program to declare and initialize a structure to store employee details (ID, Name, Salary).
#include <stdio.h>
int main() {
// Declare and initialize an Employee structure
struct Employee emp1 = {101, "John Doe", 50000.50};
return 0;
}
Justify the use of structures in real-world applications by providing at least two practical examples.
Structures in C allow developers to group related data types into a single unit, making it easier to
manage and organize complex data. Here are two practical examples demonstrating their utility:
In a real-world scenario, an organization needs to manage employee records, including details such as
employee ID, name, department, and salary. A structure simplifies the representation of each employee
as a single unit.
struct Employee {
int id;
char name[50];
char department[20];
float salary;
};
Purpose: Structures provide a logical grouping of employee attributes, making the data easy to
process, store, and transfer.
In embedded systems, sensor data from devices like temperature sensors, humidity sensors, and
pressure sensors are collected and processed. Structures can represent multiple data points.
struct SensorData {
int sensorID;
float temperature;
float humidity;
};
Purpose: Groups related sensor information for easy processing and monitoring.
Medical Records
Healthcare systems store patient data, such as ID, name, medical history, and test results. Structures
help organize and access this data.
struct Patient {
int patientID;
char name[50];
char medicalHistory[200];
float testResults[5];
};
2. (a) Explain the concept of nested structures with an example to represent a student’s marks
and personal details. (b) Analyze the memory allocation for the above structure and calculate the
total memory consumed. (8+5)
A nested structure (also known as an embedded structure) is a structure that contains another
structure as one of its members.
Syntax:
struct OuterStructure {
struct InnerStructure inner; // Embedding another structure inside
// other members...
};
#include <stdio.h>
int main() {
// Declare and initialize a student structure
struct Student student1 = {101, "John Doe", {85.5, 90.0, 78.5}};
return 0;
}
1. Marks Structure:
o float math: 4 bytes
o float physics: 4 bytes
o float chemistry: 4 bytes
o Total for Marks: 12 bytes
2. Student Structure:
o int rollNumber: 4 bytes
o char name[50]: 50 bytes
o struct Marks scores: 12 bytes (embedded structure)
o Total for Student:
Without padding: 4+50+12=664 + 50 + 12 = 664+50+12=66 bytes
3. (a) Compare and contrast the memory allocation of structures and unions with a
diagrammatic explanation.
Structure Union
In structure each member get separate space in In union, the total memory space allocated is equal
memory. Take below example. to the member with largest size. All other
members share the same memory space. This is
the biggest difference between structure and
union.
struct student
{
union student
int rollno;
{
char gender;
int rollno;
float marks;
char gender;
}s1; float marks;
}s1;
We can access any member in any sequence. We can access only that variable whose value is
recently stored.
s1.rollno = 20;
s1.rollno = 20;
s1.marks = 90.0;
s1.marks = 90.0;
printf(“%d”,s1.rollno);
printf(“%d”,s1.rollno);
All the members can be initialized while declaring Only first member can be initialized while
the variable of structure. declaring the variable of union. In above example
we can initialize only variable rollno at the time
of declaration of variable.
Write a program to demonstrate the use of unions in handling data of multiple types (e.g., integer,
float, and string).
#include <stdio.h>
#include <string.h>
int main() {
union Data data;
// Assign and display integer value
data.intVal = 25;
printf("After assigning intVal:\n");
printf("intVal: %d\n", data.intVal);
return 0;
}