16.1 – struct type in C language

by subbu on September 13, 2014

Need of user defined types

As we know any variable belongs to primitive types is capable to store a single value, If we want to store the details of any real time object like a student, product, place or an employee then need to declare number of variables of different types. Say for example, to store the details of a product we need to declare

int pcode;                /* to store product code  */
char pname[20];      /* to store product name */
float pprice;             /* to store purchse price */
float tax;                /* to store % of tax        */
float sprice;             /* to store selling price   */

With the above example we come to know that, it is needed to declare 5 different variables to store the details of a single product.

We need more and more variables to handle the details of more and more products. Say for example we nearly need 50 variables to handle the details of 10 products. It is cumbersome job to declare huge number of variables, writing input and output statements for them.

Even it is so difficult to send the details of a product to a function because individual values have to pass to individual variables. It would be easy if all the details of an entity are stored in a single variable so that all the details of a product can be sent to a function as a single argument.

struct in C language

C language provides no predefined data type to store multiple values of different types belongs to a single entity in a single variable.

But, fortunately C language allows to define our own data type according to our requirement. Say for example, we can define our own data type called “product” to store the details of a product in a single variable.

struct product
{
int pcode;
char pname[20];
float pprice;
float tax;
float sprice;
};

Here the struct is a keyword used to define our own data type. The above struct definition announces to the compiler that “product” is a data type and every variable (instance) belongs to “product” must have all the members listed in the definition, so that all the details of a product can be stored in a single variable.

It is important to note that, just defining the struct would not allocate the memory allocation of any member but only allocates on instantiating that is, on creating a variable belongs to struct. Now we will see how a variable belongs to struct can be created.

struct product x;

The above declaration statement allocates the variable “x” in the stack segment with all the members declared in the “struct product”, every member of the variable can be accessed using direct member access operator (.). Now we can store all the details of a product in the single variable “x”.

struct variable

More formal definition to struct:

A struct is a collection of one or more variables may be of different types, grouped under a single name. It helps to treat a group of related variables as a unit. The variables declared in struct definition are called members.

  • Keyword struct is used to declare the structure.
  • It should be identified with a name, which must be a valid identifier.
  • All the members are declared with in a block { }
  • The definition must be terminated with a semi-colon (;)
  • Defining the structure doesn’t allocate the memory allocation of any member
  • Generally struct is defined at the top of all the functions and bellow #include statements. Some times defined in a separate header file to include it into any program

Another way of declaring struct variable:

As in the above example, struct definition and declaration of struct variable can be written as separate statements. Even we can declare one or more struct variables with the struct definition as

struct product
{
int pcode;
char pname[20];
float pprice;
float tax;
float sprice;
}x,y,z;

struct variables

Here x,y and z are struct variables belongs to “product” available to all the functions will be defined here after

Now let us start working with struct..

Specification: Accept the details of an employee and print the pay-slip.

#include<stdio.h>
struct employee
{
int empno;
char ename[50];
int sal;
float hra,da,gross;
};
int main()
{
struct employee x;    /* Declaring struct variable */
printf("Empno:");
scanf("%d",&x.empno);
printf("Employee Name:");
scanf("%s",x.ename);
printf("Salary:");
scanf("%d",&x.sal);
x.hra=(float)x.sal*13/100;
x.da=(float)x.sal*15/100;
x.gross=x.sal+x.hra+x.da;
printf("\nPay Slip Details:\n");
printf("Empno:%d\n",x.empno);
printf("Employee Name:%s\n",x.ename);
printf("Salary:%d\n",x.sal);
printf("House Rent Allowance: %f\n",x.hra);
printf("Dearness Allowance: %f\n",x.da);
printf("Gross Salary: %f\n",x.gross);
return 0;
}

Execution:
Empno:1000
Employee Name:Smith
Salary:7500

Pay Slip Details:
Empno:1000
Employee Name:Smith
Salary:7500
House Rent Allowance: 975.000000
Dearness Allowance: 1125.000000
Gross Salary: 9600.000000

struct variable employee

Previous post:

Next post: