= vs ==
if, while or for statement
is always (or is never) executed when it (apparently) shouldn't be.
= used by mistake in a conditional context instead of ==.
gcc -Wall: for a piece of code like
if (i = 1009)
printf("Why is this always printed?\n");
gcc will say something like
foo.c:42: warning: suggest parentheses around assignment used as truth valueIf you really intend to use an assignment, you can re-write it as
if ((i = 1009))
printf("Why is this always printed?\n");
and gcc will cease to complain.
& in call to scanf
scanf(), the arguments corresponding to
%d, %u, %o, %x, %i,
%n,
%e, %g, %f and %c must be pointers to
integers, floats, doubles or characters.
If you pass an integer instead of a pointer to an integer (e.g.,
x instead of &x), you will probably get a core dump.
Note that although %s and %[ take pointers, the
& is usually not required as C passes arrays by reference.
Avoid by:
Use gcc -Wall: it will check that the arguments to
scanf, fscanf and sscanf match the format argument
(it will also check the format and arguments of printf,
fprintf and sprintf).
math.h
atof, sin, cos, etc,
appear not to return reasonable values.
float or double
is not declared as such, the compiler will assume it returns an int;
this usually leads to very strange values being returned.
gcc -Wall: it will complain about the use of routines that
have not been declared.
Unfortunately, the header files on some systems (notably, SunOS 4.x) are not
complete and gcc will complain about routines like printf, exit,
etc not being declared - one must learn to ignore such errors (or add the
declarations explicitly in your source).
for (i = 1; i <= n; i++) array[i] = ...;when they should write
for (i = 0; i < n; i++) array[i] = ...;Since C does no run time bounds checking on array references, references to
array[n] refer to the memory just past the
end of the array - there is a very good chance that some other variable
happens to reside at this location, and it will be read or written by accident.
malloc
or realloc is not initialized before being used.
gcc -Wall -O: it will complain about the use of
variables that have not been initialized; however it will not
complain about use of array elements (or structure fields) that have not
been initialized.
In the case of allocated memory, use calloc instead of
malloc - it will zero the newly allocated memory.
char *p;does not associate any memory with
p - if you use it
without giving it any memory (e.g., strcpy(p, "hi there")),
the memory references will be some random location (see section on
uninitialized variables).
gcc -Wall -O: it will complain about the use of
variables that have not been initialized; however it will not
complain about use of array elements (or structure fields) that have not
been initialized.
char *p = (char *) malloc(strlen(str)); strcpy(p, str);this does not allocate enough space for the string - it should be
char *p = (char *) malloc(strlen(str) + 1); strcpy(p, str);
strdup to copy strings (unfortunately, this routine
is not available on all machines, so this too can lead to problems).
free,
the memory may be
modified immediately by the code in free (it uses memory
to perform its bookkeeping functions) or the memory may be
re-used by the next call to malloc.
In either case, if you continue to use the memory after freeing it,
the memory can be modified unexpectedly, or your modification of it
may cause other parts of your program to act strangely.
gcc -Wall: it will complain about the use of routines that
have not been declared.foo()).
#include,
the problem may be in the header file.
char *
foo(char *str1, char *str2)
{
char buf[1024];
strcpy(buf, str1);
strcat(buf, str2);
return buf;
}
...
printf("Why isn't `%s' `hi there'?\n", foo("hi", " there"));
...
(the code is also bad as it assumes buf is big enough to hold str1
and str2)..
malloc() and friends,
or it should declared as static - in the above
example, declaring buf as static char buf[1024];
will make the code work (but it still assumes buf is big enough).
malloc()
calloc()
realloc() or
free().
scanf()
fscanf()
sscanf().
printf()
printf()
printf().
printf in
Missing & in call to scanf.
atof, sin, cos, etc,
appear not to return reasonable values.
if, while or for statement
is always (or is never) executed when it (apparently) shouldn't be.