C Programming Language Tutorial
Variables and Data Types
Input/Output
Looping and Selection Structures
Array
Functions
Preprocessing Command
Pointer
Structure
File Operations
Important Knowledge
In this tutorial, we'll explore the concept of structures in the C programming language. Structures are used to group variables of different data types under a single name, making it easier to manage and manipulate related data.
Structure Syntax
To define a structure, you can use the following syntax:
struct structure_name { data_type member1; data_type member2; ... data_type memberN; };
Examples
Here's an example of defining a structure and using it in a C program:
struct Point { int x; int y; };
#include <stdio.h> struct Point { int x; int y; }; int main() { // Declare and initialize a structure variable struct Point p1 = {2, 3}; printf("The point is located at (%d, %d)\n", p1.x, p1.y); return 0; }
In this example, we declare and initialize a structure variable p1
with values for the x
and y
members. We then print the values of these members.
#include <stdio.h> #include <math.h> struct Point { int x; int y; }; // Function declaration double calculate_distance(struct Point p1, struct Point p2); int main() { struct Point p1 = {2, 3}; struct Point p2 = {5, 7}; double distance = calculate_distance(p1, p2); printf("The distance between the two points is %.2f\n", distance); return 0; } // Function definition double calculate_distance(struct Point p1, struct Point p2) { int dx = p2.x - p1.x; int dy = p2.y - p1.y; return sqrt(dx * dx + dy * dy); }
In this example, we define a function calculate_distance()
that takes two Point
structures as arguments and calculates the distance between them. We then call this function in the main()
function to compute and print the distance between two points.
#include <stdio.h> struct Point { int x; int y; }; void set_point(struct Point *p, int x, int y); int main() { struct Point p1; set_point(&p1, 2, 3); printf("The point is located at (%d, %d)\n", p1.x, p1.y); return 0; } void set_point(struct Point *p, int x, int y) { p->x = x; p->y = y; }
In this example, we use a pointer to a structure in a function set_point()
that takes a pointer to a Point
structure and modifies its members. We use the arrow operator ->
to access the members of the structure through the pointer.
Conclusion
In this tutorial, we explored the concept of structures in the C programming language. Structures are an important feature of the language that allows for grouping related data members of different data types under a single name. Understanding and utilizing structures is essential for developing modular and maintainable C programs.
Defining and declaring structures in C language:
#include <stdio.h> // Defining and declaring structures in C struct Point { int x; int y; }; int main() { // Declaring a structure variable struct Point p1; // Accessing structure members p1.x = 10; p1.y = 20; printf("Point coordinates: (%d, %d)\n", p1.x, p1.y); return 0; }
Nested structures and hierarchy in C programming:
#include <stdio.h> // Nested structures and hierarchy in C struct Date { int day; int month; int year; }; struct Person { char name[50]; int age; struct Date birthdate; }; int main() { struct Person person1 = {"John", 25, {15, 7, 1998}}; printf("Person: %s\nAge: %d\nBirthdate: %d/%d/%d\n", person1.name, person1.age, person1.birthdate.day, person1.birthdate.month, person1.birthdate.year); return 0; }
Accessing structure members in C:
.
) operator.#include <stdio.h> // Accessing structure members in C struct Student { char name[50]; int age; float marks; }; int main() { struct Student student1 = {"Alice", 20, 85.5}; // Accessing structure members printf("Student Name: %s\nAge: %d\nMarks: %.2f\n", student1.name, student1.age, student1.marks); return 0; }
Initializing and assigning values to structures in C:
#include <stdio.h> // Initializing and assigning values to structures in C struct Rectangle { int length; int width; }; int main() { // Initializing structures during declaration struct Rectangle r1 = {10, 5}; // Assigning values to structures struct Rectangle r2; r2.length = 8; r2.width = 6; printf("Rectangle 1: %d x %d\n", r1.length, r1.width); printf("Rectangle 2: %d x %d\n", r2.length, r2.width); return 0; }
Arrays of structures and their usage in C:
#include <stdio.h> // Arrays of structures and their usage in C struct Book { char title[100]; char author[50]; int year; }; int main() { // Array of structures struct Book library[3] = { {"The C Programming Language", "K&R", 1978}, {"Clean Code", "Robert C. Martin", 2008}, {"Design Patterns", "Gamma, Helm, Johnson, Vlissides", 1994} }; // Accessing array of structures for (int i = 0; i < 3; ++i) { printf("Book %d: %s by %s (%d)\n", i + 1, library[i].title, library[i].author, library[i].year); } return 0; }