declare structure variable in C Language.

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