|
|
 |
|
 |
|
|
Anonymous Type A type that does not have an associated type identifier. |
|
|
|
|
|
|
|
|
If we can create a data type in a variable declaration, why bother with a separate type declaration that creates a named type? Named types, like named constants, make a program more readable, more understandable, and easier to modify. Also, declaring a type and declaring a variable of that type are two distinct concepts; it is better to keep them separate. In addition, assignment of one enumeration type variable to another is valid only if they are both of the same named type. In the following code segment, the assignment statement is not allowed: |
|
|
|
|
|
|
|
|
enum {NICKEL, DIME, QUARTER, HALF_DOLLAR} amount;
enum {NICKEL, DIME, QUARTER, HALF_DOLLAR} thisCoin;
.
.
.
amount = thisCoin; // No |
|
|
|
|
|
|
|
|
Even though the two anonymous data types have the same domain, the compiler considers them to be two distinct data types and won't let you assign thisCoin to amount. |
|
|
|
|
|
|
|
|
We now give a more complete syntax template for an enumeration type declaration. This template shows that the type name is optional (yielding an anonymous type) and that a list of variables may optionally be included in the declaration. |
|
|
|
|
|
|
|
|
User-Written Header Files |
|
|
|
|
|
|
|
|
As you create your own user-defined data types, you sometimes find that a data type can be useful in more than one program. An example is the Boolean type we have been using in several programs. Instead of typing the statements |
|
|
|
|
|
|
|
|
typedef int Boolean;
const Boolean TRUE = 1;
const Boolean FALSE = 0; |
|
|
|
|
|