Joe inherited some C code that left him scratching his head.

It doesn't start too badly:

typedef unsigned char byte;
typedef unsigned short word;

This aliases two built-in types (unsigned char and unsigned short) to byte and word, respectively. This isn't uncommon, using typedefs to give types more convenient or descriptive names is generally a good practice. The typedef is just an alias at compile time, so you're not really changing anything, just creating an easy to reference name.

As always, it's worth noting that short has a minimum size, but no maximum size defined in the spec. So assuming that a word is two bytes is potentially a mistake.

But that's not the WTF. The WTF is the next two lines, so here's the whole snippet, for context:

typedef unsigned char byte;
typedef unsigned short word;
#define byte unsigned char
#define word unsigned short

This creates a preprocessor macro that will replace all occurrences of byte with unsigned char, and all occurrences of word with unsigned short. This will run before compilation, and essentially be a find/replace. This means that by the time the compiler gets the code, there will be no bytes or words. So the typedef doesn't matter- the compiler will never see the alias used.

This code isn't the worst abuse we've seen in C, by far, but it still leaves me scratching my head, trying to understand how we got here. I could understand it better if these four lines weren't all together. One header doing a typedef, and another header doing a #define would be bad, but that's just the life of a C programmer, as you pull together modules written by different people across different years. I could understand having that happen. But right next to each other?

[Advertisement] Utilize BuildMaster to release your software with confidence, at the pace your business demands. Download today!