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

Structure in C for DS LAB

The document contains multiple C programming examples demonstrating the use of structures. It covers defining structures for dates and students, creating arrays of structures, using nested structures, and passing structures to functions. Each example includes code snippets and explanations of their functionality.

Uploaded by

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

Structure in C for DS LAB

The document contains multiple C programming examples demonstrating the use of structures. It covers defining structures for dates and students, creating arrays of structures, using nested structures, and passing structures to functions. Each example includes code snippets and explanations of their functionality.

Uploaded by

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

1.

Structure for Date

Problem: Define a structure to represent a date and display it.

#include <stdio.h>

struct Date {

int day;

int month;

int year;

};

int main() {

struct Date today;

today.day = 3;

today.month = 11;

today.year = 2024;

printf("Today's date is: %02d/%02d/%d\n", today.day, today.month, today.year);

return 0;

}
2.Defining a Structure for a Student

Problem: Create a structure to store information about a student (name, age, and grade).

#include <stdio.h>

#include <string.h>

struct Student {

char name[50];

int age;

float grade;

};

int main() {

struct Student student1;

strcpy(student1.name, "Alice");

student1.age = 20;

student1.grade = 88.5;

printf("Student Name: %s\n", student1.name);

printf("Age: %d\n", student1.age);

printf("Grade: %.2f\n", student1.grade);

return 0;

}
3.Array of Structures

Problem: Create an array of structures to hold multiple students' information

#include <stdio.h>

#include <string.h>

struct Student {

char name[20];

int age;

float grade;

};

int main() {

struct Student students[3];

// Input data for three students

for (int i = 0; i < 3; i++) {

printf("Enter name, age, and grade for student %d: ", i + 1);

scanf("%s %d %f", students[i].name, &students[i].age, &students[i].grade);

}
// Display the information

printf("\nStudent Information:\n");

for (int i = 0; i < 3; i++) {

printf("Name: %s, Age: %d, Grade: %.2f\n", students[i].name, students[i].age,


students[i].grade);

return 0;

4.Structure with Nested Structures

Problem: Define a structure for a book that contains a nested structure for the author.

#include <stdio.h>
#include <string.h>

struct Author {
char name[20];
char nationality[20];
};

struct Book {
char title[100];
struct Author author;
int year;
};

int main() {
struct Book book;

strcpy(book.title, "The Great Gatsby");


strcpy(book.author.name, "F. Scott Fitzgerald");
strcpy(book.author.nationality, "American");
book.year = 1925;

printf("Book Title: %s\n", book.title);


printf("Author: %s\n", book.author.name);
printf("Nationality: %s\n", book.author.nationality);
printf("Year: %d\n", book.year);

return 0;
}

5.Passing Structures to Functions

Problem: Write a function to modify a student's grade.

#include <stdio.h>
#include <string.h>

struct Student {
char name[50];
int age;
float grade;
};

void updateGrade(struct Student *s, float newGrade) {


s->grade = newGrade; // Update the grade using pointer
}

int main() {
struct Student student;

strcpy(student.name, "Bob");
student.age = 21;
student.grade = 75.0;

printf("Before update: %s's Grade: %.2f\n", student.name, student.grade);


updateGrade(&student, 85.5);
printf("After update: %s's Grade: %.2f\n", student.name, student.grade);

return 0;
}

You might also like