[Courses] [C] Lesson 8 answer on word counter

Laurel Fan laurel at sdf.lonestar.org
Sat Nov 9 16:34:59 EST 2002

On Sun, Nov 10, 2002 at 03:46:15PM -0500, Morgon Kanter wrote:
> This thing segfaulted until I realized that i was uninitialized before use. 
> The C compiler doesn't catch that like the Java one does =)

If you are using gcc, you can make it warn you about uninitialized
variables, using the -O and -Wall options.  -O (capital o) means
optimize, and -Wall means all warnings.  For my version of gcc you do
need both; I'm not sure why.  For example, if I have this code:

#include <stdio.h>

int main()
  int i;
  printf("i is %d\n", i);
  return 0;

And I try to compile it with -O and -Wall, it will say:

> gcc -Wall -O test.c
test.c: In function `main':
test.c:5: warning: `i' might be used uninitialized in this function

I normally compile with -Wall, since it makes things easier by letting
the compiler warn me about potential silly mistakes like this one.

laurel at sdf.lonestar.org

More information about the Courses mailing list