Friday, October 16, 2009

The struct Data Type

In C, a structure collects different data items in such a way that they can be referenced as a single unit. The general
form to declare a structure is
struct struct_tag {
data_type1 variable1;
data_type2 variable2;
data_type3 variable3;
.
.
.
};
Here struct is the keyword used in C to start a structure declaration. struct_tag is the tag name of the structure.
variable1 , variable2, and variable3 are the members of the structure. Their data types are specified respectively by
data_type1, data_type2, and data_type3. The declarations of the members have to be enclosed within the opening and
closing braces ({ and }) in the structure declaration, and a semicolon (;) has to be included at the end of the
declaration.
The following is an example of a structure declaration:
struct automobile {
int year;
char model[8];
int engine_power;
float weight;
};
Here struct is used to start a structure declaration. automobile is the tag name of the structure. In the example here,
there are three types of variables: char, int, and float. The variables have their own names, such as year, model,
engine_power, and weight. They are all members of the structure, and are declared within the braces ({ and }).

No comments:

Post a Comment