Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Different ways to initialize structure variable

Example no 1

#include <stdio.h>

struct Employee
{
    int id_no;
    char name[30];
    float salary;
}emp1 = {1,"Robin",35000.67},
 emp2 = {2,"Kary",45000.47},
 emp3 = {3,"Sachin",98000.34};

int main()
{
    printf("\n emp1.id_no = %d",emp1.id_no);
    printf("\n emp1.name = %s",emp1.name);
    printf("\n emp1.salary = %f",emp1.salary);
    printf("\n");
    printf("\n emp2.id_no = %d",emp2.id_no);
    printf("\n emp2.name = %s",emp2.name);
    printf("\n emp2.salary = %f",emp2.salary);
    printf("\n");
    printf("\n emp3.id_no = %d",emp3.id_no);
    printf("\n emp3.name = %s",emp3.name);
    printf("\n emp3.salary = %f",emp3.salary);

    return 0;	
}

Output

emp1.id_no = 1
emp1.name = Robin
emp1.salary = 35000.671875

emp2.id_no = 2
emp2.name = Kary
emp2.salary = 45000.468750

emp3.id_no = 3
emp3.name = Sachin
emp3.salary = 98000.343750

Example no 2

#include <stdio.h>

struct Employee
{
    int id_no;
    char name[30];
    float salary;
};

struct Employee emp1 = {10,"Ricky",70000.67};
struct Employee emp2 = {20,"Vicky",90000.47};
struct Employee emp3 = {30,"Micky",35000.34};

int main()
{
    printf("\n emp1.id_no = %d",emp1.id_no);
    printf("\n emp1.name = %s",emp1.name);
    printf("\n emp1.salary = %f",emp1.salary);
    printf("\n");
    printf("\n emp2.id_no = %d",emp2.id_no);
    printf("\n emp2.name = %s",emp2.name);
    printf("\n emp2.salary = %f",emp2.salary);
    printf("\n");
    printf("\n emp3.id_no = %d",emp3.id_no);
    printf("\n emp3.name = %s",emp3.name);
    printf("\n emp3.salary = %f",emp3.salary);

    return 0;	
}

Output

emp1.id_no = 10
emp1.name = Ricky
emp1.salary = 70000.671875

emp2.id_no = 20
emp2.name = Vicky
emp2.salary = 90000.468750

emp3.id_no = 30
emp3.name = Micky
emp3.salary = 35000.339844

Example no 3

#include <stdio.h>

struct Employee
{
    int id_no;
    char name[30];
    float salary;
};

int main()
{
    struct Employee emp1 = {100,"Sita",55000.67};
    struct Employee emp2 = {200,"Gita",66000.47};
    struct Employee emp3 = {300,"Nita",77000.34};

    printf("\n emp1.id_no = %d",emp1.id_no);
    printf("\n emp1.name = %s",emp1.name);
    printf("\n emp1.salary = %f",emp1.salary);
    printf("\n");
    printf("\n emp2.id_no = %d",emp2.id_no);
    printf("\n emp2.name = %s",emp2.name);
    printf("\n emp2.salary = %f",emp2.salary);
    printf("\n");
    printf("\n emp3.id_no = %d",emp3.id_no);
    printf("\n emp3.name = %s",emp3.name);
    printf("\n emp3.salary = %f",emp3.salary);

    return 0;	
}

Output

emp1.id_no = 100
emp1.name = Sita
emp1.salary = 55000.671875

emp2.id_no = 200
emp2.name = Gita
emp2.salary = 66000.468750

emp3.id_no = 300
emp3.name = Nita
emp3.salary = 77000.343750

Example no 4

#include <stdio.h>

struct Employee
{
    int id_no;
    char *name; // pointer is used instead of array
    float salary;
};

int main()
{
     struct Employee emp1, emp2;
     emp1.id_no = 15;
     emp1.name = "Smith";
     emp1.salary = 64576.98;

     emp2.id_no = 25;
     emp2.name = "David";
     emp2.salary = 45649.67;

     printf("\n emp1.id_no = %d",emp1.id_no);
     printf("\n emp1.name = %s",emp1.name);
     printf("\n emp1.salary = %f",emp1.salary);
     printf("\n");
     printf("\n emp2.id_no = %d",emp2.id_no);
     printf("\n emp2.name = %s",emp2.name);
     printf("\n emp2.salary = %f",emp2.salary);

     return 0;	
}

Output

emp1.id_no = 15
emp1.name = Smith
emp1.salary = 64576.980469

emp2.id_no = 25
emp2.name = David
emp2.salary = 45649.671875

Note 1

If only a few members of the structure are initialized then by default, remaining members of the structure are initialized with zero.

Example

#include <stdio.h>

struct Employee
{
    int id_no;
    float salary;
    int dept_no;
};

struct Employee emp1 = {10}; // by default
                             // only first member is initialized

int main()
{
    printf("\n emp1.id_no = %d",emp1.id_no);
    printf("\n emp1.salary = %f",emp1.salary);
    printf("\n emp1.dept_no = %d",emp1.dept_no);

    return 0;	
}

Output

emp1.id_no = 10
emp1.salary = 0.000000
emp1.dept_no = 0

Note 2

Direct initialization of structure members is not allowed.

Example

C Programming Language Can we Initialize Structure Members DirectlyAnother example of the structure

Example – using scanf() function

#include<stdio.h>
#include<string.h>

struct Employee
{
    int id_no;
    char name[50];
    float salary;
};

int main()
{
    struct Employee emp1;

    printf("\n Enter the ID no. of emp1 = ");
    scanf("%d",&emp1.id_no);
    printf("\n Enter the NAME of emp1 = ");
    scanf("%s",emp1.name);
    printf("\n Enter the SALARY of emp1 = ");
    scanf("%f",&emp1.salary);
	
    printf("\n After entering the data");
    printf("\n\t ID NO. = %d",emp1.id_no);
    printf("\n\t NAME = %s",emp1.name);
    printf("\n\t SALARY = %f",emp1.salary);

     return 0;
}

Output

Enter the ID no. of emp1 = 1

Enter the NAME of emp1 = JOHN

Enter the SALARY of emp1 = 69000.65

After entering the data
    ID NO. = 1
    NAME = JOHN
    SALARY = 69000.648438

 

Leave a reply