Structure programming in c

Structure is used to create user defined data type which can include multiple values of multiple data types.

Suppose we want to save information about car.The parameters to be stored are name (String),price(float) and year of purchasing(int).We will need three arrays to store the information of multiple cars.This approach will definiteley save information but the complexity will increase if the numbers of parameters increase.If we want to save owners name then we will need one more.

To overcome this situation,C provides special data type-Structure. A structure contains a number of data types grouped together. These data types may or may not be of the same type.

The above problem can be solved using structure as,

			void main()
{
	struct car
	{
		char name;
		float price;
		int year;
	};
	struct car c1,c2,c3,c4;
	printf("Enter the details of four cars\n");
	scanf("%c %f %d",&c1.name,&c1.price,&c1.year);
	scanf("%c %f %d",&c2.name,&c2.price,&c2.year);
	scanf("%c %f %d",&c3.name,&c3.price,&c3.year);
		
	printf("The car details are \n");
	printf("\n%c %f %d",c1.name,c1.price,c1.year);
	printf("\n%c %f %d",c2.name,c2.price,c2.year);
	printf("\n%c %f %d",c3.name,c3.price,c3.year);
}
			

This program shows,

1.Declaration of structure.

2.Accessing the elements of structure.

Syntax of structure,

			struct < structure name >
{
	structure element1;
	structure element2;
	structure element3;
	......
	......
};
			

Once the new structure data type has been defined one or more variables can be declared to be of that type.For example the variables c1,c2,c3 and c4 can be declared as,

struct car c1,c2,c3,c4;

This statement allocates memory for the variables c1,c2,c3 and c4.