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

Structure in C 1

Uploaded by

adityaraj46466
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Structure in C 1

Uploaded by

adityaraj46466
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Structure in C

Structure
 Similarly structure is another user defined data type available in C that allows to combine
data items of different kinds.
 Structures are used to represent a record.
 Example:-
 Suppose you want to keep details of students.
 To keep details of students we need attributes like
o Name_of_students
o Roll_No.
o Branch
o Mobile no.
o Aadhar card number
o Total Marks obtain.
 To keep track of all attributes of students in a single deceleration we need structure.
 The above maintained attributes are called member of structure.
 Structure is another user defined data type.
How to define a Structure
 To define a structure, you must use the struct statement.
 The struct statement defines a new data type, with more than one member.
 Syntex:-
struct Name_of_Structure
{
member definition;
member definition;
...
member definition;
};
 Example:-
Struct Students
{
Int Roll_No;
Char Name[50];
Char father_Name[50]
Int Mobile_no;
Float marks;
};
Declaring Structure Variable
 We can declare a variable for the structure so that we can access the member of the structure
easily. There are two ways to declare structure variable:
 By struct keyword within main() function.
 By declaring a variable at the time of defining the structure.
 First method
struct employee
{
int id;
char name[50];
float salary;
};
Now write given code inside the main() function.
struct employee e1, e2;

 Second Method
struct employee
{
int id;
char name[50];
float salary;
}e1,e2;

Accessing members of structure


 To access any member of a structure, we use the member access operator (.)
 The member access operator is coded as a period between the structure variable name and
the structure member that we wish to access.
 Example:-
#include <stdio.h>
#include <string.h>

struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
 Example:-
int main( )
{

struct Books Book1; /* Declare Book1 of type Book */


struct Books Book2; /* Declare Book2 of type Book */
/* book 1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;

/* book 2 specification */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;

/* print Book1 info */


printf( "Book 1 title : %s\n", Book1.title);
printf( "Book 1 author : %s\n", Book1.author);
printf( "Book 1 subject : %s\n", Book1.subject);
printf( "Book 1 book_id : %d\n", Book1.book_id);

/* print Book2 info */


printf( "Book 2 title : %s\n", Book2.title);
printf( "Book 2 author : %s\n", Book2.author);
printf( "Book 2 subject : %s\n", Book2.subject);
printf( "Book 2 book_id : %d\n", Book2.book_id);

return 0;
}

You might also like