Getting Started With C

Define the structure

First, you need to define the structure using the struct keyword. The structure definition includes the data members (variables) that will be part of the structure.

struct Person {
            char name[50];
            int age;
            float height;
        };
In this example, we define a structure named Person with three members: name (a character array), age (an integer), and height (a float).

Declare Structure Variables:

The syntax for declaring structure variables in C is as follows:

struct structure_name {
    // member1 declaration
    // member2 declaration
    // ...
};
// Declare structure variables
struct structure_name variable1, variable2, ...;

Here's a breakdown:

Example:
        struct Person person1, person2;
        Here, we declare two variables of type struct Person named person1 and person2.
        
        

Initialize Structure Variables

You can initialize the structure variables at the time of declaration or later using the assignment operator (=).
            struct Person person1 = {"John Doe", 25, 5.8};
            
            Alternatively, you can initialize the members separately:
            person2.age = 30;
            person2.height = 6.1;
            strcpy(person2.name, "Jane Smith");
            

Resources :

Test Your Knowledge
Choose The

With supporting text below as a natural lead-in to additional content.

Q1. How do you declare a variable of a structure type in C?

Q2. In C, what is the correct way to initialize a structure variable during declaration?