Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!cmcl2!brl-adm!brl-smoke!gwyn From: gwyn@brl-smoke.ARPA (Doug Gwyn ) Newsgroups: comp.lang.c Subject: Re: Question on ANSI C compatibility Message-ID: <6660@brl-smoke.ARPA> Date: Mon, 9-Nov-87 12:20:29 EST Article-I.D.: brl-smok.6660 Posted: Mon Nov 9 12:20:29 1987 Date-Received: Wed, 11-Nov-87 06:09:38 EST References: <10224@brl-adm.ARPA> Reply-To: gwyn@brl.arpa (Doug Gwyn (VLD/VMB) ) Organization: Ballistic Research Lab (BRL), APG, MD. Lines: 30 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's a real pain to prefix all the >"printf()"'s, "gets()"'s, etc. with "(void)", so could someone tell me >whether this is necessary for ANSI compatibility, and whether it is good >programming style? 1. ANSI C does NOT require that you explicitly discard unused function return values by casting to (void). It does permit you to do so. Some code checkers (e.g. "lint") may warn you if you do not do so. 2. There are only a small number of functions for which ignoring the return value is a safe thing to do. Most notable are ones such as strcpy() that simply return information you already had, so that there is no pressing reason to look at the return value. 3. Other functions such as printf() and gets() do return useful information as their return value. You should think hard before deciding to ignore this information (which generally indicates whether or not the attempted operation succeeded). It is almost never wise to ignore the value returned by gets(), for example. 4. It is not good coding style to simply pour (void) casts into your code without thinking about them. It IS good style to always consider whether or not you need the return value and explicitly discard it by a (void) cast when you deliberately decide that the value does not need to be examined. It is not particularly bad style to not use such casts, but then someone looking at your code in the future will have to puzzle out whether or not you simply forgot to test a return value.