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

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago