Structure Definition in C Language.

Let us study in detail the definition of structure from the given diagram.

struct:  The struct is the keyword in C programming which one is used to declare a new data-type. The meaning of struct is to grouping variables together.
name_of_structure:
  name_of_structure is the name of the structure which is used to define stucture.The name of structure is specified after the keyword struct. The name of structure should be unique.
data_Type:
  The data type is the one of the data type in c that indicates the type of the data members of the structure. A structure members can have different data types. A structure can have data members of different data types.
struct_member:
  This is the name and data type of the data member of the structure . Inside structure Any number of data members can be defined along their data types. Each data members is occupies a separate space in the memory.


Declaration of Structure Variables with Structure Definition.

Declaring a structure variable this way is suitable only when there are need to declare few variables.


struct employee
 {
  // structure definition
   int empno;
   char eName;
  float salary;
 } emp1, emp2;    // structure variables

The structure variables emp1 and emp2 are declared at the end of the structure definition, just before terminating the structure. In the above definition, emp1 and emp2 are the variables of the structure employee. These variables will be allocated separate copies of the structure’s data members that are- empno, eName, and salary.

C program to define and use the structure in C.

#include<stdio.h>
#include<string.h>
struct employee
 {
  // structure definition
   int empno;
   char eName[20];
  float salary;
 } emp1, emp2;    // structure variables
int main()
 {
  emp1.empno = 101;
  strcpy(emp1.eName, "Ajinkya");
  emp1.salary = 8545.5;
  printf("Employee Number: %d\n", emp1.empno);
  printf("Employee Name: %s\n", emp1.eName);
  printf("Employee Salary: %.2f\n", emp1.salary);`
}
Output: Employee Number: 101
Employee Name: Ajinkya
Employee Salary:8545.50


Previous Topic:-->> Array vs Structure in C || Next topic:-->>Declare Structure variable