As a C newbie, your #1 error will be forgetting to initialize
variables.
#include <stdio.h> int squaresum(void) { int sum; /* Never set to 0 */ int i; for ( i = 1; i <= 10; ++i ) { sum += i*i; } return sum; } int main( void ) { printf( "1st time, %d\n", squaresum() ); printf( "2nd time, %d\n", squaresum() ); return 0; }
1st time, -1073744027 2nd time, 385
Your results will vary, because things are differently random on your machine.
Jack up your warnings. Always compile with -Wall
. Some warnings
may only get returned with the -O
flag on.