To declare the structure variable, C has provided a keyword struct which represents the structure. The syntax for the declaring the structure variable is:
struct tag
{
datatype variable;
datatype variable;
……………………
};
struct tag structure_variable;
Here tag is optional. We can declare the structure variable without tag.
struct structure_variable
{
datatype variable;
datatype variable;
……………………
};
For example:
struct school
{
int number;
char name[25];
float marks;
};
struct school student;
In this example, school is tag which represents that there is a set of different data such as number, name, and marks in a single variable school. And the last one statement tells the computer that student is the structure variable that has all the attributes or properties of the variable student. Here the variables that are declared inside the structure are called member variable. Upper one example can be compressed as:
struct school
{
int number;
char name[25];
float marks;
}student;
Meaning of the both declaration is same.
Without using tag, we can also declare the structure variable as:
struct student
{
int number;
char name[25];
float marks;
};
No Comments