Structure of C


Structure of C

Arrays allow you to construct variable types that can store many data objects of the same type. Structure is another user-defined data type in C that allows you to mix data pieces of various types.

A record is represented by a structure. Assume you want to keep track of the books you've borrowed from the library. You should keep the following features in mind with each book:

Author Title Subject Book ID

Defining a Framework

The struct statement is used to define a structure. The struct statement creates a new data type that has many members. The struct statement has the following format:

struct [structure tag] { member definition; member definition; ... member definition; } [one or more structure variables];

The structure tag is optional, and each member definition is a regular variable definition, such as int I float f; or any other valid variable declaration. You can specify one or more structure variables before the last semicolon at the conclusion of the structure declaration, although this is optional. This is how the Book structure would be declared. −

struct Books { char title[50]; char author[50]; char subject[100]; int book_id; } book;
  • In C, there are times when many characteristics of an entity must be stored. It is not essential for an entity to have all information of a single kind. It can have a variety of characteristics from various data kinds. For example, a Student object may have a name (string), a roll number (int), and marks (float). The following techniques may be used to store such information on an entity student:
  • Create separate arrays to store names, roll numbers, and marks.
  • To store a collection of multiple data kinds, use a specific data structure.

Structure is a user-defined data type in C that allows us to store a variety of data kinds. A member is a term used to describe each component of a structure. Structures may be used to imitate the use of classes and templates since they can hold a variety of data.The structure is defined using the,struct keyword. Let's look at the c syntax for defining the structure.

structure name struct { data_type member1; data_type member2; . . data_type memeberN; };

Let's see the example to define a structure for an entity employee in c.

struct employee { int id; char name[20]; float salary; };

A structure is a collection of variables of various data kinds that are identified by a single name. Let's look at an example to see why a structure is necessary in C programming.

Let's assume we need to keep track of student information such as name, age, address, and ID. One method to accomplish this is to establish a separate variable for each characteristic; but, if you need to store the data of numerous students, you'll need to construct these multiple variables again for each student. Data storage in this manner is quite inconvenient.

We can build a structure that has members for name, id, address, and age, and then create variables for each student within that structure. This may appear perplexing, but don't worry, we'll explain it with the assistance of an example.

How to create a structure in C Programming

In C, the struct keyword is used to build a structure. The struct keyword stands for structured data type in concise form.

struct struct_name { DataType member1_name; DataType member2_name; DataType member3_name; … };

struct name may be whatever you want here. The data types of the members might be the same or different. We may use the struct name as a data type like int, float, and so on once we've declared the structure.We'll start with the syntax for generating struct variables, accessing struct members, and so on, before moving on to a comprehensive example..

How to declare variables of a structure?

struct struct_name var_name;

or

struct struct_name { DataType member1_name; DataType member2_name; DataType member3_name; … } var_name;

How can I use a struct variable to access data members of a structure?

var_name.member1_name; var_name.member2_name; …

Example of structure:

#include<stdio.h> #include<conio.h> #include<string.h> struct student { int age; float marks; }; void main() { struct student s; s.age=17; s.marks=80; printf("student age is : %d \n ",s.age); printf("student marks is : %f",s.marks); getch(); }

output:

student age is : 17 student marks is : 80