×

Structure in C

Why Structure came into the picture?

In the actual world, we deal with entities that are collections of objects rather than tiny bits of data like integers, characters, floats, etc. So, if we want to deal with such type of entities, like a student, so, in that case, we will need different types of data like the student’s name, Student's roll no, student ‘s percentage, etc.

Therefore, in this circumstance, we require a data type that can store different types of data, including character, integer, float, and others, in a single variable.

So, as a solution, C provides a data type called Structure for dealing with such types of distinct or even similar types of data.

What is Structure?

A structure is a keyword that produces customized or non-primitive data types.

It is a grouping of different or comparable types of data.

When we define a structure, we create a new data type that is user specified.

No memory is consumed in defining Structure.

When we build variables using user-defined data types that are formed at the time of defining a structure, these variables take some memory space which depends upon the member variables.

Moreover, Struct is a keyword that defines a structure and creates new data types.

How to Create a Structure?

Syntax:

struct book
{
 int book_pages;
char book_name;
float book_price;
};
  • We use the struct keyword for defining structure.
  • Here book is the user-defined data type created by the struct keyword.
  • Three variables, book_pages, book_name, and book_price, are enclosed in curly brackets and have the data types int, char, and float, respectively.
  • The semicolon (;) follows the last curly bracket to end the sentence.

In this way, a structure is created.

How to Declare a Structure variable?

A Structure variable can be declared in one of two ways.

  1. We can declare variables after the structure definition.
struct date
{
 int d,m,y;
} date1;

  We can declare multiple variables with the separation of commas in this way

 struct date
{
 int d,m,y;
} date1,date2,date3;
  • We can declare variables after structure declaration using the struct keyword.
struct player
{
    char name[50];
    int phone_no;
   
};
 
int main()
{
 
    struct player Hrithik; 
    return 0;
}

    In the above syntax,

  • we declared a variable “Hrithik” using the struct keyword. 
  • Here data type is “player”.

Initializing of Structure Variable:

1. We can initialize a structural variable at the moment of declaration, just like any other variable in C.

Like an array, we assigned our values with the separation of commas in curly     braces.

Syntax

struct student
{
 char student_name;
int student_rollno;
float student_percentage;
}s1 ={“Omshiv”,72,96.2};

2. In initialization, we always remember assigning the values to the variable's name respectively.

We can also initialize the variable after the structure declaration 

Syntax

struct student
{
 char student_name;
int student_rollno;
float student_percentage;
};
int main()
{
struct student s1 ={“Omshiv”,72,96.2};
}

How to access structure elements?

Let's look at how to access the structure's components as now the structure type and structure variables have been declared.

Using a subscript, we can access specific array elements in arrays. But in the case of structure, for accessing the element or member variables, we use the dot(.) operator.

Member variables have no identity; they are present in the structure variable. So, to target them, we use the dot operator.

Examples: Let’s take some examples for better understanding:

Program-1

#include<stdio.h>
struct date
{
 int d,m,y;
};


int main()
{
 struct date m1;
  m1.d=19;
  m1.m=9;
  m1.y=2022;
printf("The given date is: ");
printf("%d/%d/%d",m1.d,m1.m,m1.y);
  return 0;
}

Explanation:

  • Structure declaration begins with the struct keyword.
  • Here we declared a Data type “date” using the struct keyword.
  • In structure declaration, we have 3 member variables with integer data types.
  • In the main function, we declared a variable m1 using the struct keyword, m1 has a “date” data type.
  • For accessing the member variables, we use the dot operator.
  • We assign some values to our member variables.
  • Due to the dot operator, these values are directly assigned to the member variables.

Program Output:

Structure in C language

Program-2:

#include <stdio.h> 
#include<string.h>
struct worker{    
  char worker_name[15];
  int worker_age;
  int worker_salary;
};
int main()
{
  struct worker human; 
  
strcpy(human.worker_name, "Swapnil");
  human.worker_age = 25;
  human.worker_salary =6000;
  printf("See your details \n");
  printf("Name :%s \n Age :%d \nSalary :%d \n", human.worker_name,human.worker_age,human.worker_salary);
  return 0;
}

Explanation:

  • Here we created the “worker” data type using the struct keyword.
  • In the structure declaration, we have 3 variables.
    char worker_name[15];
    int worker_age;
    int worker_salary;
  • In main, we declare a variable “human” using struct keyword, and human has a worker data type.
  • human.worker_name is assigned with value“Swapnil”. The function'strcpy()' copies the string referred to by the source to the destination.
  • A value “25” is assigned to human.worker_age.
  • A value “6000” is assigned to human.worker_salary.

Program Output:

Structure in C language

Related Topics

Memory layout in C

Memory layout in C The C language is designed so that it becomes easier for a programmer to decide the amount of memory they want to use in a program. C program...

4 minutes read.

How to Avoid Structure Padding in C

Generally, when an object of some structure type is declared, some contiguous memory will be allocated in block form, which will be allocated to structure members. To understand structure padding...

3 minutes read.

String in C

String is a collection of character or group of characters. In array, string of character is terminated by a null value “\0” and enclose between double quote. We can declare...

2 minutes read.

Functions in C

What is function? Functions are defined as the set of announcements which take inputs, perform operations and provide results. The operation of a function only performs when the function gets called....

4 minutes read.

What is the main in C?

In the C programming language, "main" is a predefined term or function. It is the primary function in every C program and is in charge of beginning and terminating the...

6 minutes read.

While-Loop in C

Syntax of While Loop The syntax that has been used for the while loop in C programming language is: while (termination condition) {   // the body of the loop  } Working of While-Loop Initially, we...

3 minutes read.

Difference between rand() and srand() function in C

What is the rand()?The rand() means random function. This function is used in C. It generates random numbers in the range of 0 to the RAND_MAX. Suppose we generate a...

3 minutes read.

C vs Java Strings

String in C In C, we can define a string as a bunch of characters. A character array is distinguished from a string by the presence of the special character '\0'...

5 minutes read.

Call by Value and Call by Reference in C

Call by reference and call by value are two different ways of passing arguments to a function in C programming language. Call by reference means that the called function is...

4 minutes read.

Static in C

Static is a keyword that is used in the C programming language. It can be used both as variables and as functions. In other words, it can be declared both...

3 minutes read.

String literals in C

Constants refer to the fixed values of the program that may not alter during the compiling ad execution. These fixed values are known as literals. In other words, the values...

3 minutes read.

Run Time Polymorphism in C

What is polymorphism? Polymorphism refers to the existence of various forms. Polymorphism can be simply defined as a message's capacity to be presented in multiple forms. An individual who can have...

4 minutes read.

Recursion in C

Recursion in C: In the C programming language, the concept known as recursion exists that is a technique in which a function calls itself either directly or indirectly. It allows...

4 minutes read.

Bank Account System in C using File Handling

This tells about the creation of bank account system using the C language and handling of files in C. Approach Let us see the approaches and the functions how they are covering...

5 minutes read.

Nested Structure in C

In C language, we can create nested Structure (Structure within Structure). There are two ways to define a nested structure. By separate structure By Embedded structure   Separate structure In a separate...

1 minute read.

Errors in C

Errors in C Errors are nothing but problems or faults that pretty much occur in all the programming languages. Errors make the behavior of the program seem abnormal, and even the...

4 minutes read.

Ceil and Floor in C

In arithmetic, a rational number is a number that can be expressed as the quotient p/q of two integers. Where q is zero. The set of rational numbers includes all...

6 minutes read.

Java Array vs ArrayList

As we all know, arrays are linear data structures that allow you to add elements to them continuously in memory address space, whereas ArrayList is a Collection framework class. Despite...

4 minutes read.

Type Conversion in the C

The process of changing one data type to another in the C programming language is known as "type conversion." The type conversion method is only executed on data types that...

4 minutes read.

fopen() function in C

fopen() function, is one of the file handling functions. It is used to open the existing file and perform operations on the file. If the file does not exist, it...

3 minutes read.