Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!cmcl2!brl-adm!umd5!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: Question on ANSI C compatibility Message-ID: <9272@mimsy.UUCP> Date: Sun, 8-Nov-87 20:03:14 EST Article-I.D.: mimsy.9272 Posted: Sun Nov 8 20:03:14 1987 Date-Received: Wed, 11-Nov-87 03:08:19 EST References: <10224@brl-adm.ARPA> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 63 In article <10224@brl-adm.ARPA> RMANGALD%CLARKU.BITNET@wiscvm.wisc.EDU writes: >... I'm not sure whether the ANSI standard requires what I don't >want to do -- put a "(void)" in front of every function call where I'm >throwing away the return value. It does not *require* it, but it does permit a compiler to complain about every such occurrence. >It's a real pain to prefix all the "printf()"'s, "gets()"'s, etc. with >"(void)", so could someone tell me ... whether it is good programming style? For some functions, I would say yes; for others, I would say no. For instance, the value from printf et alii indicates the number of characters printed, or EOF (or < 0, not sure which) for error. Hence one should test printf return values for errors or explicitly state (via `(void)') that one is not concerned with any such error. On the other hand, it is possible to test for error after calling printf: fprintf(foil, fmt, arg); if (ferror(foil)) { /* foiled again */ ... In this case the return value is indeed dull. Some functions never return errors: strcpy, for instance, copies its second argument on top of its first, then returns its first. `(void) strcpy(to, from)' is only interesting if you have not saved `to' anywhere else: char * stralloc(int len) { char *p = malloc(len + 1); if (p == NULL) stop("out of memory (stralloc)"); return (p); } char * strsave(char *s) { return (strcpy(stralloc(strlen(s)), s)); } Unfortunately, there is no (standard) way to distinguish between functions with `interesting' values and those with `dull' values. Indeed, in 4BSD there is no way at all; this is something I intend to change. It looks as though it will be fairly easy to add to lint a new pragma: ... double atof(s) char *s; { return (0.0); } int strlen(s) char *s; { return (1); } /* DULLVALUE */ char *strcpy(to, from) char *to, *from; { return (to); } ... -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690) Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris