Structure in C Programming Group 4
Structure in C Programming Group 4
in c Programming
GROUP 4
GROUP
MEMBERS
MUGISHA Abdoullatif 24978 NDACYAYISENGA Herve 24768
NIYITEGEKA Evode 24755 ISIMBI KARANGWA Sandra 25125
NGABOYISONGA Kevin 24769 NDAHIRO Guilaine 24846
Structure in C programming is very helpful in cases where we need to store similar data of
multiple entities. Let us understand the need for structures with a real-life example.
Suppose you need to manage the record of books in a library.
We can define a structure where we can declare the data members of different data
types according to our needs. In this case, a structure named BOOK can be created
having three members book_name, author_name, and genre. Multiple variables of the
type BOOK can be created such as book1, book2, and so on (each will have its own copy
of the three members book_name, author_name, and genre).
Syntax to Define a Structure in C
struct structName
{
// structure definition
Data_type1 member_name1;
Data_type2 member_name2;
Data_type2 member_name2;
}
Description of the Syntax :
Keyword struct: The keyword struct is used at the beginning while defining a structure in C.
StructName: This is the name of the structure which is specified after the keyword struct.
Data_Type: The data type indicates the type of the data members of the structure.
Member_name: This is the name of the data member of the structure.
Declaration of Structure Variables with Structure Definition
struct bookStore
{
// structure definition
char storeName [30];
int totalBooks;
char storeLicense[20];
storeA, storeB; // structure variables
}
The structure variables are declared at the end of the structure definition, right before
terminating the structure.
OR
Declaration of Structure Variables Separately
struct bookStore
{
// structure definition
char storeName[30];
int totalBooks;
char storeLicense[20];
}
int main()
{
struct bookStore storeA, storeB; // structure variables
}
When the structure variables are declared in the main() function, the keyword struct followed by the
structure name has to be specified before declaring the variables.
How to Init ia li ze Struc ture Me mbe rs?
#Include<stdio.h>
#Include<string.h>
struct patient
{
// data members
char P_name[10];
int P_age;
char P_gender;
}
int main()
{
// structure variables
struct patient p1, p2;
// structure variables accessing the data members.
strcpy(p1.P_name, "XYZ");
p1.P_age = 25;
How to Ini tia li z e Struc ture Members?
p1.P_gender = 'M';
strcpy(p2.P_name, "ABC");
p2.P_age = 50;
p2.P_gender = 'F’;
else
{
printf("The gender of the 1st patient is: Female\n");
}
printf("\n");
// patient 2
printf("The name of the 2nd patient is: %s\n", p2.P_name);
printf("The age of the 2nd patient is: %d\n", p2.P_age);
if (p2.P_gender == 'M')
{
printf("The gender of the 2nd patient is: Male\n");
}
else
{
printf("The gender of the 2nd patient is: Female\n");
}
return 0;
}
HOW TO ENTER VALUES IN A
STRUCTURE
#include<stdio.h>
#include<string.h>
struct person
{
char name[30];
int age;
char address[20];
char gender[20];
}
main()
CONT’D
{
struct person p1,p2;
printf("Enter name:: ");
scanf("%s",&p1.name);
printf("Enter the age:: ");
scanf("%d",&p1.age);
printf("Enter the address: ");
scanf("%s",&p1.address);
printf("Enter gender:: ");
scanf("%s",&p1.gender);
CONT’D
https://www.simplilearn.com/tutorials/c-tutorial/structure-in-c