Date: Fri, 30 Oct 1998 08:03:14 +1100 From: Peter Jeremy <peter.jeremy@auss2.alcatel.com.au> To: hackers@FreeBSD.ORG Cc: luigi@labinfo.iet.unipi.it Subject: Re: compiler prototype question... Message-ID: <98Oct30.080247est.40341@border.alcanet.com.au>
next in thread | raw e-mail | index | archive | help
On Thu, 29 Oct 1998 05:53:44 +0100 (MET), Luigi Rizzo <luigi@labinfo.iet.unipi.it> wrote: >can someone tell me why in the following C program: > > int c1 (int c); > int c1 (char c) { return 1 ; } > proto.c:8: conflicting types for `c1' > proto.c:7: previous declaration of `c1' Here, you've specified conflicting prototypes. The declaration states that c1 takes a single `int' argument, whilst the definition states that it takes a single `char' argument. > int c2 (int c); > int c2 (c) char c; { return 1 ; } This is correct. A K&R definition means that the argument will be promoted: {char,short}->int, float->double. The prototype therefore matches the definition. Note that although the argument is passed as an int, it is referenced as a char. The remaining bytes are ignored. > int c3 (char c); > int c3 (c) int c; { return 1 ;} > proto.c: In function `c3': > proto.c:14: argument `c' doesn't match prototype > proto.c:13: prototype declaration Here, you've specified conflicting prototypes. The declaration states that c1 takes a single `char' argument, whilst the definition states that it takes a single `int' argument. > int c4 (char c); > int c4 (int c) { return 1 ;} > proto.c:17: conflicting types for `c4' > proto.c:16: previous declaration of `c4' This is identical to c3. >To me, c2() seems as bad as the others, and i don't understand why >the compiler does not complain. This is according to the ANSI standard rules. > It seems that when the prototype >declares an "int" argument, then any signed/unsigned type is accepted >for that argument in the non-ansi function body. Any char, short or int argument is acceptable. long isn't and I'd need to check on unsigned. > As you can imagine >this defeats the usefulness of prototypes and obfuscates code. Not true. It complies with the K&R C calling conventions. Peter -- Peter Jeremy (VK2PJ) peter.jeremy@alcatel.com.au Alcatel Australia Limited 41 Mandible St Phone: +61 2 9690 5019 ALEXANDRIA NSW 2015 Fax: +61 2 9690 5247 To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?98Oct30.080247est.40341>