Structure in C Programming Language
Created By Hitesh Vataliya
Structure is collection elements having different datatypes. Such as Student structure having rollno, percentage, name.
Structure is created with the help of struct keyword before name of structure tag.
rollno is int type,percentage is declared with float type,name of student is declared with char array type.
All three are known as elements of the structure.
Example,
struct student
{
int rollno;
float percentage;
char name[30];
};
student is name of structure which is known as structure tag, student is used to create element or variable of the structure.
rollno, percentage and name are not themselves as a variable. We can access them by the use of variable student type here in below example s1 and s2.
1. Write a program to create structure student.
#include<stdio.h>
#include<conio.h>
struct stud
{
int rollno;
float perc;
char name[30];
}s1;
void main()
{
struct stud s2;
clrscr();
printf("Enter rollno of student1 : ");
scanf("%d",&s1.rollno);
printf("Enter rollno of student2 : ");
scanf("%d",&s2.rollno);
printf("Enter Percentage of student1 : ");
scanf("%f",&s1.perc);
printf("Enter Percentage of student2 : ");
scanf("%f",&s2.perc);
printf("Enter name of student1 : ");
scanf("%d",s1.name);
printf("Enter name of student2 : ");
scanf("%d",s2.name);
printf("\n\n\tRollno\t\tPercentage\tName\n");
printf("\n-------------------------------------");
printf("\n\t%d\t\t%f\t%s",s1.rollno,s1.perc,s1.name);
printf("\n\t%d\t\t%f\t%s",s2.rollno,s2.perc,s2.name);
getch();
}
2. Write a program to create structure for orders of computer having orderno, price, quantity as input and generate tax amount and total amount as an output.
#include<stdio.h>
#include<conio.h>
struct computer
{
int orderno;
int qty;
int price;
float tax;
float total;
};
void main()
{
int i;
struct computer c[10];
clrscr();
for(i=0;i<3;i++)
{
printf("Enter order no : ");
scanf("%d",&c[i].orderno);
printf("How many computers ? ");
scanf("%d",&c[i].qty);
printf("Price of each computer : ");
scanf("%d",&c[i].price);
c[i].tax=(0.14*c[i].qty*c[i].price);
c[i].total=((float)c[i].qty*c[i].price)+c[i].tax;
}
printf("\n\n\tOrderno\tQuantity\tPrice\ttax\t\tTotal Amount\n");
printf("\n--------------------------------------------------------------------------");
for(i=0;i<3;i++)
{
printf("\n\t%d\t%d\t\t%d\t%f\t\t%f",c[i].orderno,c[i].qty,c[i].price,c[i].tax,c[i].total);
}
getch();
}
3. Write a program to use function with structure.
#include<stdio.h>
#include<conio.h>
struct employee
{
int empno;
char name[50];
int salary;
};
void printemp(struct employee a[],int s)
{
int i;
printf("\n\n\tEmployeeNo\tName\tSalary\n");
printf("\n-----------------------------------------------");
for(i=0;i<s;i++)
{
printf("\n\t%d\t\t%s\t%d",a[i].empno,a[i].name,a[i].salary);
}
}
void main()
{
int i,size=3;
struct employee e[70];
clrscr();
for(i=0;i<size;i++)
{
printf("Enter employeee no : ");
scanf("%d",&e[i].empno);
printf("Enter name of employee : ");
scanf("%s",&e[i].name);
printf("Enter salary of employee : ");
scanf("%d",&e[i].salary);
}
printemp(e,size);//call of function
getch();
}
If you are interested for learning C Programming Language. VISIT : WEBSITE
No comments:
Post a Comment