home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / c / 16126 < prev    next >
Encoding:
Text File  |  1992-11-08  |  1.8 KB  |  77 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!netnews!bandy
  3. From: bandy@netnews.jhuapl.edu (Mike Bandy)
  4. Subject: Re: Novice wants to pass type double to function
  5. Message-ID: <BxBzqJ.Gn9@netnews.jhuapl.edu>
  6. Organization: JHU/Applied Physics Laboratory
  7. References: <Oct30.161317.5071@yuma.ACNS.ColoState.EDU>
  8. Distribution: usa
  9. Date: Sat, 7 Nov 1992 05:39:54 GMT
  10. Lines: 65
  11.  
  12. sutton@lamar.ColoState.EDU (Rich Sutton) writes:
  13.  
  14. >As a first approximation I tried to recreate the power.c program in
  15. >Kernighan & Ritchie's 2nd edition. (This is on p24 if you have it handy.)
  16. >This program passes 2 variables of type int and returns type int. As
  17. >written in K&R SunOS cc won't compile it!? I thought ANSI C was supposed
  18. >to be portable? What am I doing wrong? Here is the file after doing
  19. >"cc power.c |& error" :
  20.  
  21.  
  22. >#include <stdio.h>
  23.  
  24. >/*###3 [cc] syntax error at or near type word "int"%%%*/
  25. >int power(int m, int n);
  26.  
  27. >main()
  28. >{
  29. >    int i;
  30.  
  31. >    for (i = 0; i < 10; ++i)
  32. >        printf("%d %d %d\n", i, power(2,i), power(-3,i));
  33. >    return 0;
  34. >}
  35.  
  36. >/*###14 [cc] syntax error at or near type word "int"%%%*/
  37. >int power(int base, int n)
  38. >{
  39. >    int i, p;
  40.  
  41. >    p=1;
  42. >/*###19 [cc] n undefined%%%*/
  43. >    for (i = 1; i < n; ++i)
  44. >/*###20 [cc] base undefined%%%*/
  45. >        p = p*base;
  46. >    return p;
  47. >}
  48.  
  49. The problem is that the C compiler is not ANSI!  Defining the parameter type
  50. like you do is an extension added when the language was made ANSI and was not
  51. part of the original C.  See the 1st edition of the K+R book.
  52.  
  53. Convert the routine declaration of
  54.  
  55.     int power(int base, int n)
  56.     {
  57.  
  58. to
  59.  
  60.     int power(base, n)
  61.     int base, n;
  62.     {
  63.  
  64. Likewise in your floating point routines to pass double, define your routines
  65. as:
  66.  
  67.     double myroutine(a, b)
  68.     double a, b;
  69.     {
  70.         ...
  71.  
  72. -- 
  73.  
  74.     Mike Bandy
  75.     bandy@aplcomm.jhuapl.edu
  76.     Johns Hopkins University / Applied Physics Lab
  77.