Skip to main content

ProwessApps

Learn · Practice · Excel

C Program to Store Information (Name, ID and Salary) of a Employee Using Structure

C Code Example — Structure Programs

ADVERTISEMENT

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

  1. Define a struct Employee containing an integer id, a character array name[50], and a float salary.
  2. In main(), declare a structure variable: struct Employee s;.
  3. Read the data from the user using the dot operator (e.g., &s.id and s.name).
  4. 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.id means "the id variable inside the structure s".

Complexity

Time Complexity O(1)
Space Complexity O(1)
ADVERTISEMENT