As an example, let us imagine we are designing a program to handle fractions, which are defined by an integer numerator and denominator. One could introduce two int variables to represent the numerator and denominator; alternatively, one can introduce a structure with two int members:
struct frac { int n, d; };Subsequently, in a program
struct frac f1; f1.n = 1; f1.d = 3; printf("The fraction is %d / %d\n", f1.n, f1.d);one can refer to the basic fraction entity by a single name (f1, in this case), and access individual members of the structure by the dot notation - f1.n for the numerator, and f1.d for the denominator.
Some prefer to define structures through the use of a typedef declaration:
typedef struct { int n, d; } frac;so that variables can be defined without having to specify the struct keyword:
frac f1; f1.n = 1; f1.d = 3; printf("The fraction is %d / %d\n", f1.n, f1.d);