In C programming we need to store different types of data types together then we need help of “structure”. A structure is a container provide user to put together there data, type of data maybe int, flot or char. For example you have a bookstore. In that bookstore you have many books. Every book have different price, different author, different name and different number of pages. So here we find that
Price is “float”
Author is “string”
Name is “string”
Number of Page is “int”
You might think of using array. But array is more complex than structure. In a single structure we can put all type of data together.
Let’s write a program on structure
Now let’s see the output for this program
Price is “float”
Author is “string”
Name is “string”
Number of Page is “int”
You might think of using array. But array is more complex than structure. In a single structure we can put all type of data together.
Let’s write a program on structure
#include<stdio.h>
void main()
{
struct bookshop
{
char name[20];
char author[20];
float price;
int pages;
};
struct bookshop b1,b2;
printf("Enter name,author,price and pages for first books?\n");
scanf("%s %s %f %d",&b1.name,&b1.author,&b1.price,&b1.pages);
printf("Enter name,author,price and pages for second books?\n");
scanf("%s %s %f %d",&b2.name,&b2.author,&b2.price,&b2.pages);
printf("Your entered data is\n");
printf("%s %s %.2f %d\n",b1.name,b1.author,b1.price,b1.pages);
printf("%s %s %.2f %d\n",b2.name,b2.author,b2.price,b2.pages);
}
In above program we write a simple “structure” program. First we declare a structure named “bookshop”. Syntax of declaring a “structure” isvoid main()
{
struct bookshop
{
char name[20];
char author[20];
float price;
int pages;
};
struct bookshop b1,b2;
printf("Enter name,author,price and pages for first books?\n");
scanf("%s %s %f %d",&b1.name,&b1.author,&b1.price,&b1.pages);
printf("Enter name,author,price and pages for second books?\n");
scanf("%s %s %f %d",&b2.name,&b2.author,&b2.price,&b2.pages);
printf("Your entered data is\n");
printf("%s %s %.2f %d\n",b1.name,b1.author,b1.price,b1.pages);
printf("%s %s %.2f %d\n",b2.name,b2.author,b2.price,b2.pages);
}
struct structure_name
{
data_type variable_name;
data_type variable_name;
.
.
data_type variable_name;
};
There is no limits how many element you want to put inside a structure. Once your structure created then you need to declare some variable for your structure that can access structure element. In this program we use b1 and b2 as a variable. b1 and b2 can have access to all element in structure. In above program we declare structure and structure variable differently. We can declare theme together. Then the syntax became{
data_type variable_name;
data_type variable_name;
.
.
data_type variable_name;
};
struct bookshop
{
char name[20];
char author[20];
float price;
int pages;
}b1,b2;
We can set variables data directly in declaration by this syntax{
char name[20];
char author[20];
float price;
int pages;
}b1,b2;
struct bookshop b1={“code”,”Henry”,300,120);
Now you need to access the data from structured variable. For that “b1.name” can access the “name” in structure for b1. Same way you can access “b1.author” for “author” and so on. In this way you can put and retrieve data in structure.Now let’s see the output for this program
Enter name,author,price and pages for first books?
Mask Json 300 230
Enter name,author,price and pages for second books?
Sky Mike 100 200
Your entered data is
Mask Json 300.00 230
Sky Mike 100.00 200
Mask Json 300 230
Enter name,author,price and pages for second books?
Sky Mike 100 200
Your entered data is
Mask Json 300.00 230
Sky Mike 100.00 200


0 comments:
Post a Comment