Categories: C

Structure in C

The structure is a user-defined data type in C that allows us to aggregate data of various types. Structure aids in the creation of a more understandable data type.

Need for Structure in C:

There are times when we need to keep several characteristics of an entity in C. An entity does not need to have all the information of a single kind. It can have properties from numerous data types. For example, a Student object may have a name (string), a roll number (int), and marks (float). We have the following methods for storing such details about an entity student:

  • Create separate arrays to store names, roll numbers, and marks.
  • To store a collection of multiple data kinds, use a particular data structure.

We may achieve this with a simpler way in which you can utilize a particular data structure, i.e., structure, in which you can group all the information of different data types about an entity.

Defining a Structure:

The struct keyword is used to define the structure.

struct name_of_structure
{
    dataype variable;
    dataype variable;
    dataype variable;

}; 

Example:

struct student
{
   char name;
   int roll_no;
   float marks[5];
   float percent;
};   
Declaring structure variables:

We can declare structure variables in the following two ways:

1) Declaring Structure variables separately

struct student
{
   char name;
   int roll_no;
   float marks[5];
   float percent;
};  

 struct student St1,St2;

2)Declaring Structure variables with structure definition

struct student
{
   char name;
   int roll_no;
   float marks[5];
   float percent;
}St1,St2;
Structure Initialization:

We can initialize the structure in the following way:

struct student
{
   char name;
   int roll_no;
   float marks[5];
   float percent;
};  

 struct student St1={"Coderzpy.com",23,{89,50,81,73,69},72.4};

struct student St1={“Coderzpy.com”,23,{89,50,81,73,69},72.4}; is the initialization statement in which all the structure student’s data are given a value.

Example
#include <stdio.h>
#include <string.h>
 
struct Books {
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};
 
int main( ) {

   struct Books Book1;        /* Declare Book1 of type Book */   struct Books Book2;        /* Declare Book2 of type Book */ 
   /* book 1 specification */   strcpy( Book1.title, "C Programming");
   strcpy( Book1.author, "Coderz py"); 
   strcpy( Book1.subject, "C Programming Tutorial");
   Book1.book_id = 6495407;

   /* book 2 specification */   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Zara Ali");
   strcpy( Book2.subject, "Telecom Billing Tutorial");
   Book2.book_id = 6495700;
 
   /* print Book1 info */   printf( "Book 1 title : %s\n", Book1.title);
   printf( "Book 1 author : %s\n", Book1.author);
   printf( "Book 1 subject : %s\n", Book1.subject);
   printf( "Book 1 book_id : %d\n", Book1.book_id);

   /* print Book2 info */   printf( "Book 2 title : %s\n", Book2.title);
   printf( "Book 2 author : %s\n", Book2.author);
   printf( "Book 2 subject : %s\n", Book2.subject);
   printf( "Book 2 book_id : %d\n", Book2.book_id);

   return 0;
}
Output:
Book 1 title : C Programming
Book 1 author : Coderz py
Book 1 subject : C Programming Tutorial
Book 1 book_id : 6495407
Book 2 title : Telecom Billing
Book 2 author : Zara Ali
Book 2 subject : Telecom Billing Tutorial
Book 2 book_id : 6495700

Note: also read about the Arrays in C & Two-dimensional array

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

5 days ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

6 days ago

Staircase Climbing Ways

There is a staircase with N steps, and you can ascend either 1 step or…

2 weeks ago

Autocomplete System Implementation

Build an autocomplete system that, given a query string s and a set of possible…

2 weeks ago

Job Scheduler Implementation

Design a job scheduler that accepts a function f and an integer n. The scheduler…

2 weeks ago

Largest Sum of Non-Adjacent Numbers

Problem Statement (Asked By Airbnb) Given a list of integers, write a function to compute…

2 weeks ago