0% found this document useful (0 votes)
3 views

Structure_C_Programming

The document provides an overview of structures in C, including definitions, examples, and code snippets for declaring and using structures to store student data. It explains the concept of arrays of structures, the differences between arrays and structures, and how to use pointers with structures. Additionally, it includes code for managing data for multiple students in a section.

Uploaded by

humayunahmedsust
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Structure_C_Programming

The document provides an overview of structures in C, including definitions, examples, and code snippets for declaring and using structures to store student data. It explains the concept of arrays of structures, the differences between arrays and structures, and how to use pointers with structures. Additionally, it includes code for managing data for multiple students in a section.

Uploaded by

humayunahmedsust
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

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;

You might also like