0% found this document useful (0 votes)
10 views2 pages

Structures Using Arrays

The document contains three C programs demonstrating the use of structures and arrays. Program 1 defines a 'book' structure and initializes an array of books, displaying their titles, prices, and pages. Program 2 defines a 'Student' structure, initializes an array of students, and prints their details, while Program 3 showcases different ways to initialize a structure with nested and designated initializers.

Uploaded by

latha2009
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Structures Using Arrays

The document contains three C programs demonstrating the use of structures and arrays. Program 1 defines a 'book' structure and initializes an array of books, displaying their titles, prices, and pages. Program 2 defines a 'Student' structure, initializes an array of students, and prints their details, while Program 3 showcases different ways to initialize a structure with nested and designated initializers.

Uploaded by

latha2009
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Structures Using Arrays

Program 1:
#include <stdio.h>
#include<string.h>
struct book{
char title[10];
double price;
int pages;
};
int main()
{
struct book b[3];
strcpy(b[0].title, "Learn C");
b[0].price = 650.50;
b[0].pages = 325;
strcpy(b[1].title, "C Pointers");
b[1].price = 175;
b[1].pages = 225;
strcpy(b[2].title, "C Pearls");
b[2].price = 250;
b[2].pages = 325;
printf("\nList of Books:\n");
for (int i = 0; i < 3; i++)
{
printf("Title: %s \tPrice: %7.2lf \tPages: %d\n", b[i].title, b[i].price, b[i].pages);
}
return 0;
}

Program 2

#include<stdio.h>
struct Student {
char name[50];
int age;
float marks;
};
int main() {
// Declaration and initialization of an array of structures
struct Student students[3] = {
{"Nikhil", 20, 85.5},
{"Shubham", 22, 90.0},
{"Vivek", 25, 78.0}
};
// Traversing through the array of structures and displaying the data
for (int i = 0; i < 3; i++) {
printf("Student %d:\n", i+1);
printf("Name: %s\n", students[i].name);
printf("Age: %d\n", students[i].age);
printf("Marks: %.2f\n\n", students[i].marks);
}
return 0;
}
Program 3:

#include <stdio.h>
#include <string.h>
// Structure definition
struct A {
int var;
char c;
};

int main() {
// Declaration and initialization using nested initializer list
struct A arr1[2] = { {1, 'a'}, {2, 'b'} };

// Declaration and initialization using non-nested initializer list


struct A arr2[2] = { 10, 'A', 20, 'B' };

// Designated initialization
struct A arr3[2] = { {.c = 'A', .var = 10},
{.var = 2, .c = 'b'} };

for (int i = 0; i < 2; i++)


printf("%d %c", arr1[i].var, arr1[i].c);

printf("");

for (int i = 0; i < 2; i++)


printf("%d %c", arr2[i].var, arr2[i].c);

printf("");

for (int i = 0; i < 2; i++)


printf("%d %c", arr3[i].var, arr3[i].c);

printf(" ");
return 0;
}

You might also like