Defining and Declaring Structures in C Programming
In C programming, structures are used to group different data types into a single unit. Structures allow you to store and manage multiple related variables of different types under one name. This is especially useful when dealing with complex data like student records, employee details, or geometric shapes.
At SamagraCS Educational Technology, we want to make learning structures easy and relatable, with real-life examples that students can understand and apply.
What is a Structure?
A structure is a user-defined data type in C that allows you to combine different types of data under one name. For example, a structure can hold an int
(for age), a float
(for salary), and a char[]
(for name), all together.
Real-life analogy:
Think of a structure like a form that collects information. For example, a “Student Form” can collect a student’s name, age, and marks, all grouped together.
1. Defining a Structure
Before using a structure, you must define it. A structure definition provides a blueprint for the structure but does not allocate any memory. It only tells the compiler what data types the structure will hold.
Syntax of Structure Definition:
struct structure_name {
data_type1 variable1;
data_type2 variable2;
// More variables
};
- struct: This keyword is used to define a structure.
- structure_name: This is the name of the structure.
- data_type1, data_type2: These are the data types of the variables (e.g.,
int
,float
,char[]
). - variable1, variable2: These are the names of the variables (also called members).
Example:
Let’s define a structure to hold student information.
struct Student {
char name[50];
int age;
float marks;
};
In this example:
- The
Student
structure has three members:name
(a string),age
(an integer), andmarks
(a floating-point number).
2. Declaring a Structure Variable
Once a structure is defined, you can declare variables of that structure type. Declaring a structure variable is similar to declaring variables of built-in types.
Syntax of Structure Variable Declaration:
struct structure_name variable_name;
Example:
struct Student student1;
Here, student1
is a variable of type struct Student
, which means it can hold information like the student’s name, age, and marks.
You can also declare and define the structure together:
struct Student {
char name[50];
int age;
float marks;
} student1, student2; // Declaring two structure variables
3. Accessing Structure Members
You can access the members of a structure using the dot operator (.
). The dot operator allows you to access the variables (members) inside the structure.
Syntax:
variable_name.member_name
Example:
#include <stdio.h>
struct Student {
char name[50];
int age;
float marks;
};
int main() {
struct Student student1;
// Assigning values to structure members
student1.age = 20;
student1.marks = 85.5;
strcpy(student1.name, "John");
// Accessing structure members
printf("Name: %s\n", student1.name);
printf("Age: %d\n", student1.age);
printf("Marks: %.2f\n", student1.marks);
return 0;
}
Output:
Name: John
Age: 20
Marks: 85.50
4. Structure Initialization
Just like variables of primitive data types, you can initialize structure variables when you declare them.
Example:
struct Student {
char name[50];
int age;
float marks;
};
int main() {
// Initializing the structure variable
struct Student student1 = {"Alice", 19, 90.5};
printf("Name: %s\n", student1.name);
printf("Age: %d\n", student1.age);
printf("Marks: %.2f\n", student1.marks);
return 0;
}
Output:
Name: Alice
Age: 19
Marks: 90.50
5. Real-Life Example
Let’s create a structure for an Employee to store their ID, name, and salary.
Code Example:
#include <stdio.h>
struct Employee {
int id;
char name[50];
float salary;
};
int main() {
struct Employee emp1;
// Inputting employee details
emp1.id = 101;
strcpy(emp1.name, "Pawan Jaiswal");
emp1.salary = 50000.75;
// Printing employee details
printf("Employee ID: %d\n", emp1.id);
printf("Employee Name: %s\n", emp1.name);
printf("Employee Salary: %.2f\n", emp1.salary);
return 0;
}
Output:
Employee ID: 101
Employee Name: Pawan Jaiswal
Employee Salary: 50000.75
6. Important Points to Remember
- Structures group different types of data together, making them useful for creating complex data types.
- You access structure members using the dot operator.
- Structures can be initialized when declared.
- Structures do not occupy memory until you declare a structure variable.
Tips to Remember Structures:
- Think of structures like forms: Just as a form collects various pieces of information about a person, a structure holds different data types under one name.
- Visualization: Visualize a structure as a real-life object. For example, visualize a “car” structure that holds information like model, color, and speed.
- Practice: Practice by creating structures for different objects or concepts, such as books, cars, or students.
Structures in C programming allow you to create custom data types that can store multiple related variables of different data types under one name. At SamagraCS Educational Technology, we aim to simplify these concepts with real-life examples and practical applications. Now that you know how to define, declare, and use structures, you’re ready to create more complex programs!
If you have any questions, feel free to reach out to Pawan & Pooja , or the team at SamagraCS Educational Technology. Keep learning, keep growing!