What is the Keyword Used to Define Structure in C?

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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.

structure in C

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 =50= 50, 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:

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

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.