C – Structure
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
- Data types can be int, char, float, double and long double etc.
Datatype |
C variable |
C array |
C structure |
|||
Syntax | Example | Syntax | Example | Syntax | Example | |
int | int a | a = 20 | int 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” |
char | char b | b=’Z’ | char b[10] | 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?
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. You can check “C – Array of Structures“ to know how to store and access these data for many students.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.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;
{
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.Output:
Id is: 1
Name is: Raju Percentage is: 86.500000 |
Uses of C structures:
- 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.
No comments:
Post a Comment