Some examples to show to use structure to define types
Anonymous structure
In the main function of the following program we define
anonymous structure. The anonymous structure has no name.
After the declaration of anonymous structure we create two
variable x
and y
, whose type is
the anonymous structure.
#include <stdio.h>
//from www . jav a 2s. co m
int main(void)
{
struct {
int a;
int b;
} x, y;
x.a = 10;
y = x; /* assign one structure to another */
printf("%d", y.a);
return 0;
}
Student structure
In the following code we create a structure of student. It student structure has name and student number, two fields.
struct student {//www .ja v a 2 s . c o m
char name[30];
float number;
}student1, student2;
int main() {
struct student student3;
char s1[30];
float f;
scanf ("%s", s1);
scanf ("%f", &f);
student1.name = s1;
student2.number = f;
printf ("Name = %s \n", student1.name);
printf ("Number = %f \n", student2.number);
}