A structure is a user defines data type, which is a combination of pre-defined data types. A structure can be declared as follows:
struct
dept
{
int
deptid;
char
deptname[20] ;
float
avgEmpSal;
};
The structre name says
dept followed by the keyword struct. Then inside the structure
you can place the predefined data types that together forms a user defined
type. Usually we call these pre-defined types insdie the structure as data
members.
The below statement
creates the user defines variable of type dept.
struct
dept sales;
To acces the members
use the period operator on the variable sales like as shown
below:
sales.deptid = 101;
Below is the example
of using a stucture:
#include
"stdafx.h"
#include
<stdio.h>
#include
<conio.h>
struct
dept
{
int
deptid;
char
deptname[20] ;
float
avgEmpSal;
};
int
_tmain(int argc, _TCHAR* argv[])
{
struct
dept sales;
//struct dept purchase;
sales.deptid = 101;
sprintf(sales.deptname, "%s",
"Sales Dept");
sales.avgEmpSal = 150000.45f;
printf("Printing
the Structure Data");
printf("%s
has spent %f$ for salary",
sales.deptname, sales.avgEmpSal );
getch();
}
Output:

Comments
Join the conversation! Your thoughts help the community grow.