In this tutorial section Lets learn how to declare and use structure variable in c programming.
Structure Variable: Structure variable in the C programming is a variable that is a group of different data items or members. A Structure variable is also a composite object made up of simple data items or members and structured objects.
The members in structure are identified by names.
Structure variable can be declared after the structure definition.
Structure variable is used to access structure members.
Structure variable declaration is similar to the declaraion of normal variable.
Structure variable can be declared in the following ways.
1. Declaring Structure Variable Separately.
2. Declaring Structure Variable with structure Definition.
3. Declaring multiple Structure Variable.
Declare Structure Variable in C Programming.
Let us study in detail the structure variable declaration from the given diagram.
The structure variable can be defined by three ways.
1. Individual structure variable declaration in main() using struct keyword.
2.Declaring a structure variable when the structure is defined.
3. Multiple Structure variable declaration using struct keyword.
Before to start learning the structure variable we need to keep in mind the naming convensions used for structure variable.
Structure Variable Naming Conventions.
i. Start off your structure variable with the most important parameter.
This will allow you to organise Structure variable alphabetically or systematically.
ii. Use relevant words in structure variables names to provide description and context.
iii. Keep the structure variable name a reasonable length.
iv. Do not use special characters and spaces for naming structure variables.
1. Individual structure variable declaration in main() using struct keyword.
#include<stdio.h>
#include<string.h>
struct employee
{
// structure definition
int empno;
char name[15];
float salary;
} ;
int main()
{
struct employee e1;
// individual structure variable
e1.empno=100;
strcpy(e1.name,"Denis");
e1.salary=8560.34;
printf("\n Employee info");
printf("\n Employee Number=%d",e1.empno);
printf("\n Employee Name=%s",e1.name);
printf("\n Employee Salary=%.2f",e1.salary);
return(0);
}
Output:
Employee info
Employee Number=100
Employee Name=Denis
Employee Salary=8560.34
2.Declaring a structure variable when the structure is defined.
C program to define and use the structure variable in C.
#include<stdio.h>
#include<string.h>
struct employee
{
// structure definition
int empno;
char eName[20];
float salary;
} emp1, emp2; // structure variables when structure is defined
int main()
{
emp1.empno = 202;
strcpy(emp1.eName, "Raj");
emp1.salary = 8045.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: 202
Employee Name: Raj
Employee Salary:8045.50
3. Multiple Structure variable declaration using struct keyword.
C program to define and use the structure variable in C.
#include<stdio.h>
#include<string.h>
struct product
{
// structure definition
int pid;
char pName[20];
float price;
};
int main()
{
struct product p1,p2; // multiple structure variables.
p1.pid = 40;
strcpy(p1.pName, "Parle-G");
p1.price = 10.5;
printf("Product 1 information");
printf(" ID: %d\n", p1.pid);
printf(" Name: %s\n",p1.pName);
printf(" Price: %.2f\n",p1.price);`
p2.pid = 240;
strcpy(p2.pName, "Maggi");
p2.price = 20.00;
printf("Product 2 information");
printf(" ID: %d\n", p2.pid);
printf(" Name: %s\n",p2.pName);
printf(" Price: %.2f\n",p2.price);
return(0);
}
Output:
Product 1 information
ID: 40
Name: Parle-G
Price: 10.5
Product 2 information
ID: 240
Name:Maggi
Price:20.00
Previous Topic:-->> Define Structure in C || Next topic:-->>Type def Structure
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