0% found this document useful (0 votes)
31 views

Structure in C Programming Group 4

Structures in C programming allow storing logically related data of multiple entities. A structure defines a new user-defined data type by grouping together items of possibly different types under a single name. Structures help manage records like books in a library which contain related data like book name, author name, genre. Structures are defined using the struct keyword and variables of the structure type can store data for multiple entities. Member values are initialized and accessed using the structure variable and member operator (.).
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Structure in C Programming Group 4

Structures in C programming allow storing logically related data of multiple entities. A structure defines a new user-defined data type by grouping together items of possibly different types under a single name. Structures help manage records like books in a library which contain related data like book name, author name, genre. Structures are defined using the struct keyword and variables of the structure type can store data for multiple entities. Member values are initialized and accessed using the structure variable and member operator (.).
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Structures

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

 BIMENYIMANA Angelot 24879  ISHIMWE Shakilla 24858

 IKUZWE MPFURANZIMA O’neal Dauphin 24714


 MUSHUMBA MWIZA Juste 24709
What is structure?

A structure can be defined as a single entity holding variables of different 


data types that are logically related to each other. All the data members
inside a structure are accessible to the functions defined outside the
structure. To access the data members in the main function, you need to
create a structure variable.
Why Do We Use Structures in C?

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’;

   // print the patient records.


   // patient 1

   printf("The name of the 1st patient is: %s\n", p1.P_name);


   printf("The age of the 1st patient is: %d\n", p1.P_age);
   if (p1.P_gender == 'M')
   {
      printf("The gender of the 1st patient is: Male\n");
   }
How t o Initi al iz e St ruct ure Membe rs?

   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

printf("The information entered\n");


printf("The name of the person is :%s\n",p1.name);
printf("The age is %d\n",p1.age);
printf("Address is %s\n",p1.address);
printf("Gender is %s \n",p1.gender);
return;
}
REFERENCE

 https://www.simplilearn.com/tutorials/c-tutorial/structure-in-c

You might also like