C PROGRAMMING
MULTIPLE CHOICE QUESTION
OLD QUESTION BANK
SAMPLE QUESTION 2080 AND SOLUTION

A structure can be initialized either at the time of declaration or after declaring the structure variable. 

Method 1: Initialization at Declaration

A structure can be initialized at the time of declaration by enclosing the initial values within curly braces {} after the variable name. 

#include <stdio.h>

// Define a structure named 'Person'
struct Person {
    char name[50];
    int age;
    float height;
};

int main() {
    // Declare and initialize a structure variable 'p'
    struct Person p = {"John", 30, 6.0};

    // Accessing and printing the values of 'p' members
    printf("Name: %s\n", p.name);
    printf("Age: %d\n", p.age);
    printf("Height: %.2f\n", p.height);
}

Method 2: Initialization after Declaration

A structure can also be initialized after declaring the structure variable. 

#include <stdio.h>

// Define a structure named 'Person'
struct Person {
    char name[50];
    int age;
    float height;
};

int main() {
    // Declare a structure variable 'p'
    struct Person p;

    // Initialize 'p' after declaration
    strcpy(p.name, "John");
    p.age = 30;
    p.height = 6.0;

    // Accessing and printing the values of 'p' members
    printf("Name: %s\n", p.name);
    printf("Age: %d\n", p.age);
    printf("Height: %.2f\n", p.height);

}