Miscellaneous Data Types

Oracle Certification Program Candidate Guide


In this chapter we will discuss how user defined names can be associated to data types and how enumerated data can be handled.

7.1 USER DEFINED DATA TYPES (typedef)

The typedef feature allows the user to define new data types. The type name thus introduced can then be used to declare variables, arrays, structures etc. in the usual manner. The syntax for defining new type name is,

typedef ;

Here the indicates the existing data type and refers to the new data type created by the user.

Example 7.1 :

typedef int count;

The type name count can be used to declare variables of this type.

count i,j; /* this is equivalent to int i,j;*/

7.2 ENUMERATED DATA

C supports enumerated data. Its members are constants, that are defined as identifiers. These constants determine the values that can be assigned to the associated variables.

Syntax :

enum {,,....,};

Example 7.2 :

enum day {sunday,monday,tuesday,wednesday,thursday,friday,saturday};

Here the members of the tag day are constants. Though these members are written as identifiers they have signed integer values. Thus a variable of type day can take up values, like sunday, monday, tuesday etc. The integer values that can be assigned for sunday would be 0, for monday 1, and so on.

Such automatic assignments of integer values can be overridden during declaration.

Example 7.3 :

enum day { sunday=-1, monday, tuesday, wednesday, thursday, friday, saturday};

Here the assigned values would be

sunday = -1

monday = 0

tuesday= 1

wednesday=2

:

saturday =5

A variable of type day can be defined in the usual manner.

{

:

day today,tomorrow;

/* assignment*/

today=wednesday;

:

:

if(today == wednesday)

tomorrow=thursday;

:

}

The following example illustrates type definitions using enumerated data types.

Example 7.4 :

typedef day {monday, tuesday, wednesday, thursday, friday, saturday} working_day;

working_day today;

The following example illustrates how the concept of enumerated data can be used to create boolean data types. It may be recalled C does not support boolean data values. In C true is interpreted as having non-zero value. In the following we have assigned value 1 to true and zero to false.

Example 7.5 :

/* A student is allowed to appear in the final examination provided he has atleast 75% of class attendance and has cleared the fees*/

# include

typedef enum bool {false=0,true=1} boolean;

main()

{

boolean x,y,z;

char rep=‘y’,nm[31],clrd=‘ ’;

int tot_cls=0,cls_att;

float perc;

printf (“\n Enter number of sessions:”);

scanf (“%d”, &tot_cls);

fflush (stdin);

while (rep==‘Y’ ||rep==‘y’)

{

x=y=false;

printf(“\n Enter student name :”);

gets (nm);

fflush(stdin);

do {

printf (“\n Enter sessions attended (Total Sessions %d):”,tot_cls);

scanf(“%d”, &cls_att);

fflush(stdin);

if(cls_att>tot_cls)

printf(“\tWrong Input \n”);

}while (cls_att>tot_cls);

perc=((float)cls_att*100)/tot_cls;

printf(“\nPercentage of attendance :52f\n”,perc);

if (perc>=75)

x=true;

printf(“\nFees cleared (Y/N):”);

scanf(“%c”, &clrd);

fflush(stdin);

if(clrd == ‘Y’||clrd==‘y’)

y=true;

z=x&&y;

if(z)

printf(“\n\n\nStudent %s is allowed to appear in the final examination\n”,nm);

else

printf(“\n\n\nStudent %s is not allowed to appear in the examination\n”,nm);

printf(“\nContinue (Y/N) :”);

scanf(“%c”, &rep);

fflush(stdin);

}

}

1. Explain the purpose of the following program segment.

int score = 0;

enum compass move;

...

switch (move) {

case north:

score += 10;

break;

case south:

score +=28;

break;

case east:

score +=30;

break;

case west:

score +=40;

break;

default;

printf(“ERROR - Please try again \n”);

}

2. In the following enumeration declaration, determine the value of each member:

enum compass {north = 2, south, east = 1, west};

3. Define an enumeration type called money, having the following members: panch, das, bis, panchis, panchas paisa and ek_rupiyah. Assign the following integer values to these members:

panch_paisa 5

das_paisa 10

bis_paisa 20

panchis_paisa 25

panchas_paisa 50

ek_rupiyah 100

4. Define an enumeration variable called coins, of type money. Assign the initial value panchis_paisa to coins.

0 comments: