C initializing structure

Initializing Structure Variable in C

  1. Memory is not allocated for Un-initialized Variable.
  2. Let us discuss very familiar example of structure student , We can initialize structure variable in different ways -

Way 1 : Declare and Initialize

struct student
{
   char name[20];
   int roll;
   float marks;
}std1 = { "Pritesh",67,78.3 };

Way 2 : Declaring and Initializing Multiple Variables

struct student
{
   char name[20];
   int roll;
   float marks;
}
std1 = {"Pritesh",67,78.3};
std2 = {"Don",62,71.3};

Way 3 : Initializing Single member

struct student
{
   int mark1;
   int mark2;
   int mark3;
} sub1={67};

Though there are three members of structure,only one is initialized , Then remaining two members are initialized with Zero

Way 4 : Initializing inside main

struct student
{
    int mark1;
    int mark2;
    int mark3;
};
void main()
{
struct student s1 = {89,54,65};
- - - - --
- - - - --
- - - - --
};