In this tutorial section Lets learn the definition of structure.
Structure: Group of dissimilar data types or Collection of heterogeneous elements is known as structure.
The struct keyword is used to define the structure in C language followed by the name of the structure.Inside the structure has the list of the structure member variables (fields) along with their data types. Here's an example:
Syntax:
struct Name_of_structure
{
dataType struct_member1;
dataType struct_member2;
dataType struct_member3;
...
};
Define Structure in C Programming.
Let us study in detail the definition of structure from the given diagram.
struct: The struct is the keyword in C programming which one is used to declare a new data-type. The meaning of struct is to grouping variables together.
name_of_structure:
name_of_structure is the name of the structure which is used to define stucture.The name of structure is specified after the keyword struct. The name of structure should be unique.
data_Type:
The data type is the one of the data type in c that indicates the type of the data members of the structure. A structure members can have different data types. A structure can have data members of different data types.
struct_member:
This is the name and data type of the data member of the structure . Inside structure Any number of data members can be defined along their data types. Each data members is occupies a separate space in the memory.
Declaration of Structure Variables with Structure Definition.
Declaring a structure variable this way is suitable only when there are need to declare few variables.
struct employee
{
// structure definition
int empno;
char eName;
float salary;
} emp1, emp2; // structure variables
The structure variables emp1 and emp2 are declared at the end of the structure definition, just before terminating the structure. In the above definition, emp1 and emp2 are the variables of the structure employee. These variables will be allocated separate copies of the structure’s data members that are- empno, eName, and salary.
C program to define and use the structure in C.
#include<stdio.h>
#include<string.h>
struct employee
{
// structure definition
int empno;
char eName[20];
float salary;
} emp1, emp2; // structure variables
int main()
{
emp1.empno = 101;
strcpy(emp1.eName, "Ajinkya");
emp1.salary = 8545.5;
printf("Employee Number: %d\n", emp1.empno);
printf("Employee Name: %s\n", emp1.eName);
printf("Employee Salary: %.2f\n", emp1.salary);`
}
Output:
Employee Number: 101
Employee Name: Ajinkya
Employee Salary:8545.50
Previous Topic:-->> Array vs Structure in C || Next topic:-->>Declare Structure variable
Other Topics:
Variables and Identifiers
Relational Operators
if-else statements
Switch case
While Loop
Infinite while Loops
C FOR Loop
Infinite for Loops
Continue in Loops
One Dimensional Array
Two Dimensional Arrays
Read and Display 2D Arrays
Types of functions
Passing Array To Functions
Nesting of Function
Array vs Structure
Array of Structure
Structures and Functions
Structures Within Structures
Use Of Pointers In C
File Handling In C
Loops FAQ
Arrays FAQ
count vowels in a file
Function FAQ
Conditional Statements Assignments
For Loops Assignments
Arrays Assignments
Function Assignments
Structure Assignments
Pointers Assignments
Files Assignments
Storage classes Assignments
Binary Files
count words,lines in a file
Copy files
Update File
Continue in Loops
break in Loops
Difference Between While and Do while
difference while do..while & for
malloc
calloc
Storage Classes
Operators MCQ
Conditional Statements MCQ
Loops MCQ
Arrays MCQ
Function MCQ
Structure MCQ
Pointers MCQ
Files MCQ
Storage classes MCQ