Structure in C 1
Structure in C 1
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;
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
Example:-
int main( )
{
/* book 2 specification */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;
return 0;
}