C Program to Store Information (Name, ID and Salary) of a Employee Using Structure
Objective
Write a C program to store basic information of an Employee using a Structure.
Algorithm / Approach
- Define a
struct Employeecontaining an integerid, a character arrayname[50], and a floatsalary. - In
main(), declare a structure variable:struct Employee s;. - Read the data from the user using the dot operator (e.g.,
&s.idands.name). - Print the data using the dot operator.
main.c
#include<stdio.h>
struct Employee{
int id;
char name[50];
float salary;
};
int main( ) {
struct Employee s;
printf("Enter Info. of Emp :\n");
printf("Enter the ID : ");
scanf("%d",&s.id);
printf("Enter Name : ");
scanf("%s",s.name);
printf("Enter salary : ");
scanf("%f",&s.salary);
printf("Display Info. : \n");
printf("Name : %s\n",s.name);
printf("Id : %d\n",s.id);
printf("Sal : %f\n", s.salary);
return 0;
}
Expected Output
Enter information of Emp : Enter the ID : 39 Enter name : xyz Enter Salary : 30000 Display Info. : Name : xyz Id : 39 Salary : 30000.0000
Explanation of the Program
- Unlike an Array which can only hold one data type (like all integers), a Structure allows you to group different data types together under a single name.
- The Dot Operator (
.) is used to access the individual members of the structure. For example,s.idmeans "the id variable inside the structure s".
Complexity
Time Complexity
O(1)
Space Complexity
O(1)