Structures in C


Structure is a user defined datatype. It is used to combine the different types of data into single type. It can have multiple members and structure variables. The keyword “struct” is used to define structures in C language. Structure members can be accessed by using dot(.) operator.

Here is the syntax of structures in C language,

struct structure_name {
   member definition;
} structure_variables;

Here,

structure_name − Any name given to the structure.

member definition − Set of member variables.

structure_variable − This is the object of structure.

Here is an example of structures in C language,

Example

 Live Demo

#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
", (data.i));    printf( "Memory size occupied by data : %d\t%d", sizeof(data), sizeof(data1));    return 0; }

Output

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

In the above program, a structure Data is created with the objects of structure. The variable declared in structure is called in main() by using the object of structures.

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

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 26-Jun-2020

786 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements