WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

C · Basic · question 11 of 100

What is a structure in C, and how do you define one?

📕 Buy this interview preparation book: 100 C questions & answers — PDF + EPUB for $5

In C programming language, a structure is a user-defined data type that allows developers to group together variables of different data types under a single name. Structures are useful for organizing complex data structures, such as records or objects, and for passing complex data between functions. Here’s how you can define a structure in C:

    struct example {
        int id;
        char name[50];
        float score;
    };

In this example, a structure named example is defined that contains three members: id, name, and score. The id member is an integer, the name member is an array of characters with a maximum length of 50, and the score member is a floating-point number.

To use the example structure in your program, you can declare a variable of the structure type:

    struct example record;

In this example, a variable named record is declared with the example structure type.

You can then access the members of the record variable using the dot operator .:

    record.id = 1;
    strcpy(record.name, "John Smith");
    record.score = 87.5;

In this example, the members of the record variable are assigned values. The id member is set to 1, the name member is set to "John Smith" using the strcpy() function, and the score member is set to 87.5.

You can also declare a variable of the structure type and initialize its members in a single statement:

    struct example record = {1, "John Smith", 87.5};

In this example, a variable named record is declared and initialized with the values 1, "John Smith", and 87.5.

Structures in C are a powerful tool for organizing complex data structures and passing them between functions. They allow developers to group together variables of different data types under a single name and access them using the dot operator .. By understanding how to define and use structures in C, developers can write more efficient, effective, and maintainable programs.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic C interview — then scores it.
📞 Practice C — free 15 min
📕 Buy this interview preparation book: 100 C questions & answers — PDF + EPUB for $5

All 100 C questions · All topics