C Tutorial
In programming, we usally deal with collection of ints, chars and floats rather than isolated entities.
Example an entity book is a collection of things like a title, an author, total pages, date of publication and price etc..
Structure is user defined data type which combines dissimilar data types into an entity.
Strucure syntax,
struct <name> { //data-type member1; //data-type member2; ... //data-type membern; };
Sample c program which defines the structure for book entity, reads the mutiple structure variables and prints the each structure variable of the book entity.
#include<stdio.h> void main() { struct book { int id; char title[30], author[40]; float price; }; struct book b1, b2, b3; printf("Enter book id, title, auther and price \n"); scanf("%d %s %s %f", &b1.id, b1.title, b1.author, &b1.price); scanf("%d %s %s %f", &b2.id, b2.title, b2.author, &b2.price); scanf("%d %s %s %f", &b3.id, b3.title, b3.author, &b3.price); printf("\nbook id\ttitle \t auther \t price \n"); printf("%d\t%s\t%s\t%f\n", b1.id, b1.title, b1.author, b1.price); printf("%d\t%s\t%s\t%f\n", b2.id, b2.title, b2.author, b2.price); printf("%d\t%s\t%s\t%f\n", b3.id, b3.title, b3.author, b3.price); }
Output:
$ cc structure.c $ ./a.out Enter book id, title, auther and price 1 book1 author1 30 2 book2 author2 40 3 book3 author3 55 book id title auther price 1 book1 author1 30.000000 2 book2 author2 40.000000 3 book3 author3 55.000000
book structure has different data type members like int, string and float.
Creates the multiple structure variable b1, b2 and b3.
Reads the input for each structure variables and prints the each structure variable as row.
this c program explains how to declare the structure and create structure variable definition.
#include<stdio.h> void main() { struct book { int id; char title[30], author[40]; float price; }; struct book b1 = {1, "book1", "author1", 20.5}; printf("\nbook id\ttitle \t auther \t price \n"); printf("%d\t%s\t%s\t%f\n", b1.id, b1.title, b1.author, b1.price); }
Output:
$ cc structure.c $ ./a.out $ cc structure.c $ ./a.out book id title auther price 1 book1 author1 20.500000
In C programming, we can also combine structure declaration and structure variable definition in one statement instead of doing as above.
#include<stdio.h> void main() { struct book { int id; char title[30], author[40]; float price; }b1 = {1, "book1", "author1", 20.5}; printf("\nbook id\ttitle \t auther \t price \n"); printf("%d\t%s\t%s\t%f\n", b1.id, b1.title, b1.author, b1.price); }
Output:
$ cc structure.c $ ./a.out $ cc structure.c $ ./a.out book id title auther price 1 book1 author1 20.500000
In c programming, the value of one structure variable can be copied to another same structure variable using assignment operator.
#include<stdio.h> void main() { struct book { int id; char title[30], author[40]; float price; }b2, b1 = {1, "book1", "author1", 20.5}; // structure variable copy b2 = b1; printf("\nbook id\ttitle \t auther \t price \n"); printf("%d\t%s\t%s\t%f\n", b1.id, b1.title, b1.author, b1.price); printf("%d\t%s\t%s\t%f\n", b2.id, b2.title, b2.author, b2.price); }
Output:
$ cc structure.c $ ./a.out book id title auther price 1 book1 author1 20.500000 1 book1 author1 20.500000
If we update one of the structure variable after copy statement, both structure variable will not be impacted.
#include<stdio.h> void main() { struct book { int id; char title[30], author[40]; float price; }b2, b1 = {1, "book1", "author1", 20.5}; b2 = b1; b2.price = 35.0; printf("\nbook id\ttitle \t auther \t price \n"); printf("%d\t%s\t%s\t%f\n", b1.id, b1.title, b1.author, b1.price); printf("%d\t%s\t%s\t%f\n", b2.id, b2.title, b2.author, b2.price); }
Output:
$ cc structure.c $ ./a.out book id title auther price 1 book1 author1 20.500000 1 book1 author1 35.000000
C Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us
| Report website issues in Github
| Facebook page
| Google+ page