+1 vote
in Programming Languages by (77.2k points)
Please give an example explaining "structure" in C.

1 Answer

+2 votes
by (60.0k points)
selected by
 
Best answer

In C programming, a "structure" is a user-defined data type. You can use it to group together variables of different data types under a single name. For example, suppose you want to define a new data type, Student, that contains name, age, and height so that you can access them together. For such scenarios, you can use the "structure" data type.

You can access the individual variable of a structure using dot (.) notation.

Here's an example to show how to define and use a structure in C:

#include <stdio.h>

#include <string.h>

// Defining a structure named 'Student'

struct Student {

    char name[25];

    int age;

    float height;

};

int main() {

    // Declaring a structure variable of type 'Student'

    struct Student student1;

    // Initializing structure members

    strcpy(student1.name, "Tom Dick Harry");

    student1.age = 12;

    student1.height = 4.8;

    printf("Name: %s\n", student1.name);

    printf("Age: %d\n", student1.age);

    printf("Height: %.2f\n", student1.height);

    return 0;

}


...