0% found this document useful (0 votes)
14 views61 pages

Structure

The document outlines a syllabus on structures in C programming, covering definitions, declarations, initialization, and functions related to structures. It includes multiple-choice questions (MCQs) with solutions that test understanding of structure concepts, syntax, and usage in C. Additionally, practical programming exercises are provided to demonstrate the implementation of structures for various data types and scenarios.

Uploaded by

harshvaghela400
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)
14 views61 pages

Structure

The document outlines a syllabus on structures in C programming, covering definitions, declarations, initialization, and functions related to structures. It includes multiple-choice questions (MCQs) with solutions that test understanding of structure concepts, syntax, and usage in C. Additionally, practical programming exercises are provided to demonstrate the implementation of structures for various data types and scenarios.

Uploaded by

harshvaghela400
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

Syllabus:

Chap 8: Structures
• Structure Definition, Declaration and Initialization of Variables, Accessing Structure
members
• Nested Structure
• Array of Structures and Array within the structure
• Structures and Functions

MCQ:
1. Structure can contain elements of the same data type
A
A. TRUE
B. FALSE
Solution: Structure can contain elements of same datatype and different datatype as well.

2. Which of the following operator is used to select a member of a structure variable


A
A. .(dot)
B. ,(comma)
C. : (colon)
D. ;(semicolon)

3. Array and Structure can contain elements of and data type


respectively.
A
A. same, different
B. different, Same
C. different , different
D. None of above
Solution: Array can contain elements of same datatype only but structure can contain
elements of same or different datatypes, based on program requirements.

Page 1
4. What is the correct syntax to access the value of struct variable book{ price, page }?
A
A. printf("%d%d", [Link], [Link]);
B. printf("%d%d", [Link], [Link]);
C. printf("%d%d", price::book, page::book);
D. printf("%d%d", price->book, page->book);
Solution: we can use dot sign (.) to access simple structure variables.
(Note : When we are suing pointers with structure we can use -> sign but that you will study
in detail in 10th chapter. Here in given case answer is only dot for simple variable).

5. What is the size of a C structure.?


B
A. C structure is always 128 bytes.
B. Size of C structure is the total bytes of all elements of structure.
C. Size of C structure is the size of largest element.
D. None of the above

6. What is the output of C program with structures.?


int main()
{
structure hotel
{
int items;
char name[10];
}a;
strcpy([Link], "TAJ");
[Link]=10;
printf("%s", [Link]);
return 0;
}
C

Page 2
A. TAJ
B. Empty
C. Compiler Error
D. None of the above
Solution:
int main()
{
/*creating structure named as hotel which can be used inside main only because it is declared
in main. But this will give an error because instead of keyword struct we are using
structure.*/
structure hotel
{
int items;
char name[10];
}a;
strcpy([Link], "TAJ");
[Link]=10;
printf("%s", [Link]);
return 0;
}

7. Choose a correct statement about C structure.?


int main()
{
struct ship
{

};
return 0;
}
C

Page 3
A. It is wrong to define an empty structure
B. Member variables can be added to a structure even after its first definition.
C. There is no use of defining an empty structure
D. None of the above
Solution:
When you declare any structure variable, memory will be allocated as per the total size
occupied by each member. If you declare empty structure then its instance can not be given
memory at runtime. So it will through an error that member not declared.

8. Presence of code like “s.t.b = 10” indicates


B
A. Syntax Error
B. nested structure
C. double data type
D. An ordinary variable name
Solution: This can be interpreted as s is an instance of structure which is having another
structure as member and t is an instance of that structure and b is a member of structure of t.
In short this is nesting of structure.
Ex:
#include<stdio.h>
struct temp
{
int a=5;
int b=10;
};
struct stud
{
struct temp t;
};
int main()
{
struct stud s;

Page 4
printf("%d",s.t.b);
}

9. Which of the following cannot be a structure member?


B
A. Another structure
B. Function
C. Array
D. None of the mentioned
Solution: If you write like following then it will through an error.
#include<stdio.h>
int f(int n)
{
return n+10;
}

struct stud
{
int f(5);
};

int main()
{
struct stud s;
printf("%d",s.f(5));
}
You cannot use function as a structure member.

10. Which of the following structure declaration will throw an error?


D

Page 5
A. struct temp
{
}s;
main()
{
}

B. struct temp
{
};
struct temp s;
main()
{
}

C. struct temp s;
struct temp
{
};
main()
{
}

D. None of the mentioned


Solution:
All of the above syntax are allowed in .c files but it may throw error for CPP(c++) files.

11. Choose a correct statement about C structures.


B
A. Structure elements can be initialized at the time of declaration.

Page 6
B. Structure members cannot be initialized at the time of declaration
C. Only integer members of structure can be initialized at the time of declaration
D. None of the above
Solution: This may be accepted in CPP files but not in C that is .c files. Following code will
throw an error in .c file.
#include<stdio.h>

struct temp
{
char name[10] = "komal";
int num = 20;

}a;

int main()
{
prinf("%s",[Link]);
prinf("\n%d",[Link]);
}

Page 7
Practical:
1. WAP to demonstrate simple structure str which is having int, float and char
members and show how can you declare, initialize and access the structure
members.
#include<stdio.h>
struct str
{
int a;
float b;
char c;

};
int main()
{
struct str s; // declaration of structure variable
printf("Enter value of a: ");
scanf("%d", &s.a);
printf("Enter value of b: ");
scanf("%f", &s.b);
printf("Enter value of c: ");
fflush(stdin);
scanf("%c", &s.c);
printf("\nValue of structure s is:");
printf("\na = %d\n b = %f\n c = %c",s.a,s.b,s.c);

2. WAP having structure with following elements: Name of employee, basic, DA,
TA, deduction, net_salary. Display individual elements of structure in proper
format
Method – 1:
#include<stdio.h>

Page 8
struct emp
{
char name[100];
int basic;
int da;
int ta;
int deduction;
int net_salary;
};

int main()
{
struct emp e;
printf("Enter name: ");
gets([Link]);
printf("Enter value of basic: ");
scanf("%d", &[Link]);
printf("Enter value of DA: ");
scanf("%d", &[Link]);
printf("Enter value of TA: ");
scanf("%d", &[Link]);
printf("Enter value of Deduction: ");
scanf("%d", &[Link]);
e.net_salary = [Link]+[Link]+[Link];
printf("\nEmployee Details:");
printf("\n Name\t = %s\n Basic\t = %d\n DA\t = %d\n TA\t = %d\n Deduction\t =
%d\n Net\t = %d",[Link], [Link], [Link], [Link], [Link], e.net_salary); }

Method – 2:
#include <stdio.h>
struct salary

Page 9
{
char name[20];
float basic, da, hra, ta, others;
float pf,gross_salary;
float net_salary;

}sal;
int main (void)
{
//Input basic salary
printf("Enter name of Employe\n");
gets([Link]);
printf("Input Basic Salary: ");
scanf("%f", &[Link]);

//Calculate DA
[Link] = [Link] * 25.0 / 100.0;

//Calculate HRA
[Link] = [Link] * 15.0 / 100.0;

//Calculate gross salary


sal.gross_salary = [Link] + [Link] + [Link];

//Calculate PF
[Link] = sal.gross_salary * 10.0 / 100.0;

//Calculate net salary


sal.net_salary = sal.gross_salary - [Link];

Page 10
//Print result
printf("Basic Salary: %.2f\n", [Link]);
printf("DA: %.2f\n", [Link]);
printf("HRA: %.2f\n", [Link]);
printf("Gross Salary: %.2f\n", sal.gross_salary);
printf("PF: %.2f\n", [Link]);
printf("Net Salary: %.2f", sal.net_salary);

return 0;
}

3. WAP which declares a structure named “student” with rollno, name, address
and marks [5]. The structural element marks should be an array to store marks
in each of the five subjects. The program then should read and print data for one
student using proper format.
#include<stdio.h>
struct student
{
int rollno;
char name[100];
char add[100];
int m[5];
};
int main()
{
struct student s1; // where s1 is a instance of structure student

printf("Enter student details :");


printf("\nEnter Roll number: ");
scanf("%d", &[Link]);
printf("\nEnter Name: ");

Page 11
fflush(stdin);
gets([Link]);
printf("\nEnter Addrss: ");
fflush(stdin);
gets([Link]);
printf("\nEnter Marks: ");
for(int j=0;j<5;j++)
{
printf("\nMarks %d : ", j+1);
scanf("%d", &s1.m[j]);
}

printf("\nStudent details:");
printf("\nRoll no\t: %d", [Link]);
printf("\nName\t: %s", [Link]);
printf("\nAddress\t: %s", [Link]);
for(int j=0;j<5;j++)
{
printf("\nMarks %d : ", j+1);
printf("%d", s1.m[j]);
}

Method 2: If you want to print the sum too


#include<stdio.h>
struct student
{
int rollno;
char name[100];

Page 12
char add[100];
int m[5];
};
int main()
{
struct student s1; // where s1 is a instance of structure student
int total=0;
printf("Enter student details :");
printf("\nEnter Roll number: ");
scanf("%d", &[Link]);
printf("\nEnter Name: ");
fflush(stdin);
gets([Link]);
printf("\nEnter Addrss: ");
fflush(stdin);
gets([Link]);
printf("\nEnter Marks: ");
for(int j=0;j<5;j++)
{
printf("\nMarks %d : ", j+1);
scanf("%d", &s1.m[j]);
}

printf("\nStudent details:");
printf("\nRoll no\t: %d", [Link]);
printf("\nName\t: %s", [Link]);
printf("\nAddress\t: %s", [Link]);
for(int j=0;j<5;j++)
{
printf("\nMarks %d : ", j+1);

Page 13
printf("%d", s1.m[j]);
total = total+ s1.m[j];
}
printf("\nTotal\t: %d", total);

4. WAP which declares a structure named “Student” with rollno, name and
percentage as members. Read data of 5 students from user and display them in
proper format.
#include<stdio.h>
struct student
{
int rollno;
char name[100];
char add[100];
int m[5];
};
int main()
{
struct student s[5]; // where s is an array instance of structure student

int i;
for(i=0; i<5;i++)
{
printf("Enter student details :");
printf("\nEnter Roll number: ");
scanf("%d", &s[i].rollno);
printf("\nEnter Name: ");
fflush(stdin);
gets(s[i].name);

Page 14
printf("\nEnter Addrss: ");
fflush(stdin);
gets(s[i].add);
printf("\nEnter Marks: ");
for(int j=0;j<5;j++)
{
printf("\nMarks %d : ", j+1);
scanf("%d", &s[i].m[j]);
}
}

for(i=0;i<5;i++)
{
printf("\nStudent details:");
printf("\nRoll no\t: %d", s[i].rollno);
printf("\nName\t: %s", s[i].name);
printf("\nAddress\t: %s", s[i].add);
for(int j=0;j<5;j++)
{
printf("\nMarks %d : ", j+1);
printf("%d", s[i].m[j]);
}
}
}

Page 15
5. Write a C program to store information (Name, Roll No., and Marks) of n
students using structure and display in proper format
#include<stdio.h>
struct student
{
int rollno;
char name[100];
int marks;
};

int main()
{
int n;
printf(“Enter value of n - ”);
scanf(“%d”, &n);
struct student s[n]; // where s is an array instance of structure student

int i;
for(i=0; i<n;i++)
{
printf("Enter student details :");
printf("\nEnter Roll number: ");
scanf("%d", &s[i].rollno);
printf("\nEnter Name: ");
fflush(stdin);
gets(s[i].name);
printf("\nEnter Marks: ");
scanf("%d", &s[i].marks);

Page 16
for(i=0;i<n;i++)
{
printf("\nStudent details:");
printf("\nRoll no\t: %d", s[i].rollno);
printf("\nName\t: %s", s[i].name);
printf("\nMarks\t: %d", s[i].marks);

}
}

6. WAP using structure to enter rollno, marks of the three subjects for 3 student
and find total obtained by each student
#include<stdio.h>
struct student
{
int rollno;
char name[100];
char add[100];
int m[3];
};
int main()
{
struct student s[3]; // where s is an array instance of structure student

int i;
int total;
for(i=0; i<3;i++)
{
printf("Enter student details :");
printf("\nEnter Roll number: ");
scanf("%d", &s[i].rollno);

Page 17
printf("\nEnter Name: ");
fflush(stdin);
gets(s[i].name);
printf("\nEnter Addrss: ");
fflush(stdin);
gets(s[i].add);
printf("\nEnter Marks: ");
for(int j=0;j<3;j++)
{
printf("\nMarks %d : ", j+1);
scanf("%d", &s[i].m[j]);
}
}

for(i=0;i<3;i++)
{
printf("\nStudent details:");
printf("\nRoll no\t: %d", s[i].rollno);
printf("\nName\t: %s", s[i].name);
printf("\nAddress\t: %s", s[i].add);
total = 0;
for(int j=0;j<3;j++)
{
printf("\nMarks %d : ", j+1);
printf("%d", s[i].m[j]);
total = total + s[j].m[j];
}
printf("\nTotal\t: %d", total);
}
}

Page 18
7. Define structure data type called time_struct containing three member’s integer
hour, integer minute and integer second. Develop a program that would assign
values to the individual number and display the time in the following format: 16:
40:51
Method – 1:
#include<stdio.h>
struct time_struct
{
int h;
int m;
int s;
};

int main()
{
struct time_struct t1; // where s is an array instance of structure

printf("\nEnter hour :");


scanf("%d", &t1.h);

printf("\nEnter min :");


scanf("%d", &t1.m);

printf("\nEnter min :");


scanf("%d", &t1.s);

printf("\nTime entered is - ");


printf("%d:%d:%d", t1.h,t1.m,t1.s);
}

Method – 2:

Page 19
#include<stdio.h>
#include<conio.h>

//Declaration of structure time_struct


struct time_struct{
int hour,minute,second;
}time;
//Member time declared

void main()
{
int i;
//Data input from user
printf("\nEnter the time in hour, minute and second format");
scanf("%d%d%d",&[Link],&[Link],&[Link]);

//Formating of the inserted data


if([Link]>60 || [Link]>60)
printf("\nPlease enter valid time format");
else
{
printf("\nEntered Time is :");
if([Link]<=9 && [Link]<=9 && [Link]<=9)
printf("\n0%d:0%d:0%d",[Link],[Link],[Link]);
else if([Link]<=9 && [Link]>9 && [Link]>9)
printf("\n0%d:%d:%d",[Link],[Link],[Link]);
else if([Link]>9 && [Link]<=9 && [Link]>9)
printf("\n%d:0%d:%d",[Link],[Link],[Link]);
else if([Link]>9 && [Link]>9 && [Link]<=9)
printf("\n%d:%d:0%d",[Link],[Link],[Link]);

Page 20
else if([Link]<=9 && [Link]<=9 && [Link]>9)
printf("\n0%d:0%d:%d",[Link],[Link],[Link]);
else if([Link]<=9 && [Link]>9 && [Link]<=9)
printf("\n0%d:%d:0%d",[Link],[Link],[Link]);
else if([Link]>9 && [Link]<=9 && [Link]<=9)
printf("\n%d:0%d:0%d",[Link],[Link],[Link]);
else
printf("\n%d:%d:%d",[Link],[Link],[Link]);
}
}

8. Write structure that named book which consists of three data fields, namely
name, price and pages. How can we access structure variables?

#include<stdio.h>
struct book
{
char name[100];
float price;
int page;
};
int main()
{
struct book b;

printf("\nEnter Book name :");


gets([Link]);

printf("\nEnter Book price :");


scanf("%f", &[Link]);

Page 21
printf("\nEnter Book pages :");
scanf("%d", &[Link]);

printf("\nBook details - ");


printf("%s : %f : %d", [Link], [Link], [Link]);
}

9. Write a program using structure to store student marks details. Use a function to
calculate total and percentage of this student and display the same.
#include<stdio.h>
struct student
{
int rollno;
char name[100];
int m[3];
};

int total(struct student a)


{
int k, sum = 0;
for(k=0;k<3;k++)
{
sum = sum + a.m[k];
}
return sum;
}

float per(int b)
{
return (float)b/3;
}

Page 22
int main()
{
struct student s[3]; // where s is an array instance of structure student

int i;
for(i=0; i<3;i++)
{
printf("Enter student details :");
printf("\nEnter Roll number: ");
scanf("%d", &s[i].rollno);
printf("\nEnter Name: ");
fflush(stdin);
gets(s[i].name);
printf("\nEnter Marks: ");
for(int j=0;j<3;j++)
{
printf("\nMarks %d : ", j+1);
scanf("%d", &s[i].m[j]);
}
}

for(i=0;i<3;i++)
{
printf("\nStudent details:");
printf("\nRoll no\t: %d", s[i].rollno);
printf("\nName\t: %s", s[i].name);
printf("\nTotal %d : ", total(s[i]));
printf("\nResutl %.2f : ", per(total(s[i])));
}
}

Page 23
10. Write a program that demonstrate the Nested structure.
#include<stdio.h>
struct Address{
char Apartment[500];
char Street[500];
char City[100];
char State[100];
int Zipcode;
};

//Declaring outer and inter structures//


struct Person//Main Structure//
{
char Name[500];
int Age;
char Gender;
struct Address a[20];
char temp;//To clear buffer//
}p[20];//Main Structure Variable//

int main(){
//Declaring variable for For loop//
int i;
//Reading User I/p//
for (i=1;i<3;i++){//Declaring function to accept 2 people's data//
printf("Enter the Name of person %d : ",i);
gets(p[i].Name);
printf("Enter the Age of person %d : ",i);
scanf("%d",&p[i].Age);
scanf("%c",&p[i].temp);//Clearing Buffer//

Page 24
printf("Enter the Gender of person %d : ",i);
scanf("%c",&p[i].Gender);
scanf("%c",&p[i].temp);//Clearing Buffer//
printf("Enter the City of person %d : ",i);
gets(p[i].a[i].City);
printf("Enter the State of person %d : ",i);
gets(p[i].a[i].State);
printf("Enter the Zip Code of person %d : ",i);
scanf("%d",&p[i].a[i].Zipcode);
scanf("%c",&p[i].temp);//Clearing Buffer//
}
//Printing O/p//
for (i=1;i<3;i++){
printf("The Name of person %d is : %s\n",i,p[i].Name);
printf("The Age of person %d is : %d\n",i,p[i].Age);
printf("The Gender of person %d is : %c\n",i,p[i].Gender);
printf("The City of person %d is : %s\n",i,p[i].a[i].City);
printf("The State of person %d is : %s\n",i,p[i].a[i].State);
printf("The Zip code of person %d is : %d\n",i,p[i].a[i].Zipcode);
}
}

Method 2:
#include<stdio.h>
//Declaring outer and inter structures//
struct Person//Main Structure//{
char Name[500];
int Age;
char Gender;
char temp;//To clear buffer//

Page 25
struct Address//Nested Structure//{
char Apartment[500];
char Street[500];
char City[100];
char State[100];
int Zipcode;
}a[20];//Nested Structure Variable//
}p[20];//Main Structure Variable//
void main(){
//Declaring variable for For loop//
int i;
//Reading User I/p//
for (i=1;i<3;i++){//Declaring function to accept 2 people's data//
printf("Enter the Name of person %d : ",i);
gets(p[i].Name);
printf("Enter the Age of person %d : ",i);
scanf("%d",&p[i].Age);
scanf("%c",&p[i].temp);//Clearing Buffer//
printf("Enter the Gender of person %d : ",i);
scanf("%c",&p[i].Gender);
scanf("%c",&p[i].temp);//Clearing Buffer//
printf("Enter the City of person %d : ",i);
gets(p[i].a[i].City);
printf("Enter the State of person %d : ",i);
gets(p[i].a[i].State);
printf("Enter the Zip Code of person %d : ",i);
scanf("%d",&p[i].a[i].Zipcode);
scanf("%c",&p[i].temp);//Clearing Buffer//
}
//Printing O/p//

Page 26
for (i=1;i<3;i++){
printf("The Name of person %d is : %s\n",i,p[i].Name);
printf("The Age of person %d is : %d\n",i,p[i].Age);
printf("The Gender of person %d is : %c\n",i,p[i].Gender);
printf("The City of person %d is : %s\n",i,p[i].a[i].City);
printf("The State of person %d is : %s\n",i,p[i].a[i].State);
printf("The Zip code of person %d is : %d\n",i,p[i].a[i].Zipcode);
}
}

Page 27
Some advance practical of structures
1. C Program to Add Two Distances (in inch-feet system) using Structures.
#include <stdio.h>
struct Distance {
int feet;
float inch;
} d1, d2, result;

int main() {
// take first distance input
printf("Enter 1st distance\n");
printf("Enter feet: ");
scanf("%d", &[Link]);
printf("Enter inch: ");
scanf("%f", &[Link]);

// take second distance input


printf("\nEnter 2nd distance\n");
printf("Enter feet: ");
scanf("%d", &[Link]);
printf("Enter inch: ");
scanf("%f", &[Link]);

// adding distances
[Link] = [Link] + [Link];
[Link] = [Link] + [Link];

// convert inches to feet if greater than 12


while ([Link] >= 12.0) {
[Link] = [Link] - 12.0;
++[Link];
}

Page 28
printf("\nSum of distances = %d\'-%.1f\"", [Link], [Link]);
return 0;
}

Output:
Enter 1st distance
Enter feet: 23
Enter inch: 8.6

Enter 2nd distance


Enter feet: 34
Enter inch: 2.4

Sum of distances = 57'-11.0"

2. C Program to Add Two Complex Numbers by Passing Structure to a Function.


#include <stdio.h>
struct complex {
float real;
float imag;
} complex;

complex add(complex n1, complex n2);


int main() {
complex n1, n2, result;
printf("For 1st complex number \n");
printf("Enter the real and imaginary parts: ");
scanf("%f %f", &[Link], &[Link]);
printf("\nFor 2nd complex number \n");
printf("Enter the real and imaginary parts: ");
scanf("%f %f", &[Link], &[Link]);

Page 29
result = add(n1, n2);
printf("Sum = %.1f + %.1fi", [Link], [Link]);
return 0;
}

complex add(complex n1, complex n2) {


complex temp;
[Link] = [Link] + [Link];
[Link] = [Link] + [Link];
return (temp);
}
Output:
For 1st complex number
Enter the real and imaginary parts: 2.1
-2.3

For 2nd complex number


Enter the real and imaginary parts: 5.6
23.2
Sum = 7.7 + 20.9i

3. C Program to Calculate Difference Between Two Time Periods using structure.


//C Program to Calculate Difference Between Two Time Periods using structure.
#include <stdio.h>
struct Time
{
int sec,min,hr;
};

int main()

Page 30
{
struct Time difference(struct Time, struct Time);
struct Time start,stop,diff;
printf("Enter Start time hr min sec : ");
scanf("%d%d%d",&[Link],&[Link],&[Link]);

printf("Enter Stop time hr min sec : ");


scanf("%d%d%d",&[Link],&[Link],&[Link]);

diff=difference(start,stop);
printf("Difference : %d :%d : %d\n",[Link],[Link],[Link]);
}
struct Time difference(struct Time a, struct Time b)
{
struct Time diff;
while([Link]>[Link])
{
--[Link];
[Link]+=60;
}
while([Link]>[Link])
{
--[Link];
[Link]+=60;
}
printf("Start Time : %d : %d : %d\n",[Link],[Link],[Link]);
printf("Stop Time : %d : %d : %d\n",[Link],[Link],[Link]);

[Link]=[Link] - [Link];
[Link]=[Link] - [Link];

Page 31
[Link]=[Link] - [Link];
return diff;
}

Output:
Enter the start time.
Enter hours, minutes and seconds: 13
34
55
Enter the stop time.
Enter hours, minutes and seconds: 8
12
15
Time Difference: 13:34:55 - 8:12:15 = 5:22:40

4. Find area of rectangle using Structure and Function in C.


#include<stdio.h>
struct Rectangle
{
int width;
int height;
};

void areaOfRectangle(struct Rectangle r)


{
printf("Area of Rectangle is: %d ", [Link]*[Link]);
}

int main()
{

Page 32
struct Rectangle rec;
printf("Enter width : ");
scanf("%d", &[Link]);
printf("Enter height : ");
scanf("%d", &[Link]);
areaOfRectangle(rec);
}

5. Consider there are two structures Employee (depended structure) and another
structure called Organisation (Outer structure).
The structure Organisation has the data members like
organisation_name,organisation_number.
The Employee structure is nested inside the structure Organisation and it has the data
members like employee_id, name, salary.
Display the details of organization with its employees using nested stricture.
Method 1:
// C program to implement
// the above approach
#include <stdio.h>
#include <string.h>

// Declaration of the
// dependent structure
struct Employee
{
int employee_id;
char name[20];
int salary;
};

// Declaration of the

Page 33
// Outer structure
struct Organisation
{
char organisation_name[20];
char org_number[20];

// Dependent structure is used


// as a member inside the main
// structure for implementing
// nested structure
struct Employee emp;
};

// Driver code
int main()
{
// Structure variable
struct Organisation org;

// Print the size of organisation


// structure
printf("The size of structure organisation : %ld\n", sizeof(org));
[Link].employee_id = 101;
strcpy([Link], "Robert");
[Link] = 400000;
strcpy(org.organisation_name, "LJU");
strcpy(org.org_number, "GFG123768");
// Printing the details
printf("Organisation Name : %s\n", org.organisation_name);
printf("Organisation Number : %s\n", org.org_number);

Page 34
printf("Employee id : %d\n", [Link].employee_id);
printf("Employee name : %s\n", [Link]);
printf("Employee Salary : %d\n", [Link]);
}

Output:
The size of structure organisation : 68
Organisation Name : LJU
Organisation Number : GFG123768
Employee id : 101
Employee name : Robert
Employee Salary : 400000

Method 2:

// C program to implement
// the above approach
#include <stdio.h>
#include <string.h>

// Declaration of the main


// structure
struct Organisation
{
char organisation_name[20];
char org_number[20];

// Declaration of the dependent


// structure
struct Employee

Page 35
{
int employee_id;
char name[20];
int salary;

// variable is created which acts


// as member to Organisation structure.
} emp;
};

// Driver code
int main()
{
struct Organisation org;

// Print the size of organisation


// structure
printf("The size of structure organisation : %ld\n", sizeof(org));
[Link].employee_id = 101;
strcpy([Link], "Robert");
[Link] = 400000;
strcpy(org.organisation_name, "LJU");
strcpy(org.org_number, "GFG123768");
// Printing the details
printf("Organisation Name : %s\n", org.organisation_name);
printf("Organisation Number : %s\n", org.org_number);
printf("Employee id : %d\n", [Link].employee_id);
printf("Employee name : %s\n", [Link]);
printf("Employee Salary : %d\n", [Link]);
}

Page 36
Output:
The size of structure organisation : 68
Organisation Name : LJU
Organisation Number : GFG123768
Employee id : 101
Employee name : Robert
Employee Salary : 400000

Method 3:
Passing nested structure to function - Pass the nested structure variable at once.

// C program to implement
// the above approach
#include <stdio.h>
// Declaration of the inner
// structure
struct Employee
{
int employee_id;
char name[20];
int salary;
};

// Declaration of the Outer


// structure
struct Organisation
{
char organisation_name[20];
char org_number[20];

Page 37
// Nested structure
struct Employee emp;
};

// Function show is expecting


// variable of outer structure
void show(struct Organisation);

// Driver code
int main()
{
struct Organisation org = {"LJU", "GFG111", {278, "Paul",5000}};
// Organisation structure variable
// is passed to function show
show(org);
}

// Function shoe definition


void show(struct Organisation org )
{
// Printing the details
printf("Printing the Details :\n");
printf("Organisation Name : %s\n", org.organisation_name);
printf("Organisation Number : %s\n", org.org_number);
printf("Employee id : %d\n", [Link].employee_id);
printf("Employee name : %s\n", [Link]);
printf("Employee Salary : %d\n", [Link]);
}

Page 38
Output:
Printing the Details :
Organisation Name : LJU
Organisation Number : GFG111
Employee id : 278
Employee name : Paul
Employee Salary : 5000

Method 4: - Pass the nested structure members as arguments into the function:
// C program to implement
// the above approach
#include <stdio.h>
// Declaration of the inner
// structure
struct Employee
{
int employee_id;
char name[20];
int salary;
};
// Declaration of the Outer
// structure
struct Organisation
{
char organisation_name[20];
char org_number[20];
// Nested structure
struct Employee emp;
};
// Function show is expecting

Page 39
// members of both structures
void show(char organisation_name[],
char org_number[],
int employee_id,
char name[], int salary);
// Driver code
int main()
{
struct Organisation org = {"LJU", "GFG111", {278, "Paul",5000}};
// Data members of both the structures
// are passed to the function show
show(org.organisation_name, org.org_number, [Link].employee_id, [Link],
[Link]);
}

// Function show definition


void show(char organisation_name[],
char org_number[],
int employee_id,
char name[], int salary)
{
// Printing the details
printf("Printing the Details :\n");
printf("Organisation Name : %s\n", organisation_name);
printf("Organisation Number : %s\n", org_number);
printf("Employee id : %d\n", employee_id);
printf("Employee name : %s\n", name);
printf("Employee Salary : %d\n", salary);
}

Page 40
Output:
Printing the Details :
Organisation Name : LJU
Organisation Number : GFG111
Employee id : 278
Employee name : Paul
Employee Salary : 5000

6. Write a Program to create Student Structure and accept name, roll no and marks of 3
subjects and print it in descending order of their total result. (Note: One can also
modify it by taking roll number as key and print student name in ascending order roll
number wise).
#include<stdio.h>
#include<conio.h>
struct student
{
char name[30];
int rollno;
int sub[3];
int total;
};

int main()
{
int i, n, j;
struct student st[20], temp;
printf("Enter number of students data you want to enter:\n");
scanf("%d",&n);
for(i=0;i < n;i++)
{
printf("Enter name of student %d\n",(i+1));
scanf("%s",&st[i].name);
printf("Enter Roll No of student %d\n",(i+1));
scanf("%d",&st[i].rollno);
printf("Enter marks for 3 subjects of student %d\n",(i+1));
scanf("%d%d%d",&st[i].sub[0],&st[i].sub[1],&st[i].sub[2]);
st[i].total = (st[i].sub[0]+st[i].sub[1]+st[i].sub[2]);
printf("Total Marks of %d student = %d\n",(i+1), st[i].total);
}
for(i=0;i < (n-1);i++)
{
for(j=0;j < (n-i-1);j++)

Page 41
Qb-1
#include<stdio.h>
#include<conio.h>
struct student
{
int rollno;
char name[20];
char addr[50];
int marks[5]; //array
};
void main()
{
struct student stu; //structure variable declare
int i,sum=0;
printf("Enter roll no: \n");
scanf("%d",&[Link]);
fflush(stdin);
printf("Enter name: \n");
gets([Link]);
printf("Enter address: \n");
fflush(stdin);
gets([Link]);
printf("Enter 5 subjects marks \n");
for(i=0;i<5;i++)
{
scanf("%d",&[Link][i]);
sum=sum+[Link][i];
}
printf("Entered details are as below:\n");
printf("%d\n",[Link]);
printf("%s\n",[Link]);
printf("%s\n",[Link]);
for(i=0;i<5;i++)
{
printf("%d\n",[Link][i]);
}
printf("Total is %d\n",sum);

}
QB-2 Program for Storing information
#include <stdio.h>
struct student {
char firstName[50];
int roll;
float marks;
} s[5];

int main() {
int i;
printf("Enter information of students:\n");

// storing information
for (i = 0; i < 10; i++) {
s[i].roll = i + 1;
printf("\nFor roll number%d,\n", s[i].roll);
printf("Enter first name: ");
scanf("%s", s[i].firstName);
printf("Enter marks: ");
scanf("%f", &s[i].marks);
}
printf("Displaying Information:\n\n");

// displaying information
for (i = 0; i < 10; i++) {
printf("\nRoll number: %d\n", i + 1);
printf("First name: ");
puts(s[i].firstName);
printf("Marks: %.1f", s[i].marks);
printf("\n");
}
return 0;
}
QB-3 Program for Storing information
#include <stdio.h>
struct student {
char firstName[50];
int roll;
float per;
} s[5];

int main() {
int i;
printf("Enter information of students:\n");

// storing information
for (i = 0; i < 5; i++) {
s[i].roll = i + 1;
printf("\nFor roll number%d,\n", s[i].roll);
printf("Enter first name: ");
scanf("%s", s[i].firstName);
printf("Enter Percentage: ");
scanf("%f", &s[i].per);
}
printf("Displaying Information:\n\n");

// displaying information
for (i = 0; i < 5; i++) {
printf("\nRoll number: %d\n", i + 1);
printf("First name: ");
puts(s[i].firstName);
printf("Percentage: %.1f", s[i].per);
printf("\n");
}
return 0;
}
Qb-4 Program to create Student Structure and accept name, roll no and marks of 3
subjects and print in descending order

#include<stdio.h>
#include<conio.h>
struct student
{
char name[30];
int rollno;
int sub[3];
int total;
};

void main()
{
int i, n, j;
struct student st[20], temp;
printf("Enter number of students data you want to enter:\n");
scanf("%d",&n);
for(i=0;i < n;i++)
{
printf("Enter name of student %d\n",(i+1));
scanf("%s",&st[i].name);
printf("Enter Roll No of student %d\n",(i+1));
scanf("%d",&st[i].rollno);
printf("Enter marks for 3 subjects of student %d\n",(i+1));
scanf("%d%d%d",&st[i].sub[0],&st[i].sub[1],&st[i].sub[2]);
st[i].total = (st[i].sub[0]+st[i].sub[1]+st[i].sub[2]);
printf("Total Marks of %d student = %d\n",(i+1), st[i].total);
}
for(i=0;i < (n-1);i++)
{
for(j=0;j < (n-i-1);j++)
{
if(st[j].total < st[j+1].total)
{
temp = st[j];
st[j] = st[j+1];
st[j+1] = temp;
}
}
}
printf("\n\n\n\t\t******Sorted in descending order******");
for(i=0; i < n;i++)
{
printf("\nName of student: %s",st[i].name);
printf("\nRoll No of student: %d",st[i].rollno);
printf("\nTotal of student: %d\n",st[i].total);
}
getch();
}
Qb-5- Define a structure data type called time_struct containing three member’s integer
hours, minutes, second. Develop a program that would assign values to individual
member and display the time in following format : HH:MM:SS
#include<stdio.h>
#include<conio.h>

//Declaration of structure time_struct


struct time_struct{
int hour,minute,second;
}time;
//Member time declared

void main()
{
int i;

//Data input from user


printf("\nEnter the time in hour, minute and second format");
scanf("%d%d%d",&[Link],&[Link],&[Link]);

//Formating of the inserted data


if([Link]>60 || [Link]>60)
printf("\nPlease enter valid time format");
else
{
printf("\nEntered Time is :");
if([Link]<=9 && [Link]<=9 && [Link]<=9)
printf("\n0%d:0%d:0%d",[Link],[Link],[Link]);
else if([Link]<=9 && [Link]>9 && [Link]>9)
printf("\n0%d:%d:%d",[Link],[Link],[Link]);
else if([Link]>9 && [Link]<=9 && [Link]>9)
printf("\n%d:0%d:%d",[Link],[Link],[Link]);
else if([Link]>9 && [Link]>9 && [Link]<=9)
printf("\n%d:%d:0%d",[Link],[Link],[Link]);
else if([Link]<=9 && [Link]<=9 && [Link]>9)
printf("\n0%d:0%d:%d",[Link],[Link],[Link]);
else if([Link]<=9 && [Link]>9 && [Link]<=9)
printf("\n0%d:%d:0%d",[Link],[Link],[Link]);
else if([Link]>9 && [Link]<=9 && [Link]<=9)
printf("\n%d:0%d:0%d",[Link],[Link],[Link]);
else
printf("\n%d:%d:%d",[Link],[Link],[Link]);
}}
Qb-6
#include <stdio.h>
struct salary
{
char name[20];
float basic, da, hra, ta, others;
float pf,gross_salary;
float net_salary;

}sal;
int main (void)
{

//Input basic salary


printf("Enter name of Employe\n");
gets([Link]);
printf("Input Basic Salary: ");
scanf("%f", &[Link]);
//Calculate DA
[Link] = [Link] * 25.0 / 100.0;

//Calculate HRA
[Link] = [Link] * 15.0 / 100.0;

//Calculate gross salary


sal.gross_salary = [Link] + [Link] + [Link];

//Calculate PF
[Link] = sal.gross_salary * 10.0 / 100.0;

//Calculate net salary


sal.net_salary = sal.gross_salary - [Link];

//Print result
printf("Basic Salary: %.2f\n", [Link]);
printf("DA: %.2f\n", [Link]);
printf("HRA: %.2f\n", [Link]);
printf("Gross Salary: %.2f\n", sal.gross_salary);
printf("PF: %.2f\n", [Link]);
printf("Net Salary: %.2f", sal.net_salary);

return 0;
}
Qb-7
#include<stdio.h>
struct student
{
char name[50];
int sub1;
int sub2;
int sub3,total;
float per;

};
void display(struct student s1);
void main()
{
struct student s1;

printf("Enter name: ");


fflush(stdin);
gets([Link]);
printf("Enter marks: ");
scanf("%d%d%d",&s1.sub1,&s1.sub2,&s1.sub3);

display(s1); // passing struct as an argument


}

// function prototype

void display(struct student s1) {


printf("\nDisplaying information\n");
printf("Name: %s", [Link]);
printf("\nMarks: %d %d %d", s1.sub1,s1.sub2,s1.sub3);
[Link]=s1.sub1+s1.sub2+s1.sub3;

[Link]=[Link]/3;
printf("\nPercentage: %f", [Link]);
}
{
if(st[j].total < st[j+1].total)
{
temp = st[j]; st[j] = st[j+1]; st[j+1] = temp;
}
}
}
printf("\n\n\n\t\t******Sorted in descending order******");for(i=0; i < n;i++)
{
printf("\nName of student: %s",st[i].name); printf("\nRoll No of student:
%d",st[i].rollno);printf("\nTotal of student: %d\n",st[i].total);
}

Try yourself:

7. Define a struct to store name, array marks[5] and a character grade. Write a program to display the
details of student whose name is entered by user.

8. Define a nested struct with following member


Rollno, name (nested struct member as first name, middle name, last name), dob(nested struct members as
day, month, year), marks of 4 subjects.

a) Write a program to display details of student with given DOB


b) For above display student and class average
c) Delete record of students with given first name

9. Declare a structure Point. Input the coordinates of a point variable and determine the quadrant in which it lies.
Q1 --> x=positive y=positive Q2 --> x=negative
y=positive Q3 --> x=negative y=negative Q4 -->
x=positive y=negative

Page 42

Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner

You might also like