What is the Keyword Used to Define Structure in C?
Introduction
The Structure in C is a user-defined datatype that stores variables of different data types. The keyword used to define structure in c is struct. After the struct keyword, the user must enter the structure's name. After this, inside the curly braces, the data types and the name of the members are declared.
Syntax:
What is Structure in C?
Sometimes, a user must store different data types and variables in one entity. For example, a teacher may need to make a structure of every student with name, marks, class, etc.; for these purposes, Structure in C can be used to store such data.
The structure in C is a user-defined datatype that stores different data type variables under one user-defined data type. The variables inside the structure are known as members. The members are logically related to each other (in the example of a teacher maintaining the data of students, the data were related to each other, as they were of a particular student every time).
When accessing the structure, a structure variable is created in the main function (generally outside the structure body). Then, all the structure's data members are available outside.
For example, let us make a structure with a char, int, and a size 5-character array.
The above code will create the structure template, and whenever we need to use this structure, we use it by the structure variable, which is defined outside the structure (it can be in the main() function as well). The struct keyword with the structure name is used in C to create the structure variable if it is done inside the main function.
Memory representation of the demo_variable created above.
Every time a new structure variable is created, the char A will take up the 1 byte of memory, int B will take 4 bytes, and the character array C, of size 5, will take 5 bytes. This memory may or may not be allocated contiguously (depending on the padding, which needs a separate article).
As we know, structure variables are different from standard variables, and there is also a different method for initializing them. We cannot simply use the variable name and assign the value, as in the above case, demo_variable , as it will throw a compilation error.
To initialize the variables, we need to use the structure variable name, then a . dot, and then the member name we want to initialize. For example, in the above case :
There can be more than one structure variable. To learn more about the structure in C, Follow this article by Scaler Topics
Example:
In this example, we will understand how a structure is created and how to use the structure variable to initialize and access the members.
Output:
Conclusion
- Struct is the keyword used to define structure in C.
- The structure in C is a user-defined datatype that stores different data type variables under one user-defined data type.
- The Structure variables are used to access the members of the structure.
- The size of the structure variable will be the same as the sum of the sizes of the different data types members of the structure.
- To initialize the variables, we need to use the structure variable name, a '.' dot, and the member name we want to initialize.