Q.1 what is structure?
Explain c syntax of structure declaration
with example.
A structure is a user-defined data type in C that allows you to
group different types of variables under one name.
• It helps in creating a record that can store multiple
values (like name, age, marks) of different data types.
• Used in programs to handle complex data easily (like
student records, employee info, etc.).
Syntax of Structure in C:
struct StructureName {
data_type member1;
data_type member2;
...
Conclusion:
• A structure is used to group related data.
• Each element inside the structure is called a member.
• It's useful for handling real-world data like students,
books, employees, etc.
Q.2 what is file handling? Explain different file handling function
with example.
File handling in C means storing and accessing data using files
(like .txt, .dat files). It allows data to be stored permanently and
used later — unlike variables, which lose data after program ends.
• To store output of a program permanently.
• To read input from a file instead of the keyboard.
• Useful in large applications like student records, result
management, etc.
Function Purpose Example
Opens a file in a
fopen() fopen("data.txt", "w");
specific mode
Writes formatted
fprintf() fprintf(fp, "Hello");
data to a file
Reads formatted
fscanf() fscanf(fp, "%d", &x);
data from a file
Reads a single
fgetc() character from a ch = fgetc(fp);
file
Writes a single
fputc() fputc('A', fp);
character to a file
Closes an opened
fclose() fclose(fp);
file
Q.3 compare between structure and union.
Structure (struct) Union (union)
A user-defined data type that can A user-defined data type
hold multiple members of that can store only one
different types at the same time. member at a time.
Memory is allocated separately All members share the same
for each member. memory location.
Size = sum of sizes of all Size = size of the largest
members. member.
All members can be accessed at Only one member can be
the same time. used at a time.
Used when all values need to be Used to save memory when
stored (e.g., student record). storing one value at a time.
struct union
Q.4 develop a c program to store and display student record using structure with data
element roll_no, name, marks.
#include <stdio.h>
#include <string.h>
struct Student {
int roll_no;
char name[50];
float marks;
};
int main() {
struct Student s1;
// Input student details
printf("Enter roll number: ");
scanf("%d", &s1.roll_no);
printf("Enter name: ");
scanf("%s", s1.name);
printf("Enter marks: ");
scanf("%f", &s1.marks);
printf("\n--- Student Record ---\n");
printf("Roll No: %d\n", s1.roll_no);
printf("Name : %s\n", s1.name);
printf("Marks : %.2f\n", s1.marks);
return 0;
}
OUTPUT:
Enter roll number: 101
Enter name: Rahul
Enter marks: 89.5
--- Student Record ---
Roll No: 101
Name : Rahul
Marks : 89.50
Q.5 explain about sequential and random access in file.
Sequential Access Random Access
Accesses data one by one from Accesses data directly from any
start to end. location in the file.
Jumps to specific location using
Moves through data in order.
file pointer.
Slower for large files (must go Faster if you need a specific
through each record). record.
fgetc(), fgets(), fscanf(),
fseek(), ftell(), rewind()
fprintf()
Accessing specific records in a
Reading full text or log files in order.
database-like file.
Reading file from top to bottom line Jumping to 10th byte and reading
by line. data from there.
Q.6 explain union with one example.
A union is a user-defined data type in C, similar to a
structure. However, the key difference is:
In a structure, each member has its own memory
space, but
In a union, all members share the same memory
location.
So, a union can store only one value at a time, among its
members.
Unions are mainly used when:
• You don’t need to store all values at the same time.
• You want to save memory.
• You need a variable that can hold different types of
data at different times.
Conclusion:
• A union is helpful when you want to save memory and
use different data types in the same memory space,
but not at the same time.
• It's a powerful concept for low-level programming,
embedded systems, or systems with limited memory.
Q.7 write a program for creating a file.
#include <stdio.h>
int main() {
FILE *fp;
fp = fopen("example.txt", "w");
if (fp == NULL) {
printf("Error creating the file.\n");
return 1;
}
fprintf(fp, "Hello, this is a test file.\n");
fprintf(fp, "Welcome to file handling in C.\n");
// Close the file
fclose(fp);
printf("File created and data written successfully.\n");
return 0;
}
OUTPUT:
File created and data written successfully.