quick.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

   

space.gif

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Enumerated Types

Enumerated types are a user-defined list of named integer constants. They can either be defined in the global name space, or in a class. The same name, therefore, can be used in two different name spaces (for example, enum PKT_TYPE can be defined in a class , and defined in the global name space).

   

space.gif

The width of an enumerated data type is the same as the width of the integer data type, which is 32 bit. Enumerated type names, and the element names defined in them, have global scope, unless defined in a class. Different enumerated types cannot share the same element name. Elements within enumerated type definitions are numbered consecutively, starting from 0 (or, alternatively, the explicit value assigned to the first element). Each element may have an optional explicit value assigned to it. The explicit value assigned to an element affects values of subsequent elements that do not have an explicit assignment. That is, the subsequent element is set to the value of the previous element plus one.

   

space.gif

Syntax enum enum_type = list; or

   

space.gif

enum enum_type {list};

   

space.gif

Is the name of the enumerated type. It is used to assign list values to variables.

   

space.gif

list

   

space.gif

Is a list of enumerated values separated by commas. They are assigned sequential integer values in the order listed. Variables of the enumerated type can then be declared. Syntax enum_type variable_name [= initial_value]; enum_type C the name of an enumerated type declaration.

   

space.gif

  • variable_name : must be a valid identifier.
  • initial_value : Is the initial value for the variable. The enumerated type declaration lists the valid values. If this specification is omitted, the initial value of the declared variable is undefined (or unknown, x, in integer assignment). An initial value of x or z is not allowed.
  • Enumerated values specified in the list of an enumerated type declaration are assigned consecutive integers, starting from 0. In the above example, red is assigned 0, green is assigned 1, and so on.
   

space.gif

The enumerated values can be specified in several ways:

   

space.gif

  • name: An enumerated value, name, is defined and assigned the next consecutive integer after the previous enumerated value.
  • name[N]: N number of enumerated values are defined with names in the sequence, name0, name1,..., nameN-1, where N must be a constant integer. The defined enumerated values (name0, name1,..., nameN-1) are assigned consecutive integers, starting from the previous enumerated value plus 1.
  • name[N:M]: A set of enumerated values are defined with names in the sequence starting with nameN and counting up (nameN+1, nameN+2,...) or counting down (nameN-1, nameN-2,...) to nameM, where N and M must be constant integers. The defined enumerated values (nameN,..., nameM) are assigned consecutive integers, starting from the previous enumerated value plus 1.
  • name=N: This assigns the constant N to name.
   

space.gif

   

space.gif

  ../images/main/bullet_star_pink.gif Example : Enumerated Types
   

space.gif


  1 enum pkt_size {NORMAL, RUNT, OVERSIZE};
  2 enum pkt_type {UNICAST=11,MULTICAST,BROADCAST};
  3 
  4 program enum_t {
  5   pkt_size size = NORMAL;
  6   pkt_type type = MULTICAST;
  7   // Print the enum value
  8   printf("Packet size is %d\n", size);
  9   printf("Packet type is %d\n", type);
 10   // Print the enum name
 11   printf("Packet size is %s\n", size);
 12   printf("Packet type is %s\n", type);
 13 }
You could download file enum_t.vr here
   

space.gif

  ../images/main/bullet_star_pink.gif Simulation : Enumerated Types
   

space.gif

 Packet size is           0
 Packet type is          12
 Packet size is NORMAL
 Packet type is MULTICAST
   

space.gif

   

space.gif

   

space.gif

   

space.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

  

Copyright © 1998-2014

Deepak Kumar Tala - All rights reserved

Do you have any Comment? mail me at:deepak@asic-world.com