Skip to main content

Practical-13

Write a program using structure within Function.

Introduction

In this program, we will learn to use a structure within a function.

Algorithm

  1. Start
  2. Declare a structure called details, and it stores information about a student.
  3. Declare a function, findResult, that will take the details of a student and print some information.
  4. Intiliaze the student details.
  5. Call the function to get the result.
  6. End

Code

Practical-13.c

#include <stdio.h>

/* This is a structure, called details, and it stores information about a student */
struct details
{
int age;
int year;
char name[20];
};

/* Function, findResult, will take the details of a student and print some information */
void findResult(struct details student_info)
{
printf("Student's Name: %s \n", student_info.name);
printf("Age of the student is: %d \n", student_info.age);
printf("The student is in year: %d \n", student_info.year);
}

void main()
{
/* Declaring variables of structure details */
struct details student1;
struct details student2;

/* Intiliazing the student details */
student1.age = 5;
student1.year = 1;
strcpy(student1.name, "Itachi");

student2.age = 10;
student2.year = 5;
strcpy(student2.name, "Naruto");

/* Calling the function to get the result */
findResult(student1);
printf("\n");
findResult(student2);


}

Output

Student's Name: Itachi
Age of the student is: 5
The student is in year: 1

Student's Name: Naruto
Age of the student is: 10
The student is in year: 5
Explanation

In this program, we have defined a structure called details. This structure stores information about a student. We have defined a function called findResult. This function takes the details of a student and prints some information. We have initialized the details of two students and called the function to get the result.