RMANGALD%CLARKU.BITNET@wiscvm.wisc.EDU (09/10/87)
Listmember Paul Winalski (winalski%psw.dec@decwrl.dec.com) writes: >[offers solution to someone's problem passing arguments to a UIS >routine] ...VAX C floating point constants are of type double, not >type float (see "Guide to VAX C", Section 6.4.1, Floating-Point >Constants).... The VAX C manual offers a rather strange (to me) solution to the problem of preventing the automatic conversion of <float> to <double>. It suggests using a structure, as in: void main() { struct foo { float n; } bar; void f(); bar.n = 3.14; /* or whatever */ f(bar); } Here, <f()> is a function that expects a single <float> argument passed by value, like some of the library functions that expect one or more F-floating arguments. What appears strange to me is: Why go to all this trouble when you can use a cast, as in: void main() { float n; void f(); f((float) n); } Is there something wrong with my approach? The "struct" approach looks awfully inelegant to me. >...I don't think function prototypes were implemented in VAX C at the >time that UISENTRY.H was written. Could someone explain what "function prototypes" are? I've heard a lot about these recently. Thanks. Rahul. -------------------------------- Rahul Mangaldas (rmangaldas@clarku.bitnet) Box 1311, Clark University 950 Main Street Worcester, MA 01610-1477
gwyn@brl-smoke.UUCP (09/12/87)
In article <9226@brl-adm.ARPA> RMANGALD%CLARKU.BITNET@wiscvm.wisc.EDU writes: >Why go to all this trouble when you can use a cast, as in: > f((float) n); This doesn't work -- the float-valued expression is widened to double, so the cast has no practical effect. >Could someone explain what "function prototypes" are? The proposed ANSI C standard adopted Bjarne Stroustrup's method for declaring functions. The main difference from "old C" is that a function declaration may (and normally would) specify parameter types as well as return value type. For example: #if __STDC__ /* ANSI C */ extern int atoi( const char *s ); #else /* old C */ extern int atoi(); #endif (The identifier on the parameter can be omitted.) There are rules for what happens when old-style and prototype-style declarations collide, but it is intended that new code always use prototypes. There are several advantages to function prototypes: (a) provides an opportunity to eliminate parameter widening (b) provides the necessary hook for more general varargs implementation (e.g. when the first N parameters would normally be passed in registers) (c) allows automatic coercion of parameter types (I don't like this one, but many people do) (d) permits/requires better compile-time type checking (e) code is more self-documenting The main disadvantage is that it's different from what we already were using. However, the old style is being "grandfathered in" in such a way that this should rarely be a problem. Notice that the example above takes advantage of ANSI C if it's available but works with old C too; you don't normally need to change old code to do this, but it is advisable to use this method for new code.