What is Structure in C Programming ?
-->  Like ARRAYS allows to define variables that can hold several data items of the same type. Similarly STRUCTURE is another user defined data type available in C Language that allows to combine data items of different Types. STRUCTURE is a collection of different data types.
Structure is more like a container which holds all kind of data and then this data is used throughout the Program.
It is used to keep a record of the objects/Variables that will be used in the program.Â
"For Better Understanding"
Let's Say there is a Book Store and in that book store there is a department which keeps the track record of all the books like name , author , Domain/Subject of the book , Availability and etc. so if anyone wants to know about any of the above information about any book the respective person can ask about the details in this department inspite of going and searching from shelf to shelf about that book.Â
Like the same way structure stores all the data of the variable and then supplies the info whenever required during the course of the program. It is More or Less like a Giant/Big container which can store all types of data, the reason behind writing the word big because the other elements which store data are like variables and etc. have limits base on their datatype but as structure is declared like a function its has no limit of storing values .
So while using the structure a user has to declare the variables and other declaration objects (if any) in the structure part only and then can use the credentials or write the definition in the main functionÂ
Syntax To Use Structure:
//COMMERCIAL METHOD
#include<stdio.h>
// defining a structure
struct structure_name
{
// all the declarations to be done here
};
// Syntax to create structure variable in Main function
int main()
{
struct structure_name variable1, variable2;
// code to written here
return 0;
}
//SECOND METHOD
#include<stdio.h>
// defining a structure
struct structure_name
{
// all the declarations to be done here
} variable1, variable2;
int main()
{
// code to be written here
return 0;
}
Example of Structure:
#include <stdio.h>
#include<string.h>
//defining structure
struct book
{
char name[30];
int id;
char writer[20];
float price;
};
//main function
int main()
{
// structure variables
struct book b1,b2,b3;
strcpy(b1.name,"Helpforcoders");
strcpy(b1.writer,"HFC");
b1.id = 1;
b1.price = 139.99;
strcpy(b2.name,"Programming");
strcpy(b2.writer,"HFC");
b2.id = 2;
b2.price = 149.99;
strcpy(b3.name,"C Language");
strcpy(b3.writer,"HFC");
b3.id = 2;
b3.price = 109.99;
// printing information of all books
printf("book 1 info\n id:%d \n name :%s \n writer:%s \n price:%f \n",b1.id,b1.name,b1.writer,b1.price);
printf("book 2 info\n id:%d \n name:%s \n writer:%s \n price:%f \n",b2.id,b2.name,b2.writer,b2.price);
printf("book 3 info\n id:%d \n name:%s \n writer:%s \n price:%f \n",b3.id,b3.name,b3.writer,b3.price);
return 0 ;
}
OUTPUT:-
Union in C
As we have already seen Structures in C then it won’t be much difficult to get the
concept of Unions in C Language.
Definition : UNION is a collection of different data types.
Unions are very much similar to Structures but there is a small difference in Union from Structures i.e. in Union we have to write Union in place of structure and the
whole syntax is the same, another difference is that the memory location is shared among the variables of Union. In the below table we have all points of difference
between Structure and Union.
Syntax To Use Union :Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
//COMMERCIAL METHOD
#include<stdio.h>
// defining a union
union union_name
{
// all the declarations to be done here
};
// Syntax to use union variable in Main function
int main()
{
union union_name variable1, variable2;
// code to be written here
return 0;
}
//SECOND METHOD
#include<stdio.h>
// defining a union
union union_name
{
// all the declarations to be done here
} variable1, variable2;
int main()
{
// code to be written here
return 0;
}
Example program of Union :Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
#include <stdio.h>
union test
{
int x, y;
};
int main()
{
union test t;
t.x = 2; // t.y also gets value 2
printf(" x = %d, y = %d\n\n", t.x, t.y);
t.y = 10; // t.x is also updated to 10
printf(" x = %d, y = %d\n\n", t.x, t.y);
return 0;
}
//ENJOY CODING\\
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP