C Program to Read and Print an Employee's Detail Using Structure
A structure is a composite data type in the C programming language that enables to organize variables of various data kinds under a single name. It resembles a class in object-oriented programming but lacks the member functions and related methods. A collection of connected data, which can be of different data kinds, is represented by a structure in C. Real-world entities or data structures are frequently represented by structures.
Syntax:
The following is the syntax in C for defining a structure:
struct structure_name {
// member variables of different data types
data_type1 member1;
data_type2 member2;
// ...
data_typeNmemberN;
};
Let's examine each component of a structure in C in more detail:
- Name of the structure: The name of the structure is user-defined, and it acts as the type name for variables of the structure type. In the above code, the name of the structure is represented by `structure_name`.
- Member Variables: Member variables, usually referred to as fields or members, are the variables that have been defined inside the structure. They may be of any data type, including user-defined data types like other structures or arrays and primitive data types like int, float, double, and char. In the above code example, the member variables are represented by ` member1` and ` member2`, and their data types as `data_type1` and `data_type2`, etc.
- Accessing structure members: With the dot (.) operator, you can access the members after the structure has been defined.
Let’s take a small example code below where we define structure Point with two int variables, x and y. In addition, we have used the (.) operator to assign the value of x and y.
struct Point {
int x;
int y;
};
struct Point p1; // Declaration of a variable of type structure Point
p1.x = 10; // Assigning a value to member x of p1
p1.y = 20; // Assigning a value to member y of p1
In the above example, you can notice that we have used p1.x i.e., (.) operator to assign values.
- Memory allocation: A structural variable's memory is allocated as a single, contiguous block of memory, with each member stored in a different position.
Now, let’s come to our question, which asks us to create a structure datatype in C for Employee.
For a structure datatype for an Employee, it can consist of variables like employee id, employee name, employee age, and employee salary.
Let’s create a structure Employee is defined with the following members:
- empId (integer): This represents the employee ID of type int.
- empName (character array): This represents the employee's name, which is an array of characters (i.e., a string) with a maximum size of 50 characters.
- empAge (integer): This represents the employee age of type int.
- empSalary (float): This represents the employee salary of type float.
Based on the above description, our structure code would look something like this:
struct Employee {
int empId;
char empName[50];
int empAge;
float empSalary;
};
Now, after defining the structure, we need to create the structure object and give some object name. The syntax for object creation looks like this:
Syntax:
struct Employee emp1;
Now, after creating the object, we need to store the values in the respective variables of the structure object. The structure object's members can be accessed and used to store values using the dot (.) operator.
The syntax for assigning value to the struct variable using the (.) dot operator looks like:
object_name.variable_name= value;
Say for example:
emp1.empId = 1001;
C Program
On combining the entire thing, our program would look like this:
#include <stdio.h>
#include <string.h>
struct Employee {
int empId;
char empName[50];
int empAge;
float empSalary;
};
int main() {
struct Employee emp1; // Declare a structure object
// Store values in the structure object
emp1.empId = 1001;
strcpy(emp1.empName, "John Doe");
emp1.empAge = 30;
emp1.empSalary = 5000.0;
// Display the stored values
printf("Employee ID: %d\n", emp1.empId);
printf("Employee Name: %s\n", emp1.empName);
printf("Employee Age: %d\n", emp1.empAge);
printf("Employee Salary: %.2f\n", emp1.empSalary);
return 0;
}
Output:
Employee ID: 1001
Employee Name: John Doe
Employee Age: 30
Employee Salary: 5000.00
If you notice the program above, we have used the strcpy() function. The strcpy() function is used to copy a string (i.e., the employee's name) to the character array member empName of the structure object emp1. You need to include the string.h header to use the strcpy() function.
Conclusion
So, in this article, we learn about structure in C. We code a structure object Employee to manage employee details using structures in C programming language.