Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
203 views
in C Programming by (112k points)
Explore the power of C structures (structs) and learn how to efficiently organize data in your programs. Discover the benefits, syntax, and usage of structs in C programming. From creating custom data types to optimizing memory allocation, this guide covers it all. Get started on mastering C structures today!

Please log in or register to answer this question.

2 Answers

0 votes
by (112k points)

1. Introduction to C Structures (structs)

C structures, commonly referred to as structs, are user-defined data types that allow you to group related data items together into a single unit. A struct can contain multiple members of different data types, such as integers, floating-point numbers, characters, or even other structures. This makes structs a powerful tool for organizing and manipulating data in C programs.

2. Creating a Structure

To create a struct, you need to define its layout and members. The syntax for creating a struct is as follows:

struct structure_name {
    data_type member1;
    data_type member2;
    // ...
};
 

Here's an example of defining a struct called Person with members for name, age, and height:

struct Person {
    char name[50];
    int age;
    float height;
};
 

3. Accessing Structure Members

Once you've defined a struct, you can create variables of that struct type and access its members using the dot (.) operator. 

Here's an example:

struct Person person1;
strcpy(person1.name, "John Doe");
person1.age = 25;
person1.height = 1.75;
 

In this example, we create a variable person1 of type struct Person and assign values to its members (name, age, and height) using the dot operator.

4. Strings in Structures

Structs can also contain members of type char arrays, allowing you to store and manipulate strings within structures. 

Here's an example:

struct Person {
    char name[50];
    int age;
};

struct Person person1;
strcpy(person1.name, "John Doe");
person1.age = 25;
 

In this case, the name member of the Person struct is an array of characters, which can store a string of up to 49 characters (plus the null terminator).

5. Simpler Syntax (Optional)

C99 introduced a simpler syntax for defining and initializing structs, known as designated initializers. It allows you to initialize specific members of a struct without specifying them in order. 

Here's an example:

struct Person {
    char name[50];
    int age;
    float height;
};

struct Person person1 = {
    .age = 25,
    .name = "John Doe",
    .height = 1.75
};
 

Using designated initializers, you can specify the values of struct members explicitly by name, making the code more readable.

6. Copying Structures

To copy the values of one struct into another, you can simply use the assignment operator (=). This will copy each member from the source struct to the destination struct. 

Here's an example:

struct Person person1;
struct Person person2;

strcpy(person1.name, "John Doe");
person1.age = 25;
person1.height = 1.75;

person2 = person1; // Copying person1 to person2
 

After the assignment, person2 will have the same values as person1.

7. Modifying Structure Values

You can modify the values of struct members directly by assigning new values to them. 

Here's an example:

struct Person person1;

strcpy(person1.name, "John Doe");
person1.age = 25;
person1.height = 1.75;

person1.age = 26; // Modifying the age member
 

In this case, we change the value of the age member in person1 from 25 to 26.

That covers the basics of working with C structures (structs). By creating structures, accessing their members, using strings within structures, employing simpler syntax, copying structures, and modifying values, you can effectively organize and manipulate data in your C programs.

0 votes
by (112k points)

FAQs on C Structures (structs)

Q: What is a structure in C? 

A: In C, a structure, also known as a struct, is a user-defined data type that allows you to combine different types of variables under a single name. It allows you to create a custom data structure that can hold related information.

Q: How do you define a structure in C? 

A: To define a structure in C, you use the struct keyword followed by the structure name and a list of member variables enclosed in curly braces. 

Here's an example:

struct Person {
    char name[50];
    int age;
    float height;
};
 

Q: How do you declare a structure variable? 

A: To declare a structure variable, you use the structure name followed by the variable name. 

Here's an example:

struct Person person1;
 

Q: How do you access members of a structure? 

A: To access the members of a structure, you use the dot (.) operator. 

Here's an example:

person1.age = 25;
printf("Name: %s\n", person1.name);
printf("Age: %d\n", person1.age);
printf("Height: %.2f\n", person1.height);
 

Q: Can you provide an example of using structures in C? 

A: Certainly! Here's an example that demonstrates the usage of a structure:

#include <stdio.h>

struct Point {
    int x;
    int y;
};

int main() {
    struct Point p1;
    p1.x = 3;
    p1.y = 5;

    printf("Coordinates: (%d, %d)\n", p1.x, p1.y);

    return 0;
}
 

In this example, we define a structure called Point with x and y as its members. We then declare a structure variable p1 and assign values to its members. Finally, we print the coordinates using printf.

Important Interview Questions and Answers on C Structures (structs)

Q: What is a structure in C? 

A structure in C is a user-defined data type that allows you to combine different variables of different data types into a single unit. It is used to represent a group of related data items.

Example:

struct Employee {
   int empId;
   char empName[50];
   float empSalary;
};
 

Q: How do you declare a structure variable? 

To declare a structure variable, you need to use the struct keyword followed by the structure name and the variable name.

Example:

struct Employee emp1;
 

Q: How do you access members of a structure variable? 

You can access the members of a structure variable using the dot operator (.).

Example:

emp1.empId = 1001;
strcpy(emp1.empName, "John Doe");
emp1.empSalary = 5000.0;
 

Q: Can a structure contain another structure as a member? 

Yes, a structure can contain another structure as a member. This is known as nesting or embedding of structures.

Example:

struct Date {
   int day;
   int month;
   int year;
};

struct Student {
   int rollNo;
   char name[50];
   struct Date dob;
};
 

Q: How do you pass a structure to a function? 

You can pass a structure to a function either by value or by reference. When passed by value, a copy of the structure is created. When passed by reference, the function receives a pointer to the structure.

Example of passing by value:

void displayEmployee(struct Employee emp) {
   printf("ID: %d\n", emp.empId);
   printf("Name: %s\n", emp.empName);
   printf("Salary: %.2f\n", emp.empSalary);
}

// Calling the function
displayEmployee(emp1);
 

Example of passing by reference:

void displayEmployee(struct Employee *emp) {
   printf("ID: %d\n", emp->empId);
   printf("Name: %s\n", emp->empName);
   printf("Salary: %.2f\n", emp->empSalary);
}

// Calling the function
displayEmployee(&emp1);
 

Q: What is the size of a structure? 

The size of a structure is determined by the sum of the sizes of its members. It may also include padding to ensure proper memory alignment.

Example:

printf("Size of struct Employee: %d bytes\n", sizeof(struct Employee));
 

Q: How do you initialize a structure variable? 

You can initialize a structure variable at the time of declaration using an initializer list.

Example:

struct Employee emp1 = {1001, "John Doe", 5000.0}; 

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...