syque.com

The Psychology of Quality and More

| Menu | Books | Share | Search | Settings |

C Style: Standards and Guidelines (contents)

CHAPTER 9 : Data Usage

PART 4 : USAGE

CHAPTER 9 : Data Usage
9.1 Declarations
9.2 Using floating point numbers
9.3 Using 'typedef'
9.4 Using global data
9.5 Using Structures
9.6 Using Unions
9.7 Using Arrays
9.8 Using Pointers
9.9 Using bit structures
9.10 Using Constants
9.11 Using 'static' declarations
9.12 Initializing variables
9.13 Summary

<--Prev page | Next page -->

 

9.3  Using 'typedef'

Many Pascal programmers coming to C have bemoaned its lack of typing. typedef, to some extent, corrects this situation, allowing more explicitness in the declaration of variables and more understandability of the code.

9.3.1  Typing variables

Where a variable has a clear purpose, it is easy to assign a type to it:

 

typedef WEEKDAY     unsigned int;
...
WEEKDAY Today;
Today = MON;

 

A general philosophy of typedef-ing variables, can make the intent of data usage clearer, although when the typedef is not within visual scope (for example in a header file), there can be a danger of misunderstanding and incorrect use, such as a variable being typedef'd as an unsigned char, but then being assigned a number greater than 255.

Over-use of typedef's can also result in reduced readability, as there are just too many different types to remember. For example, it would probably be going too far to declare:

 

typedef LOOPCOUNT   unsigned int;
LOOPCOUNT   i;
...
    for ( i = 0; i < WINMAX; i++ )

-----------------------------------------------------------------

A compromise is to restrict the use of typedef's to a manageable set of common types used in the program.

9.3.2  Typing numbers

Another common use of typedef is to aid portability by redefining numbers:

 

typedef INT8    signed char;
typedef INT16   signed int;
typedef UINT32  unsigned long int;
...

 

Note that the numbers do not define actual integer size. On most computers char is at least 8 bits, int is at least 16 bits and long is at least 32 bits. signed and unsigned are used to explicitly show the importance of the signedness (it is necessary with char).

Note that this cannot be used for all integers, for example some standard library functions return an int, which may be of different sizes on different machines. In this case, it is more portable to not use typedef.

Boolean variables are widely used, and it is worth including a typedef (also see 9.10.3):

 

typedef BOOL    int;

 

9.3.3  'typedef' vs. '#define'

It is possible to replace typedef with #define in many situations:

 

typedef WEEK int; ..or..      #define WEEK int
WEEK   Weekday;               WEEK   Weekday;

 

Consider the underlying message: Using typedef is saying, "Weekday is of type WEEK, which happens to be represented by an int," whilst using #define is saying, "Weekday is an int." Using typedef is thus being more explicit. Using #define can also result in some nasty problems:

 

#define WEEKPTR int *
WEEKPTR pThisWeek, pNextWeek;       /* pNextWeek is an int! */

 

There are, occasionally, good reasons for using #define, such as where void is reserved, but not implemented, identifier (in which case typedef would cause an error).

9.3.4  'typedef' vs. 'struct Tag'

Structures may be defined using tags:

 

struct MESSAGE
{
    char    *Header;
    char    *Body;
};
struct MESSAGE  Msg;    /* this is a structure called MESSAGE */

 

This explicitly informs the reader that a structure is being used. On the other hand, it is also being explicit to use the typing paradigm, in which case a typedef is more appropriate:

 

typedef struct
{
    char    *Header;
    char    *Body;
} MESSAGE;
MESSAGE Msg;            /* this is a variable of type MESSAGE */

 

The only time that a structure tag must be used is in recursive references:

 

struct NODE
{
    char        *DataType;
    struct NODE *pNextNode;
};

 

<--Prev page | Next page -->

 

Site Menu

| Home | Top | Settings |

Quality: | Quality Toolbook | Tools of the Trade | Improvement Encyclopedia | Quality Articles | Being Creative | Being Persuasive |

And: | C Style (Book) | Stories | Articles | Bookstore | My Photos | About | Contact |

Settings: | Computer layout | Mobile layout | Small font | Medium font | Large font | Translate |

 

You can buy books here

More Kindle books:

And the big
paperback book


Look inside

 

Please help and share:

 

| Home | Top | Menu |

© Changing Works 2002-
Massive Content -- Maximum Speed