English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Structures in C

A structure is a user-defined data type. It is used to combine different types of data into a single type. It can have multiple members and structure variables. The keyword 'struct' is used to define a structure in C. The dot (.) operator can be used to access structure members.

This is the syntax of the C language structure,

struct structure_name {
   member definition;
}; structure_variables;

Here,

structure_name-ToOf the structureAny name.

Member definition-Set of member variables.

structure_variable-This is the object of the structure.

This is an example of a C language structure,

Example

#include <stdio.h>
#include <string.h>
struct Data {
   int i;
   long int f;
data, data1;
int main() {
   data.i = 28;
   printf("The value of i: %d\n", (data.i));
   printf("Memory size occupied by data: %d\t%d", sizeof(data), sizeof(data)1));
   return 0;
}

Output result

The value of i: 28
Memory size occupied by data: 1616

In the above program, structure data is created using a structure object.main()Variables declared within a structure can be accessed using the object of the structure.

struct Data {
   int i;
   long int f;
data, data1;
int main() {
   data.i = 28;
   printf("The value of i: %d\n", (data.i));
   printf("Memory size occupied by data: %d\t%d", sizeof(data), sizeof(data)1));
}