C Structure Declaration

beginner
22 min

C Structure Declaration

Welcome to our comprehensive guide on C Structure Declaration! šŸŽÆ

In this lesson, we'll dive deep into understanding Structures, their declaration, and usage in C programming. By the end of this tutorial, you'll be able to create and manipulate your own custom data structures, a crucial skill for many programming projects.

Let's get started! šŸŽ‰

What are Structures in C?

In C programming, a structure is a user-defined data type that allows you to combine variables of different data types into a single entity. This can help in managing complex data structures encountered in real-world programming scenarios. šŸ’”

Structures Syntax

A structure is declared using the struct keyword, followed by a tag name and a set of member variables enclosed within curly braces. Here's a simple example of a structure named Person:

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

In this example, we've created a structure called Person with three members: name, age, and height.

šŸ“ Note: The structure tag Person is used to declare variables of this structure type.

Declaring Structure Variables

Now that we've declared our structure, let's create some variables of this type:

c
struct Person john; // Declaring a structure variable named john struct Person jane; // Declaring a structure variable named jane

Accessing Structure Members

To access the members of a structure variable, we use the dot operator (.). Here's how we can access and assign values to the members of our Person structure variables:

c
john.name = "John Doe"; john.age = 30; john.height = 1.75; jane.name = "Jane Smith"; jane.age = 28; jane.height = 1.65;

Printing Structure Members

We can print the members of a structure variable using the printf() function:

c
printf("John's name is %s\n", john.name); printf("John's age is %d\n", john.age); printf("John's height is %.2f\n", john.height); printf("Jane's name is %s\n", jane.name); printf("Jane's age is %d\n", jane.age); printf("Jane's height is %.2f\n", jane.height);
Quick Quiz
Question 1 of 1

What is the difference between a built-in data type and a structure in C?

Arrays of Structures

You can create arrays of structures using the same syntax as for regular arrays. Here's an example of an array of Person structures:

c
struct Person people[3];

Now, you can access and assign values to each element of the array just like regular arrays:

c
people[0].name = "John Doe"; people[0].age = 30; people[0].height = 1.75; people[1].name = "Jane Smith"; people[1].age = 28; people[1].height = 1.65; people[2].name = "Mike Johnson"; people[2].age = 25; people[2].height = 1.80;

Looping Through Arrays of Structures

We can loop through arrays of structures using nested loops or a single loop with an index variable:

c
for (int i = 0; i < 3; i++) { printf("Name: %s\n", people[i].name); printf("Age: %d\n", people[i].age); printf("Height: %.2f\n", people[i].height); }

And that's it! You now have a basic understanding of structure declaration and manipulation in C. As you progress in your programming journey, you'll find structures to be an invaluable tool for organizing complex data structures in your programs. šŸ“

Quick Quiz
Question 1 of 1

What is the purpose of the `struct` keyword in C?