Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Defining and Declaring Structs | Introduction to Structs
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C Structs

bookDefining and Declaring Structs

Defining

A structure declaration begins with the keyword struct.

main.c

main.c

copy
1234567
struct <name_of_struct> { <type_1> <field_1>; <type_2> <field_2>; … <type_N> <field_N>; };

<type_1> <field_1> - variables that will make up the structure. The structure declaration does not take up memory, that is, it is just a blueprint, a template.

First Method

Once we have declared a structure, we can create a variable of that type using the word struct.

main.c

main.c

copy
1
struct <name_of_struct> <name_of_variable>;

Declaring multiple struct variables is the same as declaring regular variables:

main.c

main.c

copy
1
struct <name_of_struct> <name_of_variable_1>, <name_of_variable_2>,<name_of_variable_3>;

Second Method

This option allows the structure to be instantiated immediately after it is defined.

main.c

main.c

copy
1234567
struct <name_of_struct> { <type_1> <field_1>; <type_2> <field_2>; … <type_N> <field_N>; } <name_of_variable>; // defining of a variable

In this way it is convenient to create several variables at once:

main.c

main.c

copy
1234567
struct <name_of_struct> { <type_1> <field_1>; <type_2> <field_2>; … <type_N> <field_N>; } <name_of_variable_1>, <name_of_variable_2>;

Initialization

Initializing a structure == initializing a variable of a new data type

To initialize a structure, you must use curly braces {...}:

main.c

main.c

copy
12345678910
struct <name_of_struct> { <type_1> <field_1>; <type_2> <field_2>; … <type_N> <field_N>; }; // initialization of struct-variable struct <name_of_struct> <name_of_variable> = {field_1, field_2, field_N};

In this case, the fields will be assigned in order and there is no need to indicate the data type of each field, because this has already been done in the blueprint of the structure.

Using the Student structure as an example β€” it includes all the fields we need for a student: id, name, and age. Now all the data is stored in one place, and we have a separate structure that we can easily reuse.

question mark

Select the option with the correct structure declaration:

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you show me an example of how to declare a struct in code?

How do I initialize a struct variable with values?

What are the benefits of using structures in programming?

bookDefining and Declaring Structs

Swipe to show menu

Defining

A structure declaration begins with the keyword struct.

main.c

main.c

copy
1234567
struct <name_of_struct> { <type_1> <field_1>; <type_2> <field_2>; … <type_N> <field_N>; };

<type_1> <field_1> - variables that will make up the structure. The structure declaration does not take up memory, that is, it is just a blueprint, a template.

First Method

Once we have declared a structure, we can create a variable of that type using the word struct.

main.c

main.c

copy
1
struct <name_of_struct> <name_of_variable>;

Declaring multiple struct variables is the same as declaring regular variables:

main.c

main.c

copy
1
struct <name_of_struct> <name_of_variable_1>, <name_of_variable_2>,<name_of_variable_3>;

Second Method

This option allows the structure to be instantiated immediately after it is defined.

main.c

main.c

copy
1234567
struct <name_of_struct> { <type_1> <field_1>; <type_2> <field_2>; … <type_N> <field_N>; } <name_of_variable>; // defining of a variable

In this way it is convenient to create several variables at once:

main.c

main.c

copy
1234567
struct <name_of_struct> { <type_1> <field_1>; <type_2> <field_2>; … <type_N> <field_N>; } <name_of_variable_1>, <name_of_variable_2>;

Initialization

Initializing a structure == initializing a variable of a new data type

To initialize a structure, you must use curly braces {...}:

main.c

main.c

copy
12345678910
struct <name_of_struct> { <type_1> <field_1>; <type_2> <field_2>; … <type_N> <field_N>; }; // initialization of struct-variable struct <name_of_struct> <name_of_variable> = {field_1, field_2, field_N};

In this case, the fields will be assigned in order and there is no need to indicate the data type of each field, because this has already been done in the blueprint of the structure.

Using the Student structure as an example β€” it includes all the fields we need for a student: id, name, and age. Now all the data is stored in one place, and we have a separate structure that we can easily reuse.

question mark

Select the option with the correct structure declaration:

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 3
some-alt