Accessing Context-Sensitive Help

Chapter 13 - Structures, Unions, and Miscellaneous Items

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

Miscellaneous Items
There are two additional topics that should be mentioned at this point: typedef declarations and enumerated types using enum. Both typedef and enum have the capability to clarify program code when used appropriately.
Using typedef
New data types can be associated with existing data types by using typedef. In a mathematically intense program, for example, it might be necessary to use one of the following data types: fixed, whole, real, or complex. These new types can be associated with standard C types with typedef. In the next program, two novel data types are created:
/*
*   typedf.c
*   C program shows the use of typedef.
*   Two new types are created, “whole” and “real”,
*   which can be used in place of “int” and “double”.
*   Copyright (c) Chris H. Pappas and William H. Murray, 1997
*/
#include <stdio.h>

typedef int whole;
typedef double real;

int main(void)
{
 whole wvalue=123;
 real  rvalue=5.6789;

 printf(“The whole number is %d.\n”,wvalue);
 printf(“The real number is %f.\n”,rvalue);
 return (0);
}
Be aware that using too many newly created types can have a reverse effect on program readability and clarity. Use typedef carefully.
You can use a typedef declaration to simplify declarations. Look at the next two coded examples and see if you can detect the subtle code difference introduced by the typedef keyword:
struct stboat {
 char sztype [iSTRING15 + iNULL_CHAR];
 char szmodel[iSTRING15 + iNULL_CHAR];
 char sztitle[iSTRING20 + iNULL_CHAR];
 int iyear;
 long int lmotor_hours;
 float fsaleprice;
} stused_boat;
typedef struct {
 char sztype [iSTRING15 + iNULL_CHAR];
 char szmodel[iSTRING15 + iNULL_CHAR];
 char sztitle[iSTRING20 + iNULL_CHAR];
 int iyear;
 long int lmotor_hours;
 float fsaleprice;
} STBOAT;
Three major changes have taken place:
  The optional tag field has been deleted. (However, when using typedef you can still use a tag field, although it is redundant in meaning.)
  The tag field stboat has now become the new type STBOAT and is placed where structure variables have been defined traditionally.
  There now is no variable declaration for stused_boat.
The advantage of typedefs lies in their usage. For the remainder of the application, the program can now define variables of the type STBOAT using the simpler syntax
STBOAT STused_boat;
The use of uppercase letters is not syntactically required by the compiler; however, it does illustrate an important coding convention. With all of the possible sources for an identifier’s declaration, C programmers have settled on using uppercase to indicate the definition of a new type, constant, enumerated value, and macro, usually defined in a header file. The visual contrast between lowercase keywords and uppercase user-defined identifiers makes for more easily understood code since all uppercase usually means, “Look for this declaration in another file.”
Using enum
The enumerated data type enum exists for one reason only, to make your code more readable. In other computer languages, this data type is referred to as a user-defined type. The general syntax for enumerated declarations looks like this:
enum op_tag_field { val1,. . .valn } op_var_dec ;
As you may have already guessed, the optional tag field operates exactly as it does in structure declarations. If you leave the tag field off, you must list the variable or variables after the closing brace. Including the tag field allows your application to declare other variables of the tag type. When declaring additional variables of the tag type in C++, it is not necessary to repeat the keyword enum.
Enumerated data types allow you to associate a set of easily understood human symbols—for example, Monday, Tuesday, Wednesday, and so on—with an integral data type. They also help you create self-documenting code. For example, instead of having a loop that goes from 0 to 4, it can now read from Monday to Friday:
enum eweekdays { Monday, Tuesday, Wednesday, Thursday, Friday };

/
* C enum variable declaration   */
enum eweekdays ewToday;

/
* Same declaration in C++       */
eweekdays ewToday;

/
* Not using the enumerated type */
for(i = 0; i <= 4; i++)
    .
    .
    .
/
* Using the enumerated type    */
for(ewToday = Monday; ewToday <= Friday; ewToday++)
C compilers, historically speaking, have seen no difference between the data types int and enum. This meant that a program could assign an integer value to an enumerated type. In C++ the two types generate a warning message from the compiler without an explicit type cast:
/* legal in C not C++ */
ewToday = 1;

/
* correcting the problem in C++ */
ewToday = (eweekdays)1;
The use of enum is popular in programming when information can be represented by a list of integer values such as the number of months in a year or the number of days in a week. This type of list lends itself to enumeration.
The following example contains a list of the number of months in a year. These are in an enumeration list with a tag name emonths. The variable associated with the list is emcompleted. Enumerated lists will always start with zero unless forced to a different integer value. In this case, January is the first month of the year.
/*
*   enum.c
*   C program shows the use of enum types.
*   Program calculates elapsed months in year, and
*   remaining months using enum type.
*   Copyright (c) Chris H. Pappas and William H. Murray, 1997
*/

#include <stdio.h>

enum emonths {
 January=1,
 February,
 March,
 April,
 May,
 June,
 July,
 August,
 September,
 October,
 November,
 December
} emcompleted;
int main(void)
{
 int ipresent_month;
 int isum,idiff;

 printf(“\nPlease enter the present month (1 to 12): ”);
 scanf(“%d”,&ipresent_month);

 emcompleted = December;
 isum = ipresent_month;
 idiff = (int)emcompleted - ipresent_month;

  printf(“\n%d month(s) past, %d months to go.\n”,isum,idiff);

 return (0);
}
The enumerated list is actually a list of integer values, from 1 to 12, in this program. Since the names are equivalent to consecutive integer values, integer arithmetic can be performed with them. The enumerated variable emcompleted, when set equal to December, is actually set to 12.
This short program will just perform some simple arithmetic and report the result to the screen:
Please enter the current month (1 to 12): 4
4 month(s) past, 8 months to go.
The next chapter completes the coverage of standard C and C++ programming features. After completing Chapter 14, you will be ready to investigate the fundamentals of object-oriented programming, which are presented in Chapter 15.

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net