Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Programming in C language – B.A.

1st Year (Computer Application)


UNIT- 4

Structure Array of Structure


Example-
Declaration of Structure and variable of structure- #include<stdio.h>
void main()
Syntax- {
struct structure_name struct student
{ {
data_type variable_name; int roll_no;
data_type variable_name; char name[20];
data_type variable_name; float percent;
}; };
Struct structure_name variable name; struct student t[5];

+++++++++++++++++++++++++++++++++++++++++++++++ int i;
Example- Declaration of Structure, initialization of for(i=0;i<=4;i++)
structure and accessing of structure element {
printf("Enter Students details like roll no, name,
#include<stdio.h> percent");
void main() scanf("\n%d%s%f",&t[i].roll_no,&t[i].name,
{ t[i].percent);
//defining a union }
struct student
{ printf("Students information are as: RollNo,
int rollno; Name,Percent");
char name[30]; for(i=0;i<=4;i++)
float percent; {
}; printf("\n%d%s%f",t[i].roll_no,t[i].name,
struct student t={101,"Ram",75.5};//initialization of t[i].percent);
structure }
//accessing structure element
printf("Student details are:\n"); }
printf("%d\n",t.rollno); ++++++++++++++++++++++++++++++++++++++++
printf("%s\n",t.name);
printf("%f\n",t.percent);
}

Output- Student details are:


101
Ram
75.50
+++++++++++++++++++++++++++++++++++++

Prof. Gaurav Gupta


Programming in C language – B.A. 1st Year (Computer Application)
Nested Structure- Union
Syntax-
#include<stdio.h> union structure_name
struct outer {
{ data_type variable_name;
int rollno; data_type variable_name;
struct inner data_type variable_name;
{ };
float percent; union union_name variable name;
};
struct inner t1; //tag of inner structure Example of Union-
};
struct outer t2; //tag of outer structure #include<stdio.h>
#include<string.h>
void main() void main()
{ {
t2.rollno=1024; //defining a union
t2.t1.percent=75.5; union student
{
printf("\n Roll No of Student: %d",t2.rollno); int rollno;
printf("\n Percentage of Student: %f",t2.t1.percent) char name;
} float percent;
};
Output- union student t; // tag of union
Roll No of Student: 1024 // assigning values to t union variable
Percentage of Student: 75.500000 printf("Student details are:\n");
++++++++++++++++++++++++++++++++++
t.rollno=101;
printf("%d\n",t.rollno);

t.name='R';
printf("%c\n",t.name);

t.percent=75.5;
printf("%f\n",t.percent);
}

Output- Students details are:


101
R
75.500000
++++++++++++++++++++++++++++++++++++++

Prof. Gaurav Gupta


Programming in C language – B.A. 1st Year (Computer Application)
Difference between Structure and Union
Example-
#include <stdio.h>
union unionstudent
{
//defining a union
int rollno;
char name[32];
float percent;

} uStudent;

struct structurestudent
{
//defining a union
int rollno;
char name[32];
float percent;
} sStudent;

void main()
{
printf("size of union = %d bytes", sizeof(uStudent));
printf("\nsize of structure = %d bytes", sizeof(sStudent));
}

Output-
size of union = 32
size of structure = 40
+++++++++++++++++++++++++++++++++++++++

Prof. Gaurav Gupta

You might also like