Reputation: 1370
I have mainly two kinds of compile warning:
1. implicit declaration of function
in a.c
, it has char *foo(char *ptr1, char *ptr2)
, in b.c
, some functions use this foo
function without any declaration, and I found seems compiler will treat the function foo
return value as integer, and even I can pass some variables less or more than foo
function declaration
2. enumerated type mixed with another type
My target chip is ARM11, it seems that even I don't solve these two kinds of compile warning, my program can run without any issues, but I believe it must have some risk behind these. Can anyone give me some good example that these two kinds of compile warning can cause some unexpected issues?
Meanwhile, if these two warnings have potential risk, why c compiler allow these kinds warning happen but not set them to error directly? any story behind?
Upvotes: 0
Views: 134
Reputation: 6989
Simple example:
File: warn.c
#include <stdio.h>
double foo(double x)
{
return myid(x);
}
int
main (void)
{
double x = 1.0;
fprintf (stderr, "%lg == %lg\n", x, foo (x));
return 0;
}
File: foo.c
double
myid (double x)
{
return x;
}
Compile and run:
$ gcc warn.c foo.c -Wall
warn.c: In function ‘foo’:
warn.c:5: warning: implicit declaration of function ‘myfabs’
$ ./a.out
1 == 0
Old C standard (C90) had this strange "default int" rule and for compatibility it is supported even in latest compilers.
Upvotes: 0
Reputation: 18399
Implicit declaration. E.g. you have function: float foo(float a)
, which isn't declared when you call it. Implicit rules will create auto-declaration with following signature: int foo(double)
(if passed argument is float). So value you pass will be converted to double, but foo
expects float
. The same with return - calling code expects int
, but returned float
. Values would be a complete mess.
enum mixed with other type. Enumerated type have list of values it could take. If you trying to assign numeric value to it, there is a chance that it isn't one of listed values; if later your code expects only specified range and presumes nothing else could be there - it could misbehave.
Upvotes: 2