0

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?

How Chen
  • 1,340
  • 2
  • 17
  • 37
  • Don't perceive warnings as risks, but as *useful help*. Always enable all of them (`gcc -Wall -Wextra`), and always improve your code to avoid them. Sometimes you'll got false positives. And most of them are not errors because the C99 standard demands that. Beware of [undefined behavior](http://en.wikipedia.org/wiki/Undefined_behavior) – Basile Starynkevitch Oct 29 '14 at 08:12
  • 1
    It may not be problem now, but in the future. After few weeks, you would like to change something in your code. If you have all warnings solved, compiler can tell you that your new modifications can be somehow broken or wrong. Imagine, that you e.g. add parameter to the foo function in new version. With function declared in header file, compiler will tell you, that you must correct it in all places, but without it, it will compile but don't work properly. – j123b567 Oct 29 '14 at 08:23

2 Answers2

2

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.

keltar
  • 17,711
  • 2
  • 37
  • 42
  • ok, so may I know why c compiler allow `Implicit declaration` in default? any story behind this? – How Chen Oct 29 '14 at 08:02
  • 1
    Because of historical reason. There is an answer http://stackoverflow.com/questions/11835001/why-does-did-c-allow-implicit-function-and-typeless-variable-declarations – j123b567 Oct 29 '14 at 08:06
  • Historic reasons. C99+ forbids implicit declarations. C++ does too. – keltar Oct 29 '14 at 09:03
0

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.

Konstantin Vladimirov
  • 6,791
  • 1
  • 27
  • 36