Unit-V
STRUCTURE AND UNION
Introduction
 A variable stores a single value of a data type.
 Arrays can store many values of a similar data type.
 In real life, we need to have different data types; for example, to maintain
employees information we should have information such as name, age,
qualification, salary and so on.
 Name and qualification of the employee are char data type, age is an int
and salary is float.
Introduction (cont..)
 For tackling such a mixed data type problems, a special feature is provided
by C. It is known as a structure.
 A structure is a collection of one or more variables of different data types,
grouped together under a single name.
 It is a derived data type to be arranged in a group of related data items of
different data types.
 It is a user defined data type because the user can decide the data types
to be included in the body of a structure.
 By using structures, we can make a group of variables, arrays, pointers.
FEATURES OF STRUCTURES
 In order to copy elements of an array to another array, elements of the
same data type are copied one by one.
 It is not possible to copy all the elements at a time.
 However, in a structure it is possible to copy the contents of all structure
elements of different data types to another structure variable of its type
using assignment (=) operator.
 It is possible because the structure elements are stored in successive
memory locations.
FEATURES OF STRUCTURES
(cont..)
 Nesting of structures is possible, i.e. one can create structure within the
structure.
 It is also possible to pass structure elements to a function.
 This is similar to passing an ordinary variable to a function.
 One can pass individual structure elements or entire structure by value or
address.
FEATURES OF STRUCTURES
(cont..)
 It is also possible to create structure pointers.
 In the pointer, we have studied pointing a pointer to an int, pointing to a
float and pointing to a char.
 In a similar way, we can create a pointer pointing to structure elements.
For this it requires -> operator.
Comparison between an array and a
structure
Points of comparison Array Structure
Collection of Data Same data type Different data type
Keyword There is no keyword for
array.
struct is a keyword
Declaration and definition
of datatype
Only declaration Both declaration and
definition
DECLARATION AND INITIALIZATION
OF STRUCTURES
 Structures can be declared as follows:
struct struct_type
{
type variable1;
type variable2;
------------------
------------------
};
DECLARATION AND INITIALIZATION
OF STRUCTURES (cont..)
struct struct_type
{
type variable1;
type variable2;
------------------
------------------
};
 Structure declaration always starts with struct
keyword.
 Here, struct_type is known as tag.
 The struct declaration is enclosed within a pair of
curly braces.
 Closing brace is terminated with a semi-colon.
 Using struct and tag, a user can declare structure
variables like variable1, variable2 and so on.
DECLARATION AND INITIALIZATION
OF STRUCTURES (cont..)
 After defining structure template, we can create variables as given below:
struct struct_type v1,v2,v3;
 Here v1, v2 and v3 are variables or objects of structure struct_type.
 This is similar to declaring variables of any data type.
 The declaration defines the structure but this process does not
allocate memory.
 The memory allocation takes place only when variables are declared.
DECLARATION AND INITIALIZATION
OF STRUCTURES (cont..)
struct book1
{
char book[30];
int pages;
float price;
};
struct book1 bk1;
 In this example, a structure of type book1 is
created.
 It consists of three members: book [30] of char
data type, pages of int type and price of float data
type.
DECLARATION AND INITIALIZATION
OF STRUCTURES (cont..)
struct book1
{
char book[30];
int pages;
float price;
};
struct book1 bk1;
 struct book1 bk1;
 The above line creates variable bk1 of type
book1.
 it reserves total 36 bytes
 30 bytes for book[30],
 2 bytes for int and 4 bytes for float.
 Through bk1 all the three members of structure
can be accessed
DECLARATION AND INITIALIZATION
OF STRUCTURES (cont..)
 In order to initialize structure elements with
certain values following statement is used.
 struct book1 bk1 = {“shrinivas”,500,385.00};
 All the members of structure related to
variable bk1 can be accessed as
structure_variable.member
 The (.) sign is used to access the structure
members.
 We can directly assign values to members as
given below:
 bk1.book =”shrinivas”;
 bk1.pages=500;
 bk1.price=385.00;
Write a program to display the size of structure elements.
Use sizeof() of operator.
void main()
{
struct book1
{
char book[30];
int pages;
float price;
};
struct book1 bk1;
clrscr();
printf(“n Size of Structure Elements”);
printf(“n Book : %d”,sizeof(bk1.book));
printf(“n Pages : %d”,sizeof(bk1.pages));
printf(“n Price : %d”,sizeof(bk1.price));
printf(“n Total Bytes : %d”,sizeof(bk1));
}
Cont..
Output:-
Size of Structure Elements
Book : 30
Pages : 2
Price : 4
Total Bytes : 36
Write a program to define a structure and initialize its member
variables.
void main()
{
struct book1
{
char book[30];
int pages;
float price;
};
struct book1 bk1={“Programming in C “,600,185};
clrscr();
printf(“n Book Name : %s”,bk1.book);
printf(“n No. of Pages : %d”,bk1.pages);
printf(“n Book Price : %.2f”,bk1.price);
getche();
}
Cont..
OUTPUT:
Book Name : Programming in C
No. of Pages : 600
Book Price : 185.00
Write a program to copy structure elements from one object to
another object.
# include <string.h>
void main()
{
struct disk
{
char co[15];
float type;
int price;
};
struct disk d1={“SONY”,1.44,20};
struct disk d2,d3;
strcpy(d2.co,d1.co);
d2.type=d1.type;
d2.price=d1.price;
d3=d2=d1;
clrscr();
printf(“n %s %g %d”,d1.co,d1.type,d1.price);
printf(“n %s %g %d”,d2.co,d2.type,d2.price);
printf(“n %s %g %d”,d3.co,d3.type,d3.price);
getche();
}
Cont..
OUTPUT:
SONY 1.44 20
SONY 1.44 20
SONY 1.44 20
Write a program to read the values using scanf() and assign them to
structure variables.
void main()
{
struct book1
{
char book[30];
int pages;
float price;
};
struct book1 bk1;
clrscr();
printf(“Enter Book name, pages, price :”);
scanf(“%s”, bk1.book);
scanf(“%d”, &bk1.pages);
scanf(“%f”, &bk1.price);
printf(“n Book Name : %s”,bk1.book);
printf(“n No. of Pages : %d”,bk1.pages);
printf(“n Book Price : %.2f”,bk1.price);
getche();
}
Cont..
OUTPUT:
Enter Book name, pages, price :C 500 450
Book Name : C
No. of Pages : 500
Book Price : 450.00
Array of Structures
 we know an array is a collection of similar data types.
 In the same way, we can also define an array of structure.
 In such type of array, every element is of structure type.
 Array of structure can be declared as follow:
Array of Structures (cont..)
struct time
{
int second;
int minute;
int hour;
} t[3];
 In this example, t[3] is an array of three
elements containing three objects of time
structure.
 Each element of t[3] has structure of time
with three members that are second,
minute and hour.
Example of an array of structures that stores information of 5
students and prints it.
#include<stdio.h>
#include <string.h>
struct student{
int rollno;
char name[10];
};
void main(){
int i;
struct student st[5];
printf("Enter Records of 5 students");
for(i=0;i<5;i++){
printf("nEnter Rollno:");
scanf("%d",&st[i].rollno);
printf("nEnter Name:");
scanf("%s",&st[i].name);
}
printf("nStudent Information List:");
for(i=0;i<5;i++){
printf("nRollno:%d, Name:%s",st[i].rollno,st[i].name);
}
getch();
}
Output:
Enter Records of 5 students
Enter Rollno:1
Enter Name:Sonoo
Enter Rollno:2
Enter Name:Ratan
Enter Rollno:3
Enter Name:Vimal
Enter Rollno:4
Enter Name:James
Enter Rollno:5
Enter Name:Sarfraz
Student Information List:
Rollno:1, Name:Sonoo
Rollno:2, Name:Ratan
Rollno:3, Name:Vimal
Rollno:4, Name:James
Rollno:5, Name:Sarfraz
POINTER TO STRUCTURE
 The pointer is a variable that holds the
address of another data variable.
 The variable may be of any data type,
i.e. int, float or double.
 In the same way, we can also define
pointer to structure.
 Here, starting address of the member
variables can be accessed.
 Thus, such pointers are called structure
pointers.
POINTER TO STRUCTURE (cont..)
Example:
struct book
{ char name[25];
char author[25];
int pages;
};
struct book *ptr;
 In this example, ptr is pointer to structure
book.
 The syntax for using pointer with member is
as given below:
 (1) ptr->name (2) ptr->author (3) ptr->pages.
 By executing these three statements,
starting address of each member can be
estimated.
Write a program to declare pointer to structure and display the
contents of the structure.
void main()
{
struct book
{
char name[25];
char author[25];
int pages;
};
struct book b1={“Programming in C”, “Kamthane”, 886};
struct book *ptr;
ptr=&b1;
clrscr();
printf(“n %s by %s of %d pages”,b1.name,b1.author,b1.pages);
printf(“n %s by %s of %d pages”, ptr->name,ptr->author,ptr->pages);
}
OUTPUT:
Programming in CProgramming in C by Kamthane of 886 pages
Programming in CProgramming in C by Kamthane of 886 pages
Write a program to declare pointer as members of structure and
display the contents of the structure.
void main()
{
struct student
{
char *name;
int *age;
float *height;
};
struct student *sp;
char nm[10]=”Nency”;
int ag=17;
float ht=5.40;
sp->name=nm;
sp->age=&ag;
sp->height=&ht;
clrscr();
printf(“n Name = %s”,sp->name);
printf(“n Age = %d”,*sp->age);
printf(“n Height = %.2f”,*sp->height);
getch(); }
typedef
 We can create new data type by using typedef.
 The statement typedef is to be used while defining the new
data type.
 The syntax is as follows:
typedef type dataname;
 Here, type is the datatype and dataname is the user-defined
name for that type.
typedef (cont…)
typedef int hours;
 Here, hours is another name for int and now we can use
hours instead of int in the program as follows:
hours hrs;
Write a program to create user-defined data type hours on int data
type and use it in the program.
#define H 60
void main()
{
typedef int hours;
hours hrs;
clrscr();
printf(“Enter Hours: ”);
scanf(“%d”, &hrs);
printf(“nMinutes = %d”,hrs*H);
printf(“nSeconds = %d”,hrs*H*H);
getch();
}
OUTPUT:
Enter Hours: 2
Minutes = 120
Seconds = 7200
Write a program to create string data type.
void main()
{
typedef char string[20];
string a=” Hello ”,b;
clrscr();
puts(“Enter Your Name :”);
gets(b);
printf(“%s %s”,a,b);
}
OUTPUT:-
Enter Your Name : KAMAL
Hello KAMAL
Create a userdefined data type from structure. The structure should
contain the variables such as char, int. By using these variables,
display name, sex and acno. of an employee.
void main()
{
typedef struct
{
char name[20];
char sex[2];
int acno;
}info;
Info employee={“Ankit”,“M”,125};
clrscr();
printf(“nNamet Sext A/c No.n”);
printf(“%st”,employee.name);
printf(“ %st”,employee.sex);
printf(“ %dn”,employee.acno);
getch(); }
OUTPUT:
Name Sex A/c No.
Ankit M 125
Create a user-defined data type from structure. The structure
should contain the variables such as char, int. By using these
variables display name, sex and acno of two employees. Use array
of structures.
void main()
{
typedef struct
{
char name[20];
char sex[2];
int acno;
}info;
info employee[2];
int k;
clrscr();
for (k=0;k<2;k++)
{
printf(“ Name of the Employee :”);
scanf(“%s”,employee[k].name);
printf(“ Sex :”);
scanf(“%s”,employee[k].sex);
printf(“A/c No. :”);
scanf(“%d”, &employee[k].acno);
}
Cont..
printf(“nNamet Sext A/c No.n”);
for (k=0;k<2;k++)
{
printf(“%st”,employee[k].name);
printf(“ %st”,employee[k].sex);
printf(“ %dn”,employee[k].acno);
}
}
OUTPUT:
Name of the Employee : AJAY
Sex : M
A/c No. : 122
Name of the Employee : ANITA
Sex : F
A/c No. : 124
NAME SEX A/C NO.
AJAY M 122
ANITA F 124
ENUMERATED DATA TYPE
 The enum is a keyword.
 It is used for declaring enumeration types.
 The programmer can create his/ her own data type and define
what values the variables of these data types can hold.
 This enumeration data type helps in reading the program.
ENUMERATED DATA TYPE (cont..)
 Consider the example of 12 months of a year.
 enum month {Jan, Feb, Mar, Apr, May, Jun, Jul, Aug,Sep, Oct, Nov, Dec};
 This statement creates a user defined data type.
 The keyword enum is followed by the tag name month.
 The enumerators are the identifiers Jan,Feb, Mar, Apr, May and so on.
 Their values are constant unsigned integers and starts from 0.
 The identifier Jan referes to 0, Feb to 1 and so on.
Write a program to create enumerated data type for 12 months.
Display their values in integer constants.
void main()
{
enum month {Jan, Feb, Mar, Apr, May, June, July, Aug, Sep, Oct, Nov,
Dec};
clrscr();
printf(“nJan = %d”,Jan);
printf(“nFeb = %d”,Feb);
printf(“nJune = %d”,June);
printf(“nDec = %d”,Dec );
getch();
}
Cont..
OUTPUT:
Jan = 0
Feb = 1
June = 5
Dec = 11
Write a program to create enumerated data type for 12 months.
Initialize the first identifier with 1. Display their values in integer
constants.
void main()
{
enum month {Jan=1, Feb, Mar, Apr, May, June, July,Aug, Sep, Oct, Nov, Dec};
clrscr();
printf(“nJan = %d”,Jan);
printf(“nFeb = %d”,Feb);
printf(“nJune = %d”,June);
printf(“nDec = %d”,Dec );
}
Cont..
OUTPUT:
Jan = 1
Feb = 2
June = 6
Dec = 12
Write a program to identify the type of entered character whether it
is a letter, digit or other symbol. Use enumerated data type.
# include <ctype.h>
void main()
{
char ch;
int f;
enum ctype
{
Letter,Digit,Other
};
clrscr();
printf(“n Enter any character ”);
ch=getch();
f=isalpha(ch);
if(f!=0)
printf(“n %c is type %d symbol ”,ch,Letter);
else
{
f=isdigit(ch);
Cont..
if (f!=0)
printf(“n %c is type %d symbol ”,ch,Digit);
else
printf(“n %c is type %d symbol ”,ch,Other);
}
}
 Output:-
 Enter any character
 = is type 2 symbol
UNION
 Union is a variable, which is similar to the structure.
 It contains the number of members like structure but it holds
only one object at a time.
 In the structure, each member has its own memory location
whereas the members of unions have the same memory
locations.
 It can accommodate one member at a time in a single area of
storage.
 union also contains members of types int, float, long, arrays,
pointers.
UNION (cont..)
 It allocates fixed specific bytes of memory for access of data
types irrespective of any data type.
 The union requires bytes that are equal to the number of
bytes required for the largest members.
 For example, if the union contains char, integer and long
integer then the number of bytes reserved in the memory for
the union is 4 bytes.
Write a program to find the size of union and the number of bytes
reserved for it.
void main()
{
union result
{
int marks;
char grade;
};
struct res
{
char name[15];
int age;
union result perf;
} data;
clrscr();
printf(“Size of union : %dn”,sizeof(data.perf));
printf(“Size of Structure : %dn”, sizeof(data));
}
Cont..
OUTPUT:
Size of union : 2
Size of Structure : 19
UNION OF STRUCTURES
 We know that one structure can be nested within another
structure.
 In the same way, a union can be nested within another union.
 We can also create a structure in a union or vice versa.
Write a program to use structure within union. Display the
contents of structure elements.
void main()
{
struct x
{
float f;
char p[2];
};
union z
{
struct x set;
};
union z st;
st.set.f=5.5;
st.set.p[0]=65;
st.set.p[1]=66;
Cont..
clrscr();
printf(“n %g”,st.set.f);
printf(“n %c”,st.set.p[0]);
printf(“n %c”,st.set.p[1]);
}
OUTPUT:
5.5
A
B

Unit-V.pptx

  • 1.
  • 2.
    Introduction  A variablestores a single value of a data type.  Arrays can store many values of a similar data type.  In real life, we need to have different data types; for example, to maintain employees information we should have information such as name, age, qualification, salary and so on.  Name and qualification of the employee are char data type, age is an int and salary is float.
  • 3.
    Introduction (cont..)  Fortackling such a mixed data type problems, a special feature is provided by C. It is known as a structure.  A structure is a collection of one or more variables of different data types, grouped together under a single name.  It is a derived data type to be arranged in a group of related data items of different data types.  It is a user defined data type because the user can decide the data types to be included in the body of a structure.  By using structures, we can make a group of variables, arrays, pointers.
  • 4.
    FEATURES OF STRUCTURES In order to copy elements of an array to another array, elements of the same data type are copied one by one.  It is not possible to copy all the elements at a time.  However, in a structure it is possible to copy the contents of all structure elements of different data types to another structure variable of its type using assignment (=) operator.  It is possible because the structure elements are stored in successive memory locations.
  • 5.
    FEATURES OF STRUCTURES (cont..) Nesting of structures is possible, i.e. one can create structure within the structure.  It is also possible to pass structure elements to a function.  This is similar to passing an ordinary variable to a function.  One can pass individual structure elements or entire structure by value or address.
  • 6.
    FEATURES OF STRUCTURES (cont..) It is also possible to create structure pointers.  In the pointer, we have studied pointing a pointer to an int, pointing to a float and pointing to a char.  In a similar way, we can create a pointer pointing to structure elements. For this it requires -> operator.
  • 7.
    Comparison between anarray and a structure Points of comparison Array Structure Collection of Data Same data type Different data type Keyword There is no keyword for array. struct is a keyword Declaration and definition of datatype Only declaration Both declaration and definition
  • 8.
    DECLARATION AND INITIALIZATION OFSTRUCTURES  Structures can be declared as follows: struct struct_type { type variable1; type variable2; ------------------ ------------------ };
  • 9.
    DECLARATION AND INITIALIZATION OFSTRUCTURES (cont..) struct struct_type { type variable1; type variable2; ------------------ ------------------ };  Structure declaration always starts with struct keyword.  Here, struct_type is known as tag.  The struct declaration is enclosed within a pair of curly braces.  Closing brace is terminated with a semi-colon.  Using struct and tag, a user can declare structure variables like variable1, variable2 and so on.
  • 10.
    DECLARATION AND INITIALIZATION OFSTRUCTURES (cont..)  After defining structure template, we can create variables as given below: struct struct_type v1,v2,v3;  Here v1, v2 and v3 are variables or objects of structure struct_type.  This is similar to declaring variables of any data type.  The declaration defines the structure but this process does not allocate memory.  The memory allocation takes place only when variables are declared.
  • 11.
    DECLARATION AND INITIALIZATION OFSTRUCTURES (cont..) struct book1 { char book[30]; int pages; float price; }; struct book1 bk1;  In this example, a structure of type book1 is created.  It consists of three members: book [30] of char data type, pages of int type and price of float data type.
  • 12.
    DECLARATION AND INITIALIZATION OFSTRUCTURES (cont..) struct book1 { char book[30]; int pages; float price; }; struct book1 bk1;  struct book1 bk1;  The above line creates variable bk1 of type book1.  it reserves total 36 bytes  30 bytes for book[30],  2 bytes for int and 4 bytes for float.  Through bk1 all the three members of structure can be accessed
  • 13.
    DECLARATION AND INITIALIZATION OFSTRUCTURES (cont..)  In order to initialize structure elements with certain values following statement is used.  struct book1 bk1 = {“shrinivas”,500,385.00};  All the members of structure related to variable bk1 can be accessed as structure_variable.member  The (.) sign is used to access the structure members.  We can directly assign values to members as given below:  bk1.book =”shrinivas”;  bk1.pages=500;  bk1.price=385.00;
  • 14.
    Write a programto display the size of structure elements. Use sizeof() of operator. void main() { struct book1 { char book[30]; int pages; float price; }; struct book1 bk1; clrscr(); printf(“n Size of Structure Elements”); printf(“n Book : %d”,sizeof(bk1.book)); printf(“n Pages : %d”,sizeof(bk1.pages)); printf(“n Price : %d”,sizeof(bk1.price)); printf(“n Total Bytes : %d”,sizeof(bk1)); }
  • 15.
    Cont.. Output:- Size of StructureElements Book : 30 Pages : 2 Price : 4 Total Bytes : 36
  • 16.
    Write a programto define a structure and initialize its member variables. void main() { struct book1 { char book[30]; int pages; float price; }; struct book1 bk1={“Programming in C “,600,185}; clrscr(); printf(“n Book Name : %s”,bk1.book); printf(“n No. of Pages : %d”,bk1.pages); printf(“n Book Price : %.2f”,bk1.price); getche(); }
  • 17.
    Cont.. OUTPUT: Book Name :Programming in C No. of Pages : 600 Book Price : 185.00
  • 18.
    Write a programto copy structure elements from one object to another object. # include <string.h> void main() { struct disk { char co[15]; float type; int price; }; struct disk d1={“SONY”,1.44,20}; struct disk d2,d3; strcpy(d2.co,d1.co); d2.type=d1.type; d2.price=d1.price; d3=d2=d1; clrscr(); printf(“n %s %g %d”,d1.co,d1.type,d1.price); printf(“n %s %g %d”,d2.co,d2.type,d2.price); printf(“n %s %g %d”,d3.co,d3.type,d3.price); getche(); }
  • 19.
  • 20.
    Write a programto read the values using scanf() and assign them to structure variables. void main() { struct book1 { char book[30]; int pages; float price; }; struct book1 bk1; clrscr(); printf(“Enter Book name, pages, price :”); scanf(“%s”, bk1.book); scanf(“%d”, &bk1.pages); scanf(“%f”, &bk1.price); printf(“n Book Name : %s”,bk1.book); printf(“n No. of Pages : %d”,bk1.pages); printf(“n Book Price : %.2f”,bk1.price); getche(); }
  • 21.
    Cont.. OUTPUT: Enter Book name,pages, price :C 500 450 Book Name : C No. of Pages : 500 Book Price : 450.00
  • 22.
    Array of Structures we know an array is a collection of similar data types.  In the same way, we can also define an array of structure.  In such type of array, every element is of structure type.  Array of structure can be declared as follow:
  • 23.
    Array of Structures(cont..) struct time { int second; int minute; int hour; } t[3];  In this example, t[3] is an array of three elements containing three objects of time structure.  Each element of t[3] has structure of time with three members that are second, minute and hour.
  • 24.
    Example of anarray of structures that stores information of 5 students and prints it. #include<stdio.h> #include <string.h> struct student{ int rollno; char name[10]; }; void main(){ int i; struct student st[5]; printf("Enter Records of 5 students"); for(i=0;i<5;i++){ printf("nEnter Rollno:"); scanf("%d",&st[i].rollno); printf("nEnter Name:"); scanf("%s",&st[i].name); } printf("nStudent Information List:"); for(i=0;i<5;i++){ printf("nRollno:%d, Name:%s",st[i].rollno,st[i].name); } getch(); }
  • 25.
    Output: Enter Records of5 students Enter Rollno:1 Enter Name:Sonoo Enter Rollno:2 Enter Name:Ratan Enter Rollno:3 Enter Name:Vimal Enter Rollno:4 Enter Name:James Enter Rollno:5 Enter Name:Sarfraz Student Information List: Rollno:1, Name:Sonoo Rollno:2, Name:Ratan Rollno:3, Name:Vimal Rollno:4, Name:James Rollno:5, Name:Sarfraz
  • 26.
    POINTER TO STRUCTURE The pointer is a variable that holds the address of another data variable.  The variable may be of any data type, i.e. int, float or double.  In the same way, we can also define pointer to structure.  Here, starting address of the member variables can be accessed.  Thus, such pointers are called structure pointers.
  • 27.
    POINTER TO STRUCTURE(cont..) Example: struct book { char name[25]; char author[25]; int pages; }; struct book *ptr;  In this example, ptr is pointer to structure book.  The syntax for using pointer with member is as given below:  (1) ptr->name (2) ptr->author (3) ptr->pages.  By executing these three statements, starting address of each member can be estimated.
  • 28.
    Write a programto declare pointer to structure and display the contents of the structure. void main() { struct book { char name[25]; char author[25]; int pages; }; struct book b1={“Programming in C”, “Kamthane”, 886}; struct book *ptr; ptr=&b1; clrscr(); printf(“n %s by %s of %d pages”,b1.name,b1.author,b1.pages); printf(“n %s by %s of %d pages”, ptr->name,ptr->author,ptr->pages); } OUTPUT: Programming in CProgramming in C by Kamthane of 886 pages Programming in CProgramming in C by Kamthane of 886 pages
  • 29.
    Write a programto declare pointer as members of structure and display the contents of the structure. void main() { struct student { char *name; int *age; float *height; }; struct student *sp; char nm[10]=”Nency”; int ag=17; float ht=5.40; sp->name=nm; sp->age=&ag; sp->height=&ht; clrscr(); printf(“n Name = %s”,sp->name); printf(“n Age = %d”,*sp->age); printf(“n Height = %.2f”,*sp->height); getch(); }
  • 30.
    typedef  We cancreate new data type by using typedef.  The statement typedef is to be used while defining the new data type.  The syntax is as follows: typedef type dataname;  Here, type is the datatype and dataname is the user-defined name for that type.
  • 31.
    typedef (cont…) typedef inthours;  Here, hours is another name for int and now we can use hours instead of int in the program as follows: hours hrs;
  • 32.
    Write a programto create user-defined data type hours on int data type and use it in the program. #define H 60 void main() { typedef int hours; hours hrs; clrscr(); printf(“Enter Hours: ”); scanf(“%d”, &hrs); printf(“nMinutes = %d”,hrs*H); printf(“nSeconds = %d”,hrs*H*H); getch(); } OUTPUT: Enter Hours: 2 Minutes = 120 Seconds = 7200
  • 33.
    Write a programto create string data type. void main() { typedef char string[20]; string a=” Hello ”,b; clrscr(); puts(“Enter Your Name :”); gets(b); printf(“%s %s”,a,b); } OUTPUT:- Enter Your Name : KAMAL Hello KAMAL
  • 34.
    Create a userdefineddata type from structure. The structure should contain the variables such as char, int. By using these variables, display name, sex and acno. of an employee. void main() { typedef struct { char name[20]; char sex[2]; int acno; }info; Info employee={“Ankit”,“M”,125}; clrscr(); printf(“nNamet Sext A/c No.n”); printf(“%st”,employee.name); printf(“ %st”,employee.sex); printf(“ %dn”,employee.acno); getch(); } OUTPUT: Name Sex A/c No. Ankit M 125
  • 35.
    Create a user-defineddata type from structure. The structure should contain the variables such as char, int. By using these variables display name, sex and acno of two employees. Use array of structures. void main() { typedef struct { char name[20]; char sex[2]; int acno; }info; info employee[2]; int k; clrscr(); for (k=0;k<2;k++) { printf(“ Name of the Employee :”); scanf(“%s”,employee[k].name); printf(“ Sex :”); scanf(“%s”,employee[k].sex); printf(“A/c No. :”); scanf(“%d”, &employee[k].acno); }
  • 36.
    Cont.. printf(“nNamet Sext A/cNo.n”); for (k=0;k<2;k++) { printf(“%st”,employee[k].name); printf(“ %st”,employee[k].sex); printf(“ %dn”,employee[k].acno); } } OUTPUT: Name of the Employee : AJAY Sex : M A/c No. : 122 Name of the Employee : ANITA Sex : F A/c No. : 124 NAME SEX A/C NO. AJAY M 122 ANITA F 124
  • 37.
    ENUMERATED DATA TYPE The enum is a keyword.  It is used for declaring enumeration types.  The programmer can create his/ her own data type and define what values the variables of these data types can hold.  This enumeration data type helps in reading the program.
  • 38.
    ENUMERATED DATA TYPE(cont..)  Consider the example of 12 months of a year.  enum month {Jan, Feb, Mar, Apr, May, Jun, Jul, Aug,Sep, Oct, Nov, Dec};  This statement creates a user defined data type.  The keyword enum is followed by the tag name month.  The enumerators are the identifiers Jan,Feb, Mar, Apr, May and so on.  Their values are constant unsigned integers and starts from 0.  The identifier Jan referes to 0, Feb to 1 and so on.
  • 39.
    Write a programto create enumerated data type for 12 months. Display their values in integer constants. void main() { enum month {Jan, Feb, Mar, Apr, May, June, July, Aug, Sep, Oct, Nov, Dec}; clrscr(); printf(“nJan = %d”,Jan); printf(“nFeb = %d”,Feb); printf(“nJune = %d”,June); printf(“nDec = %d”,Dec ); getch(); }
  • 40.
    Cont.. OUTPUT: Jan = 0 Feb= 1 June = 5 Dec = 11
  • 41.
    Write a programto create enumerated data type for 12 months. Initialize the first identifier with 1. Display their values in integer constants. void main() { enum month {Jan=1, Feb, Mar, Apr, May, June, July,Aug, Sep, Oct, Nov, Dec}; clrscr(); printf(“nJan = %d”,Jan); printf(“nFeb = %d”,Feb); printf(“nJune = %d”,June); printf(“nDec = %d”,Dec ); }
  • 42.
    Cont.. OUTPUT: Jan = 1 Feb= 2 June = 6 Dec = 12
  • 43.
    Write a programto identify the type of entered character whether it is a letter, digit or other symbol. Use enumerated data type. # include <ctype.h> void main() { char ch; int f; enum ctype { Letter,Digit,Other }; clrscr(); printf(“n Enter any character ”); ch=getch(); f=isalpha(ch); if(f!=0) printf(“n %c is type %d symbol ”,ch,Letter); else { f=isdigit(ch);
  • 44.
    Cont.. if (f!=0) printf(“n %cis type %d symbol ”,ch,Digit); else printf(“n %c is type %d symbol ”,ch,Other); } }  Output:-  Enter any character  = is type 2 symbol
  • 45.
    UNION  Union isa variable, which is similar to the structure.  It contains the number of members like structure but it holds only one object at a time.  In the structure, each member has its own memory location whereas the members of unions have the same memory locations.  It can accommodate one member at a time in a single area of storage.  union also contains members of types int, float, long, arrays, pointers.
  • 46.
    UNION (cont..)  Itallocates fixed specific bytes of memory for access of data types irrespective of any data type.  The union requires bytes that are equal to the number of bytes required for the largest members.  For example, if the union contains char, integer and long integer then the number of bytes reserved in the memory for the union is 4 bytes.
  • 47.
    Write a programto find the size of union and the number of bytes reserved for it. void main() { union result { int marks; char grade; }; struct res { char name[15]; int age; union result perf; } data; clrscr(); printf(“Size of union : %dn”,sizeof(data.perf)); printf(“Size of Structure : %dn”, sizeof(data)); }
  • 48.
    Cont.. OUTPUT: Size of union: 2 Size of Structure : 19
  • 49.
    UNION OF STRUCTURES We know that one structure can be nested within another structure.  In the same way, a union can be nested within another union.  We can also create a structure in a union or vice versa.
  • 50.
    Write a programto use structure within union. Display the contents of structure elements. void main() { struct x { float f; char p[2]; }; union z { struct x set; }; union z st; st.set.f=5.5; st.set.p[0]=65; st.set.p[1]=66;
  • 51.