Sample Questions and Answers on Structures in C
1. Define record (structure) and give example.
A record or structure is a user-defined data type in C that allows combining data of different types
under a single name.
Each item in a structure is called a member or field. These fields can be of different types (int, float,
char, etc.).
Example:
struct student {
int id;
char name[50];
float marks;
};
2. Write code to declare a record (structure) with id, name, and marks of a student, write code to
store (enter) data and print (display) the data.
#include <stdio.h>
#include <string.h>
struct student {
int id;
char name[50];
float marks;
};
int main() {
struct student s;
// Input data
printf("Enter student ID: ");
scanf("%d", &s.id);
printf("Enter student name: ");
scanf("%s", s.name);
printf("Enter marks: ");
scanf("%f", &s.marks);
// Display data
printf("\nStudent Details:\n");
printf("ID: %d\n", s.id);
printf("Name: %s\n", s.name);
printf("Marks: %.2f\n", s.marks);
return 0;
3. What is an array of structures (records)? Write its usefulness and explain with an example.
An array of structures is a collection of structures where each structure in the array holds data of the
same type, but each element can have different values.
Usefulness:
- It helps store data of similar types for multiple entities (e.g., students, employees) in a single
variable.
Example:
#include <stdio.h>
struct student {
int id;
char name[50];
float marks;
};
int main() {
struct student s[5]; // Array of 5 students
// Input data for 5 students
for(int i = 0; i < 5; i++) {
printf("Enter details for student %d:\n", i+1);
printf("Enter ID: ");
scanf("%d", &s[i].id);
printf("Enter name: ");
scanf("%s", s[i].name);
printf("Enter marks: ");
scanf("%f", &s[i].marks);
// Display data for 5 students
printf("\nStudent Details:\n");
for(int i = 0; i < 5; i++) {
printf("\nStudent %d:\n", i+1);
printf("ID: %d\n", s[i].id);
printf("Name: %s\n", s[i].name);
printf("Marks: %.2f\n", s[i].marks);
return 0;
4. What is the difference between an array and a record (structure)?
| Array | Structure (Record) |
|-----------|---------------------|
| An array stores data of the same type (e.g., all integers, all floats). | A structure stores data of
different types (e.g., integer, float, string). |
| Arrays use a single data type for all elements. | Structures use multiple data types for its members.
| Example: int arr[5]; | Example: struct student {int id; char name[50]; float marks;}; |
5. What is a pointer to the record and structure? Give an example.
A pointer to a structure is a pointer variable that points to the memory address of a structure. It is
used to access the structure's members using the -> operator.
Example:
#include <stdio.h>
struct student {
int id;
char name[50];
float marks;
};
int main() {
struct student s = {101, "John", 85.5};
struct student *ptr = &s; // Pointer to structure
// Access structure members using pointer
printf("Student ID: %d\n", ptr->id);
printf("Student Name: %s\n", ptr->name);
printf("Student Marks: %.2f\n", ptr->marks);
return 0;
6. There are 40 students in a section, declare a structure with id, name, and marks of a student,
write code to enter (store) data for the students of the section and print (display) their data.
#include <stdio.h>
struct student {
int id;
char name[50];
float marks;
};
int main() {
struct student s[40]; // Array of 40 students
// Input data for 40 students
for(int i = 0; i < 40; i++) {
printf("Enter details for student %d:\n", i+1);
printf("Enter ID: ");
scanf("%d", &s[i].id);
printf("Enter name: ");
scanf("%s", s[i].name);
printf("Enter marks: ");
scanf("%f", &s[i].marks);
// Display data for 40 students
printf("\nStudent Details:\n");
for(int i = 0; i < 40; i++) {
printf("\nStudent %d:\n", i+1);
printf("ID: %d\n", s[i].id);
printf("Name: %s\n", s[i].name);
printf("Marks: %.2f\n", s[i].marks);
return 0;