Skip to content

C structure tutorial | Example and Exercises

Structure is a user-defined datatype in C programming which allows to store different data types e.g. int, float, char, array and pointer etc. together.

Consider an example, we want to store information’s of a Car i.e. Brand, Model, Year and Cost.  Brand and Model are of string data types, Year is an int data type and Cost is of float data type.

We know that array in C programming is used to store homogeneous data types. Array can store either int data type or float data type etc. but cannot hold different data types. So, structure comes into the picture to store different data types in C language. Here is how structure looks like.


struct Car{
	char brand[32];//char array
	char model[32];//char array
	int year; //int data type
	float cost;//float data type 
};

How to create structure in C?

To declare a structure, we use struct keyword to create a structure. Here is the C syntax of structure definition. You can compare it with above Car structure.


struct structure_name{ 
	//variables of different data types
};

How to create / Assign / Access structure variables?

We create structure variable using struct keyword. e.g. struct structure Name var. See below example. Note that in C we must use struct keyword during creating variables of structure. in C++ it is not required.


int main()
{
	//Create variables of structure car
	struct Car c;
	
	return 0;
}

We can access the structure’s member using dot(.) operator on structure variable declared. e.g. c.brand.

Simple and Complete structure program example in C

First, let’s see the complete C structure program sample using Car structure that is how to create structure, declare structure variable, assign  values to them and accessing all those structure variables. Read the code comments.


/* Program example of structure in C*/
#include <stdio.h>

struct Car{
	char brand[32];//char array
	char model[32];//char array
	int year; //int data type
	float cost;//float data type 
};

int main()
{
	//Create variables of structure car
	struct Car c;
	//Fill structure with car informations
	strcpy(c.brand, "Maruti");
	strcpy(c.model, "Vitara Brezza");
	c.year = 2016;
	c.cost = 76000.00;


	//Display car information
	printf("Car Information:\n");
	printf("Brand: %s\n", c.brand);
	printf("Model: %s\n", c.model);
	printf("Year: %d\n", c.year);
	printf("Cost: %f\n", c.cost);


	return 0;
}

Output:
Car Information:
Brand: Maruti
Model: Vitara Brezza
Year: 2016
Cost: 76000.00

Structure initialization in C

Structure initialization can also be done at compile time if we want to initialize structures with some default values.

For example,


int main()
{
	//Initialize structure with defult values
	struct Car c = {"Maruti","Vitara Brezza",2016,76000.00 };	

	return 0;
}

So, if you don’t assign the values to a variables after initialization, then default value will be taken. for in below example, we have initialize the structure variables, but a later point of time we are changing only model of the car. So as an output default value (Vitara Brezza) of model will be changed to “Swift”.


int main()
{
	//Create variables of structure car
	struct Car c = {"Maruti","Vitara Brezza",2016,76000.00 };	

	//only change model of the car
	strcpy(c.model, "Swift");

	//display car information
	printf("Car Information:\n");
	printf("Brand: %s\n", c.brand);
	printf("Model: %s\n", c.model);
	printf("Year: %d\n", c.year);
	printf("Cost: %.2f\n", c.cost);

	return 0;
}

Here is the output with default values and assigned new value

Output:
Car Information:
Brand: Maruti
Model: Swift
Year: 2016
Cost: 76000.00


Published inC Programming Examples