Our website is made possible by displaying online advertisements to our visitors.Please consider supporting us by disabling your ad blocker.

# Structure in C

C Structure is a collection of different data types which are grouped together and each element in a C structure is called member.
• If you want to access structure members in C, structure variable should be declared.
• Many structure variables can be declared for same structure and memory will be allocated for each separately.
• It is a best practice to initialize a structure to null while declaring, if we don’t assign any values to structure members.

#### Difference between C variable, C array and C structure:

• A normal C variable can hold only one data of one data type at a time.
• An array can hold group of data of same data type.
• A structure can hold group of data of different data types
• Data types can be int, char, float, double and long double etc.
Datatype

#### C structure

SyntaxExampleSyntaxExampleSyntaxExample
intint aa = 20int a[3]a[0] = 10
a[1] = 20
a[2] = 30
a[3] = ‘\0′
struct student
{
int a;
char b[10];
}
a = 10
b = “Hello”
charchar bb=’Z’char b[10]b=”Hello”

#### Below table explains following concepts in C structure.

1. How to declare a C structure?
2. How to initialize a C structure?
3. How to access the members of a C structure?
 Type Using normal variable Using pointer variabe Syntax struct tag_name { data type var_name1; data type var_name2; data type var_name3; }; struct tag_name { data type var_name1; data type var_name2; data type var_name3; }; Example struct student { int  mark; char name[10]; float average; }; struct student { int  mark; char name[10]; float average; }; Declaring structure variable struct student report; struct student *report, rep; Initializing structure variable struct student report = {100, “Mani”, 99.5}; struct student rep = {100, “Mani”, 99.5}; report = &rep; Accessing structure members report.mark report.name report.average report  -> mark report -> name report -> average

#### Example program for C structure:

This program is used to store and access “id, name and percentage” for one student. We can also store and access these data for many students using array of structures.
#include <stdio.h>
#include <string.h>
#include<conio.h>
struct student
{
int id;
char name[20];
float percentage;
};
void main()
{
struct student record = {0};
//Initializing to null
record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
printf(" Id is: %d \n", record.id);
printf(" Name is: %s \n", record.name);
printf(" Percentage is: %f \n", record.percentage);
getch();
}

Output:
Id is: 1Name is: RajuPercentage is: 86.500000

Example program – Another way of declaring C structure:
In this program, structure variable “record” is declared while declaring structure itself. In above structure example program, structure variable “struct student record” is declared inside main function which is after declaring structure.

#include <stdio.h>
#include <string.h>
#include<conio.h>
struct student
{
int id;
char name[20];
float percentage;
} record;
void main()
{
record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
printf(" Id is: %d \n", record.id);
printf(" Name is: %s \n", record.name);
printf(" Percentage is: %f \n", record.percentage);
getch();
}

Output:
Id is: 1Name is: RajuPercentage is: 86.500000

C structure declaration in separate header file:
In above structure programs, C structure is declared in main source file. Instead of declaring C structure in main source file, we can have this structure declaration in another file called “header file” and we can include that header file in main source file as shown below.

Header file name – structure.h
Before compiling and executing below C program, create a file named “structure.h” and declare the below structure.
struct student{int id;char name[20];float percentage;} record;

Main file name – structure.c:
In this program, above created header file is included in “structure.c” source file as #include “Structure.h”. So, the structure declared in “structure.h” file can be used in “structure.c” source file.

// File name - structure.c
#include <stdio.h>
#include <string.h>
#include "structure.h"   /* header file where C structure is
declared */

int main()
{
record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
printf(" Id is: %d \n", record.id);
printf(" Name is: %s \n", record.name);
printf(" \tPercentage is: %f \n", record.percentage);
return 0;
}

Output:
Id is: 1Name is: Raju    Percentage is: 86.500000

Uses of C structures:

1.  C Structures can be used to store huge data. Structures act as a database.
2. C Structures can be used to send data to the printer.
3. C Structures can interact with keyboard and mouse to store the data.
4. C Structures can be used in drawing and floppy formatting.
5. C Structures can be used to clear output screen contents.
6. C Structures can be used to check computer’s memory size etc.