home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!netnews!bandy
- From: bandy@netnews.jhuapl.edu (Mike Bandy)
- Subject: Re: Novice wants to pass type double to function
- Message-ID: <BxBzqJ.Gn9@netnews.jhuapl.edu>
- Organization: JHU/Applied Physics Laboratory
- References: <Oct30.161317.5071@yuma.ACNS.ColoState.EDU>
- Distribution: usa
- Date: Sat, 7 Nov 1992 05:39:54 GMT
- Lines: 65
-
- sutton@lamar.ColoState.EDU (Rich Sutton) writes:
-
- >As a first approximation I tried to recreate the power.c program in
- >Kernighan & Ritchie's 2nd edition. (This is on p24 if you have it handy.)
- >This program passes 2 variables of type int and returns type int. As
- >written in K&R SunOS cc won't compile it!? I thought ANSI C was supposed
- >to be portable? What am I doing wrong? Here is the file after doing
- >"cc power.c |& error" :
-
-
- >#include <stdio.h>
-
- >/*###3 [cc] syntax error at or near type word "int"%%%*/
- >int power(int m, int n);
-
- >main()
- >{
- > int i;
-
- > for (i = 0; i < 10; ++i)
- > printf("%d %d %d\n", i, power(2,i), power(-3,i));
- > return 0;
- >}
-
- >/*###14 [cc] syntax error at or near type word "int"%%%*/
- >int power(int base, int n)
- >{
- > int i, p;
-
- > p=1;
- >/*###19 [cc] n undefined%%%*/
- > for (i = 1; i < n; ++i)
- >/*###20 [cc] base undefined%%%*/
- > p = p*base;
- > return p;
- >}
-
- The problem is that the C compiler is not ANSI! Defining the parameter type
- like you do is an extension added when the language was made ANSI and was not
- part of the original C. See the 1st edition of the K+R book.
-
- Convert the routine declaration of
-
- int power(int base, int n)
- {
-
- to
-
- int power(base, n)
- int base, n;
- {
-
- Likewise in your floating point routines to pass double, define your routines
- as:
-
- double myroutine(a, b)
- double a, b;
- {
- ...
-
- --
-
- Mike Bandy
- bandy@aplcomm.jhuapl.edu
- Johns Hopkins University / Applied Physics Lab
-