Structure and Union 393
1Z8 typedef
By using typedef we can create new data type. The statement typedef is to be used while defining
the new data type. The syntax is as given under.
ty p e d ef type dataname;
Here, type is the data type and dataname is the user defined name for that type,
typedef in t hours;
Here, hours is another name for int and now we can use hours instead of int in the program as
follows.
hours hrs;
Examples based on the above are illustrated as under.
12.16 Write a program to create user-defined data type hours on int data type and use it in
the program.
td e fin e H 60
m ain()
{
ty p ed ef in t hours;
hours h rs;
c l r s c r ( ) ;
printf ("Enter Hou rs:");
scanf ("% d ,&hrs);
printf ("nMinutes = %d",hrs*H);
printf ("nSeconds = % d",hrs*H *H );
}
Qirrru T;
Enter Hours: 2
Minutes = 120
Seconds = 7200
Explanation In the above example with typ e d ef we have declared hours as an integer data
type. Immediately after the ty p ed ef statement hrs is a variable of hours data type which is
similar to in t. Further the program calculates minutes & seconds using hrs variable.
12.17 Write a program to create string data type.
mainQ
{
typ ed e f char s t r in g [2 0 ]j
strin g a»* H ello *,b ;
c lrs c rO ;
putsC'Enter Your Name
gets(b);
printf (" % s % s",a,b);
394 Programming and Data Structures
q u it ij x -
Enter Your Name: KAM AL
HelloKAMAL
Explanation In the above program s t r i n g [20] is user defined character data type. It defines
two variables a & b having 20 characters space for each.
Similarly, we can also use ty p e d ef for defining the structure. One of its example is as given under.
12.18 Create user defined data type from structure. The structure should contain the variables
such as cha^ int etc By using these variables display name, sex & acno. of an employee
aainO
{
typedef struct
{
char name[2 0 ];
char s e x [2 ];
in t acno;
}in £o;
info «qpolyee»{ "Sanjay*, " I T , 125} j
clrscr ();
printf ( tiNamet SextAJc N o . n ");
printf ("%st",empolyee.name);
printf ( " % $t"& n polyee*ex);
printf ( %dn"^mpolyee.acno);
I
OUTPUT:
Name Sex A/c No.
Sanjay M 125
Explanation In the above program info is another user-defined name for defining the structure.
Here info is used for defining the structure variables such as employee's name, sex, and age etc.
The user can understand the rest of the program.
12.19 Create user defined data type from structure. The structure should contain the variables
such as char, int etc. By using these variables display name, sex & acno. of two
employees. Use array of structures.
main ()
{
typedef s tru c t
{
char name[ 20] ;
Structure and Union 395
char sex [2];
Int acno;
}in fo ;
info enpolyee [2];
int k;
c lrs c r O ;
fo r (k=0;k<2;k++)
I
printf ("N a m e o f the Em ployee:");
scanf ("% s " fempolyee[k].name)i
printf ("S e x
scanf ("%s",empolyee[kbsex);
printf ( “Ale N o. :" );
scanf ("%d",& em polyee[k].aato);
I
printf (" n N a m e t Sext Ale N o .n ");
fo r (k=0;k<2;k++)
I
print)t("% s t"rempolyee[k].name);
printf ( " %st",empolyee[k].sex);
printf ( " % dn "/empolyee[k].acno);
I
OUTPUT:
Name of the Employee : AJAY
Sex : M
A/cNo. : 122
Name of the Employee : ANITA
Sex : F
A/cNo. : 124
NAME SEX A/C NO.
AJAY M 122
ANITA F 124
Explanation In the above program using typedef statement user-defined data type info is created.
The in fo data type contains two characters and one integer field. An array employee [2 ] is declared
based on the info data type. The f i r s t for loop and scanf () statements within it reads data. The
second fo r loop and the p rin tf () statements within it displays the contents of the array on the
screen.
396 Programming and Data Structures
1220 Write a program to define the structure containing the details of the employee. The
structure may contain first, middle, last name, place, city & pincode. Use typedef to
create data type. Display the records of two employees.
main()
{
int j;
t^padef struct
char first[20];
char middle[20];
char last [15];
char city[15];
int pincode;
} name;
name person[2];
clrscr();
fo r (f=0,j<2,-j++)
I
printf ("nR ecord 'N o .: % d ”,j+ l);
printf ( nFirst Name
scan)f(“°/os",person[j].first);
printf ("M iddle N a m e:");
scanf ( %s",person [j].middle);
printf ("L a st N a m e:”);
scanf ("%s",person[j].last);
printf ("C ity & Pincode");
scanf ("% s %d",person[jj.city,&person[j].pincode);
I
fo r (j=0;j<2;j++)
I
printf ("nnFirst Name : %s",person[j].first);
printf CnMiddle Name : %s",person[j].middle);
printf ( nLast Name : % s",person[j].last);
printf (" n C ity & Pincode: %s - %d",person[j1.city,person[j].pincode);
)
O U TP UT :
Record N o .: 1
First Name : Jay
Middle Name : Mohan
Last Name : Deshmukh
City & Pincode : Nanded - 431 602
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.70.185