Structure_C_Programming
Structure_C_Programming
A record or structure is a user-defined data type in C that allows combining data of different types
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
#include <stdio.h>
#include <string.h>
struct student {
int id;
char name[50];
float marks;
};
int main() {
struct student s;
// Input data
scanf("%d", &s.id);
scanf("%s", s.name);
scanf("%f", &s.marks);
// Display data
printf("\nStudent Details:\n");
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
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() {
scanf("%d", &s[i].id);
scanf("%s", s[i].name);
scanf("%f", &s[i].marks);
printf("\nStudent Details:\n");
return 0;
|-----------|---------------------|
| An array stores data of the same type (e.g., all integers, all floats). | A structure stores data of
| 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;}; |
A pointer to a structure is a pointer variable that points to the memory address of a structure. It is
Example:
#include <stdio.h>
struct student {
int id;
char name[50];
float marks;
};
int main() {
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
scanf("%d", &s[i].id);
scanf("%s", s[i].name);
scanf("%f", &s[i].marks);
printf("\nStudent Details:\n");
return 0;