Structure data type


Structure:
  • It is used to store different data items under a common name.
  • It is opposite to the array because the array stored only similar data items.
Rules:
  • A structure must end with a semicolon.
  • Usually, a structure appears at the top of the main program.
  • Each structure must be terminated.
  • The structure variables must be accessed with structure variable with the dot operator.
Syntax:

struct structure_name
{
data_type variable_name 1;
data_type variable_name 2;
.................................
.................................
data_type variable_name  n;
};
struct structure_name  short_name;

Example program structure:

#include<stdio.h>
#include<conio.h>
//Structure declaration
struct std
{
int roll_num,t,e,m,s,so,tot;
char name[30];
float avg;
};
struct std s;//strucure short name declare
void main()
{
clrscr();
printf("\nEnter student roll number : ");
scanf("%d",&s.roll_num);//calling structure varaiable via strcure 
                                             short name
printf("\nEnter student name : ");
scanf("%s",&s.name);
printf("\nEnter student five subject mark: ");
scanf("%d %d %d %d %d",&s.t,&s.e,&s.m,&s.s,&s.so);
s.tot=s.t+s.e+s.m+s.s+s.so;
printf("\nTotal mark: %d ",s.tot);
s.avg=s.tot/5;
printf("\nAverage mark: %f ",s.avg);
getch();
}

Output:

User defined data types:
‘C’ provides a capability that enables the programmer to assign an alternate name to a data type.
This is done with a statement known as a typedef.
Syntax:

 typedef  type dataname;
dataname short_name;

Example program for typedef:

#include<stdio.h>
#include<conio.h>
#define D 7
void main()
{
typedef int weeks;//declare typedef
weeks wk;//new datatype called weeks
clrscr();
printf("\nEnter weeks:");
scanf("%d",&wk);
printf("\nThe number of days %d in %d weeks",wk*D,wk);
getch();
}

Output:

Nested structure:
  • If a structure contains more than one structure as its members, is known as a nested structure i.e. structure within another structure.
  • It is used to reliability of the program by reading the complexity.

Note: Members are nothing but, if variables are function inside of the main program called members.
Syntax for nested structure members can be accessed as:

Outer_structure_name.inner_strucure_name.inner_structure_member

Example program for if:

/* Write a C program detail about emloyee*/
#include<stdio.h>
#include<conio.h>
void main()
{
int expe=2020;//To calculate working experience
struct date//Inner structure
{
int day,year;
char month[20];
};
struct employee//outer structure
{
int emp_id,salary;
char name[30];
struct date doj;//date strcture defined in outer structure
};
struct employee emp;
clrscr();
printf("\nEnter employee id: ");
scanf("%d",&emp.emp_id);
printf("\nEnter employee name: ");
scanf("%s",&emp.name);
printf("\nEnter employee salary: ");
scanf("%d",&emp.salary);
printf("\nEnter date of joining: ");
//Nested structure of members calling
scanf("%d %s %d",&emp.doj.day,&emp.doj.month,&emp.doj.year);
printf("\nEmployee Name : %s",emp.name);
printf("\nEmployee Employee id: %d",emp.emp_id);
printf("\nEmployee Salary : %d",emp.salary);
printf("\nEmployee DOJ : %d %s %d ",emp.doj.day,emp.doj.month,
                                                                                           emp.doj.year);
expe=expe-emp.doj.year;
printf("\nWorking experience %d years",expe);
getch();
}

Output:
Share:

No comments:

Post a Comment

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me