Prev Next
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 and Data types can be int, char, float, double and long double etc.
C Structure:
Syntax | struct student { int a; char b[10]; } |
Example | a = 10; b = “Hello”; |
C Variable:
int | Syntax: int a; Example: a = 20; |
char | Syntax: char b; Example: b=’Z’; |
C Array:
int | Syntax: int a[3]; Example: a[0] = 10; a[1] = 20; a[2] = 30; a[3] = ‘\0’; |
char | Syntax: char b[10]; Example: b=”Hello”; |
Below table explains following concepts in C structure.
- How to declare a C structure?
- How to initialize a C structure?
- How to access the members of a C structure?
Using normal variable | Using pointer variable |
Syntax: struct tag_name { data type var_name1; data type var_name2; data type var_name3; }; |
Syntax: 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; }; |
Example: struct student { int mark; char name[10]; float average; }; |
Declaring structure using normal variable: struct student report; |
Declaring structure using pointer variable: struct student *report, rep; |
Initializing structure using normal variable: struct student report = {100, “Mani”, 99.5}; |
Initializing structure using pointer variable: struct student rep = {100, “Mani”, 99.5}; report = &rep; |
Accessing structure members using normal variable: report.mark; report.name; report.average; |
Accessing structure members using pointer variable: 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. You can check “C – Array of Structures” to know how to store and access these data for many students.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <stdio.h> #include <string.h> struct student { int id; char name[20]; float percentage; }; int 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); return 0; } |
Output:
Id is: 1
Name is: Raju Percentage 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <stdio.h> #include <string.h> struct student { int id; char name[20]; float percentage; } record; 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(" Percentage is: %f \n", record.percentage); return 0; } |
Output:
Id is: 1
Name is: Raju Percentage 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// 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(" Percentage is: %f \n", record.percentage); return 0; } |
Output:
Id is: 1
Name is: Raju Percentage is: 86.500000 |
Uses of structures in C:
- C Structures can be used to store huge data. Structures act as a database.
- C Structures can be used to send data to the printer.
- C Structures can interact with keyboard and mouse to store the data.
- C Structures can be used in drawing and floppy formatting.
- C Structures can be used to clear output screen contents.
- C Structures can be used to check computer’s memory size etc.
Continue on C – Array of Structures….
Continue on C – Passing structure to function….
Continue on C – Structure using Pointer….
Continue on C – Structure within Structure….
Continue on C – Structure Memory Allocation….
Continue on C – Structure Padding….