On Wed, 30 Jul 2003, Mark A. Odell wrote:
http://remus.rutgers.edu/~rhoads/Code/crc-32b.c
I grabbed this and was confused by this function's use of 'int char'.
unsigned long get_crc( FILE *fp) /* calculate the crc value */
{
register unsigned long crc;
int char; <------------------ NOTE
crc = 0xFFFFFFFF;
while ((char = getc(fp)) != EOF)
crc = ((crc>>8) & 0x00FFFFFF) ^ crc_table[ (crc^char) & 0xFF ];
return( crc^0xFFFFFFFF );
}
What's with the NOTE'd line. Could someone really have published a
function with this sort of error?
Yes.

That is absolutely ridiculous. Maybe the OP should look
somewhere else for more reliable code, since this was obviously
never tested in a real program.
The lines
#ifndef FILE /* if FILE type not defined */
#include <stdio.h> /* then bring it in */
#endif
should also have raised several red flags. I'm going to mail
the guy right away and point it out.
http://www.google.com/search?q=crc32+c+source&btnI=I'mFeelingLucky
-Arthur
From my email response:
The line "int char" is *not* a syntax error! In fact there is a very
good reason for using a variable of type "int" instead of "char." The
function "getc" can return any value from the character set *plus*
the special non-character value EOF. According to the C standard, the
type "char" is guaranteed to contain enough bits to hold all of the
values corresponding to the machine's character set; it is *not*
guaranteed to be able to hold the additional EOF value (without
possibly mixing up EOF with some actual character value). Thus, to
ensure that the code works across all implementations, you need to
declare the variable as an "int" instead of a "char." For
portability reasons, it is good practice to always declare character
data as type "int" except when you are declaring an array of
characters.
(Also, the lines
#ifndef FILE
#include <stdio.h>
#endif
are suspicious; the #ifndef and #endif directives
are completely useless and irrelevant there.)
I disagree with this claim too. There are two distinct methods of
making use of this crc code as part of a system library. First, you
may want to compile the crc code separately to generate an object
file only and then make the object file available as part of your
library while hiding the actual source code. Alternatively, you may
want to make the actual source code available and have the user
compile their source code and the crc source code together at once.
To use the first method, you need to include "stdio.h" so the
compiler will not complain about the type "FILE." But if include
"stdio.h" without the preprocessor directives #ifndef and #endif,
then you will run into a problem using the second method if your own
source code file also includes "stdio.h." Since I cannot know in
advance which method someone might use, I used the above method
to let you to use either method without running into a problem.
Having the #ifndef and #endif directives will not hurt you if your
preferred method does not need them.