Structures in C
Definition
C Structure is a collection of different data types which are grouped together and
each element in a C structure is called member.
‘
struct’ keyword is used to create a structure.
Syntax of a structure.
struct tag_name { //Here tag_name is the structure’s name. member1; //Member 1 of the structure.
member2; //Member 2 of the structure.
Declaring a structure.
I method
struct book { char name; float price; int pages; };
struct book b1,b2,b3;
II method
Giving values to members
#include <stdio.h> int main()
{
struct book {
char name; float price; int pages; };
struct book b1, b2, b3;
printf(“And this is what you entered.\n”);
printf(“%c %f %d\n”, b1.name, b1.price, b1.pages); printf(“%c %f %d\n”, b2.name, b2.price, b2.pages); printf(“%c %f %d\n”, b3.name, b3.price, b3.pages); return 0;
}
OUTPUT
Enter names, prices & no. of pages of 3 books. A 100.0 342
F 343.2 534 G 213.0 321
And this is what you entered. A 100.0 342
struct Book {
int pages;
char name[10]; char author[10]; float price;
}b1;
Size = size of 'Pages' + size of 'Name' + size of 'Author' + size of 'Price' = 2 + 10 * 1 + 10 * 1 + 4
= 2 + 10 + 10 + 4 = 26
#include <stdio.h> int main()
{
struct Book {
int pages;
char name[10]; char author[10]; float price;
}b1;
printf("\nSize of Structure : %d",sizeof(b1)); return 0;
}
OUTPUT
26
Array of Structures
#include <stdio.h> int main()
{
struct book {
char name; float price; int pages; }
struck book b[10]; int i;
for (i=0; i<=9;i++) {
printf(“Enter name, price and pages.\n”);
for (i=0; i<=9; i++) {
printf(“%c %f %d\n”, b[i].name, b[i].price, b[i].pages);
}
return 0; }
OUTPUT
Enter name, price and pages. A 54.3 984
J 54.3 342 A 43.3 542 G 54.3 342 A 54.3 342 F 43.4 342 A 54.3 432 H 54.3 112 A 54.3 342 A 54.3 546
#include <stdio.h> struct Student
{
int Roll;
char Name[25];
int Marks[3]; int Total;
float Avg; };
void main() {
int i;
struct Student S;
printf("Enter Student Roll : \n"); scanf("%d\n",&S.Roll);
printf("Enter Student Name : \n"); scanf("%s\n",&S.Name);
S.Total = 0;
for(i=0;i<3;i++) {
printf("Enter Marks : \n"); scanf("%d\n",&S.Marks[i]);
S.Total += S.Marks[i]; }
S.Avg = S.Total / 3;
printf("\nRoll : %d",S.Roll);
printf("\nTotal : %d",S.Total); printf("\nAverage : %f",S.Avg); }
Output :
Enter Student Roll : 10
Enter Student Name : Kumar Enter Marks 1 : 78
Enter Marks 2 : 89 Enter Marks 3 : 56
Roll : 10
Name : Kumar Total : 223
#include <stdio.h> int main() { struct address { char phone[10]; char city[25]; char pin[6]; }; struct emp { char name[25]; struct address a; };
struct emp e = {“Aman”, “935324324”, “Gurgaon”, “228001”};
printf (“Name = %s Phone = %s\n”, e.name, e.a.phone);
printf (“City = %s PIN= %s\n”, e.a.city, e.a.pin); return 0;
}
Structures within Structures
OUTPUT
Name = Aman Phone = 935324324
#include <stdio.h> struct student
{
char name[50]; int roll;
};
void display(struct student stud);
int main() {
struct student stud;
printf("Enter student's name: "); scanf("%s", &stud.name);
printf("Enter roll number:"); scanf("%d", &stud.roll); display(stud);
return 0; }
void display(struct student stud){
printf("Output\nName: %s",stud.name); printf("\nRoll: %d",stud.roll);
}
OUTPUT
Enter student's name: Kevin Amla Enter roll number: 149
Output
Union
A union is a special data type available in C that allows to store different data types in the same memory location. We can define a union with many members, but only one member can contain a value at any given time. Unions provide an efficient way of using the same memory location for multiple-purpose.
union Data { int i;
float f;
char str[20]; } data;
Union
#include <stdio.h> #include <string.h> union Data {
int i; float f;
char str[20]; };
int main( ) {
union Data data;
printf( "Memory size occupied by data : %d\n", sizeof(data)); return 0;
}
it produces the following result −
Accessing Union members
it produces the following result − data.i : 1917853763
data.f
41223605803277948604527599943 68.000000
Accessing Union members
it produces the following result − data.i : 10
data.f : 220.500000