I post here the output of a modern compiler with the '
all warnings activated' option (namely
gcc -Wall
) invoked on your code:
foo.c:3:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
main()
^
foo.c: In function ‘main’:
foo.c:8:9: warning: format ‘%s’ expects a matching ‘char *’ argument [-Wformat=]
printf("%s");
^
foo.c: In function ‘newme’:
foo.c:23:11: warning: implicit declaration of function ‘atof’ [-Wimplicit-function-declaration]
Data[i]=atof(token);
^
foo.c:26:10: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘int’ [-Wformat=]
printf("%f",Data[i]);
^
foo.c:30:9: warning: function returns address of local variable [-Wreturn-local-addr]
return input2;
^
foo.c:29:2: warning: ‘I’ is used uninitialized in this function [-Wuninitialized]
printf("\n data =%f %f %f %f %f %f %f %f %f",A,B,C,D,E,F,G,H,I);
^
foo.c:29:2: warning: ‘H’ is used uninitialized in this function [-Wuninitialized]
foo.c:29:2: warning: ‘G’ is used uninitialized in this function [-Wuninitialized]
foo.c:29:2: warning: ‘F’ is used uninitialized in this function [-Wuninitialized]
foo.c:29:2: warning: ‘E’ is used uninitialized in this function [-Wuninitialized]
foo.c:29:2: warning: ‘D’ is used uninitialized in this function [-Wuninitialized]
foo.c:29:2: warning: ‘C’ is used uninitialized in this function [-Wuninitialized]
Now let's examine the above mess...
foo.c:3:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
main()
^
You should always specify the
type of the return value of a
C
function (in this particular scenario you are lucky because the default,
int
, is correct).
foo.c: In function ‘main’:
foo.c:8:9: warning: format ‘%s’ expects a matching ‘char *’ argument [-Wformat=]
printf("%s");
^
You didn't provide the required argument (the string to print).
foo.c: In function ‘newme’:
foo.c:23:11: warning: implicit declaration of function ‘atof’ [-Wimplicit-function-declaration]
Data[i]=atof(token);
^
You should include
stdlib.h
in order to use
atof
.
foo.c:26:10: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘int’ [-Wformat=]
printf("%f",Data[i]);
^
There is a mismatch between the format specifier and the type of the variable to printout (you usually have to either change the format specifier of the variable type).
foo.c:30:9: warning: function returns address of local variable [-Wreturn-local-addr]
return input2;
^
This should make ring a bell very loud in your head, because it is usually the first sign of the incoming catastrophe: after function execution, a local variable does not exist any more.
foo.c:29:2: warning: ‘I’ is used uninitialized in this function [-Wuninitialized]
printf("\n data =%f %f %f %f %f %f %f %f %f",A,B,C,D,E,F,G,H,I);
^
foo.c:29:2: warning: ‘H’ is used uninitialized in this function [-Wuninitialized]
foo.c:29:2: warning: ‘G’ is used uninitialized in this function [-Wuninitialized]
foo.c:29:2: warning: ‘F’ is used uninitialized in this function [-Wuninitialized]
foo.c:29:2: warning: ‘E’ is used uninitialized in this function [-Wuninitialized]
foo.c:29:2: warning: ‘D’ is used uninitialized in this function [-Wuninitialized]
foo.c:29:2: warning: ‘C’ is used uninitialized in this function [-Wuninitialized]
Printing out uninitialized variables usually produces fancy results.
The bottom line is: the compiler is your friend, use it wisely.