Thursday, 5 April 2012

Structure, Declaration, Declaring Structure Variable


Structure:-
          A variable declared in ‘C’ holds data of one type at one time. Arrays hold a number of data items of the same type. But if we want to process many data items of different types together as a unit, them array is not useful. In order to solve this problem, C provides a data type, called structure which enables us to store and process different data types at the same time.
          Def- A structure is a collection of one or more variables grouped as a unit under a single name for easy processing.
          The construct structure is mainly used to store a “record” that contains different data types.

Declaration:-
          Syntax:-
                             struct name
{
data_type mem1;
data_type mem2;
-----------------
-----------------
data_type mem-n;
};
          In this declaration, struct is a keyword, ‘name’ is the name that identifies structure of this type and mem1, mem2, mem-n, are individual member declarations.
Ex:-
#include<stdio.h>
void main()
{
struct simple
{
int num;
char ch;
};
struct simple ez1;
ez1.num=2;
ez1.ch=’z’;
printf(“\n ez %num= %d,ez1.ch=%c\n”,ez1.num,ez1.ch);
}

Declaring Structure Variable:-
          After the declaration of structure data type, we may declare one or more variables to be of that type. As in above example ez1 is the variable of type simple.

No comments:

Post a Comment