Re: any error in the code
- From: Harald van Dijk <truedfx@xxxxxxxxx>
- Date: Wed, 02 Apr 2008 07:23:59 +0200
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>.
- Follow-Ups:
- Re: any error in the code
- From: Richard Heathfield
- Re: any error in the code
- References:
- any error in the code
- From: parag_paul@xxxxxxxxxxx
- Re: any error in the code
- From: Jack . Thomson . v3
- Re: any error in the code
- From: Richard Heathfield
- Re: any error in the code
- From: Keith Thompson
- Re: any error in the code
- From: Richard Heathfield
- Re: any error in the code
- From: Keith Thompson
- Re: any error in the code
- From: Richard Heathfield
- Re: any error in the code
- From: Keith Thompson
- any error in the code
- Prev by Date: Re: warning: use of C99 long long integer constant
- Next by Date: Re: ftp connection through C.
- Previous by thread: Re: any error in the code
- Next by thread: Re: any error in the code
- Index(es):
Relevant Pages
|
Loading