initialization of structure members in c programming Language.

From the given figure we can say that the Structure initialization can be done in different ways.
1.Default initialization.
2.Initialize using (.) Operator.
3.Value initialized structure variable.


1. Default Initialization.
Default initialization of a structure variable is considered as good practice in programming . But, C programming doesn’t support any programming feature for default structure initialization. you have to manually initialize all the fields of structure to NULL or 0 .
Initializing all fields of structure to NULL is bit difficult process. Let’s do have a small example to initialize the structure members to default value, on every structure variable declaration.
e.g.

/* default structure initialization using macro */
#define NEW_EMP { "", 0, 0.0f }
/* Default initialization of structure variable */
struct employee emp1 = NEW_EMP;


Program to declare, initialize and access structure.

 /**
 *declare, access and initialize structures in C language
 */
 #include <stdio.h>
 #include <string.h>
 // Macro for default employee structure initialization
 #define NEW_EMP { "", 0, 0.0f }
 // employee structure type declaration
 struct employee
 {
    char name[15];
   int empno;
   float salary;
 };
 int main()
 {
    // Declare employee structure variable with default initialization
   struct employee emp1 = NEW_EMP;
   // Read employee details from user
   printf("Enter employee name:\n ");
   gets(emp1.name);
   printf("Enter employee number:\n ");
   scanf("%d", &emp1.empno);
   printf("Enter employeee salary:\n ");
   scanf("%f", &emp1.salary);
   // Print employee details
   printf("\n\nEmployee details\n");
   printf("Employee Name : %s\n", emp1.name);
   printf("Employee Number : %d\n", emp1.empno);
   printf("Salary: %.2f\n", emp1.salary);
   return 0;
 }
Output:
Enter employee name:
John P.
Enter employee number:
101
Enter employeee salary:
5500

Employee details
Employee Name: John P.
Employee Number: 101
Salary: 5500


2. Structure initialization using dot(.) Operator.

/* C program to demonstrate the Structure initialization using dot(.) operator*/
#include <stdio.h>
#include<string.h>
 struct employee
 {
    int empno;
   char name[15];
   float salary;
 };
int main()
 {
   struct employee e1;
   /* intialize structure member */
   e1.empno=101;
   strcpy(e1.name,"Kailas W.");
   e1.salary=4567.78;
    /* accessing structure member */
   printf("Employee Number: %d", e1.empno);
   printf("Employee Name: %s", e1.name);
   printf("Employee Salary: %.2f", e1.salary);
 return 0;
 }
Output:
Employee Number: 101
Employee Name: Kailas W.
Employee Salary: 4567.78


3.Value initialized structure variable.

The above given method we studied is easy and straightforward to initialize a structure variable.
However, value initialization is also supported in C programming for structure variable. Means, the user or programmer can initialize a structure to some initial or default value during its variable declaration.

Example. Program for Value Initialized structure variable & Accessing the Structure Members in C Programming .


#include<stdio.h>
struct employee
 {
  char *ename;
  int empno;
  float salary;
 };
int main()
 {
   struct employee e={"Kailas W.",101,4567.78f};
  printf("\nEmployee Number : %d",e.empno);
  printf("\nEmployee Name : %s",e.ename);
  printf("\nSalary : %.2f",e.salary);
  return 0;
 }
Output:

Employee Number :101
Employee Name :Kailas W.
Salary :4567.78

Previous Topic:-->> Access stucture members. || Next topic:-->>Copy and compare stucture variable.