Structures introduction in c

structures are user defined data types. We can store multiple variables of any data type in one place .each variables within the structure is called structure member. They can hold any number of variables. The general syntax of declaring structure is given below

syntax:

struct <name>
{
<data type><member1>;
<data type><member2>;
.......
.......
<data type><member3>;
}
<struct variables>;

in the above syntax,name refers to the name of the structure,i.e; the name of the new data type. Struct variables are the names of structures variables observe the following example

example:

struct student {
int roll_no;
char name[20];
char course[20];
int marks_obtained ;
};
struct student stud1, stud2 ;

accessing the members of a structure:

individual structure members can be accessed by dot operator(.) between structure name and variable name.

<structure variable name>. <member-name>;

example:

struct value{
int x;
int y;
}first,second

for assigning first values we use

first.x=30;

first.y=60;

for assigning second values we use

second.x=23;

second.y=345;

program example:

#include
struct student
{
int roll_no;
char name[20];
char course[20];
int marks_obtained;
};
main()
{
struct student s1;
printf ("Enter the student roll number:");
scanf ("%d",&s1.roll_no);
printf ("\nEnter the student name: ");
scanf ("%s",s1.name);
printf ("\nEnter the student course");
scanf ("%s",s1.course);
printf ("Enter the student percentage\n");
scanf ("%d",&s1.marks_obtained);
printf ("\nData entry is complete");
printf ("\nThe student roll no is %d",s1.roll_no);
printf ("\nThe student name is %s",s1.name);
printf ("\nThe student course is %s",s1.course);
printf ("\nThe student percentage is %d",s1.marks_obtained);
}

output:

Enter the student roll number:4
Enter the student name: walker
Enter the student course cse
Enter the student percentage
35
Data entry is complete
The student roll no is 4
The student name is walker
The student course is cse
The student percentage is 35

initializing structures:

we can assign values to the structures just as initiating arrays. The structure declaration is followed by an equal sign and alist of initialization values is separated by commas and enclosed in braces.

lets see one example

#include
struct student
{
int roll_no;
char name[20];
char course[20];
int marks_obtained;
};
main()
{
struct student s1={21,"akhil","cse",99};
printf ("\nThe student roll no is %d",s1.roll_no);
printf ("\nThe student name is %s",s1.name);
printf ("\nThe student course is %s",s1.course);
printf ("\nThe student percentage is %d",s1.marks_obtained);
}