Re: any error in the code



On Tue, 01 Apr 2008 21:54:08 -0700, Keith Thompson wrote:
Richard Heathfield <rjh@xxxxxxxxxxxxxxx> writes:
Keith Thompson said:
[...]
But you (that's a generic "you") shouldn't assume that the compiler
either will or will not warn you about incorrect calls. It's up to
the programmer to get this right in the first place.

Well, you're right - I'm forever forgetting to #include <string.h>, and
being bluntly reminded of the fact only when I compile the code on a
Win32 box, because my Linux-based compiler simply isn't interested, no
matter how many diagnostic switches I set, so I ought to know by now
that I can't rely on the compiler to diagnose me out of trouble - but
it's a non-trivial problem (in the general case); it's easy to forget
to add a header, and saying "Don't Do That" doesn't really help much.

<OT>

Perhaps it's time for an upgrade. Consider the following program, which
I presume is the kind of thing you're talking about (note the missing
"#include <string.h>"):

#include <stdio.h>
int main(void)
{
char *s = "hello";
size_t len = strlen(s);
printf("len = %d\n", (int)len);
return 0;
}

With gcc 3.4.4, "-Wall" triggers "warning: implicit declaration of
function `strlen'".

With gcc 4.1.3, even with no options I get "warning: incompatible
implicit declaration of built-in function 'strlen'".

GCC 2.x used to have a bug/feature where strlen is a predefined
identifier, and warnings for implicit declarations of predefined
identifiers were not emitted.

$ cat test.c
main() { return strlen(""); }
$ gcc-2.95.3 -c -Wall test.c
test.c:1: warning: return-type defaults to `int'

However, even then it's possible to disable this by adding -fno-builtin
to the compiler options.

$ gcc-2.95.3 -c -Wall test.c -fno-builtin
test.c:1: warning: return-type defaults to `int'
test.c: In function `main':
test.c:1: warning: implicit declaration of function `strlen'

Oddly enough, this _does_ generate a warning:

$ cat test.c
main() { return strlen(""); }
$ gcc-2.95.3 -c test.c
test.c: In function `main':
test.c:1: warning: built-in function `strlen' used without declaration

</OT>
.



Relevant Pages

  • Re: Windowed-sinc function
    ... >>> fc is the cutoff frequency expressed as a fraction ... When using C, always use compiler warnings. ... wsinc2.c:6: warning: implicit declaration of function `atan' ...
    (comp.dsp)
  • Re: Why does this work?
    ... The compiler is able to tell the difference between the two by the context in which the word "color" is used. ... The point is that the compiler reads the original code as attempting to access a shared member of an instance. ... That's why the compiler is warning the programmer that it's not going to do what was coded. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: input and output questions about file
    ... open/read/write if you want, its just the same. ... compiler tells me ... moron.c:5: warning: type defaults to `int' in declaration of `std' ... moron.c:25: warning: implicit declaration of function `getch' ...
    (comp.lang.c)
  • Re: Newbie question on time()
    ... Just using your compiler to determine the ... int intResult = 0; ... perhaps you should increase the warning level. ... implicit declaration of function 'srand' ...
    (comp.lang.c)
  • Re: va_arg and short
    ... Imagine that the compiler emits a warning ... Another argument for having a C programmer building C programs. ... the C programmer and totally insurmountable for the non-C programmer. ...
    (comp.lang.c)

Loading