Structure in C

Structure is a user-defined data type in C programming that allows you to combine different data types to store a particular type of record. It helps to construct a complex data type in a more meaningful way.

Structure

Structure is a user-defined data type in C which allows you to combine different data types to store a particular type of record. Structure helps to construct a complex data type in more meaningful way. It is somewhat similar to an Array. The only difference is that array is used to store collection of similar datatypes while structure can store collection of any type of data.


Program Structure

Program

#include<stdio.h>
#include<conio.h>
void main()
{
struct str //'str' is name of Structure.
{
void fun() //this is function.
{
printf("This is fun Function\n");
}
void fun2() //this is function.
{
printf("This is fun2 Function");
}
};
str obj; //'obj' is object of Structure 'str'.
obj.fun(); //with the help of object 'obj' calling 'fun()' function.
obj.fun2(); //with the help of object 'obj' calling 'fun2()' function.
getch();
}
Output
This is fun Function
This is fun2 Function

Program Structure Another Method

Program

#include<stdio.h>
#include<conio.h>
void main()
{
struct str //'str' is name of Structure.
{
void fun() //this is function.
{
printf("This is fun Function\n");
}
void fun2() //this is function.
{
printf("This is fun2 Function");
}
};
str obj, obj2; //'obj' and 'obj2' is objects of Structure 'str'.
obj.fun(); //with the help of object 'obj' calling 'fun()' function.
obj2.fun2(); //with the help of object 'obj2' calling 'fun2()' function.
getch();
}
Output
This is fun Function
This is fun2 Function

Program Structure Another Method
Program

#include<stdio.h>
#include<conio.h>
struct str //'str' is name of Structure.
{
void fun() //this is function.
{
printf("This is fun Function\n");
}
void fun2() //this is function.
{
printf("This is fun2 Function");
}
};
void main()
{
str obj, obj2; //'obj' and 'obj2' is objects of Structure 'str'.
obj.fun(); //with the help of object 'obj' calling 'fun()' function.
obj2.fun2(); //with the help of object 'obj2' calling 'fun2()' function.
getch();
}
Output
This is fun Function
This is fun2 Function