Publications
of Jon Jagger
jon@jaggersoft.com
Appeared in CVu 9.5, July 1997

{ yourself }
Silent Changes

When you write code you tend to assume that the code you write is the same code that the compiler compiles. However, as a previous { yourself } noted, this is not so, since the pre-processor gets a chance to silently change your carefully crafted code. While this is the most obvious silent change that can occur, it is by no means the only one.

One of the best known silent changes is the default int, which is usually seen in the context of a function return type.

uncertain(float epsilon)
{
	...
}
and the poor reader has to wade through the body of the function to see if an int is actually returned.
int certain(float epsilon)
{
	...
}
Incidentally, the default int rule can occur in many other contexts. For example, the following horrors are all legal:
typedef Int_alias;
volatile v;
const c = 42;
typedef Int_array[];
Fortunately they are also rare.

Another silent change occurs whenever a C compiler performs a promotion. For example.

double tolerance(double delta)
{
	...
}
double lo = tolerance(0);        /* 0.0 is better? */
In this case the zero parameter is an int, which is silently promoted to a double. A much more common example of promotion (though you may never have realized it) lives inside the humble printf.
int printf(const char * format, ...);
char ch = 'a';
printf("%c", ch);
The char ch in the above call will be silently promoted to an int. This really does happen. If you have to implement printf in C, the code to extract a char will have to extract an int first and then cast the int to a char.
static void printf_specifier(va_list * pap, char code)
{
    switch (code)
    {
    case 'c' :  /* from "%c" */
        {
        int i = va_arg(*pap, int);
        char c = (char)i;
        break;
	}
    ...
    }
    ...
}

int printf(const char * format, ...)
{
    va_list ap;
    va_start(ap, format);
    ...
    printf_specifier(&ap, format[i]);
    ...
    va_end(ap);
    ...
}
When you take a long hard look, there are many more cases in C when a silent change occurs.

Experienced programmers know that while a compiler does eventually read your code, this is really of very little consequence at all! It is much more important to remember that people have to read your code. Reading code is hard, especially if you didn't write the code yourself (or you did, but a long time ago). Strive to write code which is easy for other people to read. As a final thought, remember that comments are meant to be read as well. Writing good comments is as hard as writing good code. However, you can at least make the physical process of reading the comments a little easier; simply use more line-breaks, shortening the length of each line, thus aiding the eye as it scans left and down to the next line. Editors have long known this which is one of the reasons the column format of newspapers (and certain other smaller publications!) endures.

That's all for now.
Cheers
Jon Jagger
jon@jaggersoft.com