omahony@csvax1.cs.tcd.ie (Donal O'Mahony - OMAHONY@cs.tcd.ie) (03/23/89)
Can anone explain the problem with the following program (made up of 2 files). ::main.c #include <stdio.h> main() {double a,b; a = 1.234; b = store_length(a); printf("b=%f\n",b); } ::rt1.c double store_length( double measurements) { measurements= 2; printf("Returning measurements=%f\n",measurements); return(measurements); } When these two programs are linked and run, they do not print 'b=2,' as I would expect. Why? -- _______________________________________________________________________ Donal O'Mahony omahony@cs.tcd.ie Computer Science Dept., Trinity College, Dublin 2, Ireland _______________________________________________________________________
jinli@gpu.utcs.toronto.edu (Jin Li) (03/24/89)
In article <39722@csvax1.cs.tcd.ie> omahony@csvax1.cs.tcd.ie (Donal O'Mahony - OMAHONY@cs.tcd.ie) writes: >Can anone explain the problem with the following program (made up of 2 files). > >::main.c >#include <stdio.h> >main() >{double a,b; > > a = 1.234; > b = store_length(a); You should explicitly declare the type for 'store_length'. ie. you do 'extern double store_length();' at the beginning. The reason is that C assumes undeclared functions having 'int' as their type. Thus, value returned from 'store_length' will be implicitly casted to 'int' before assigned to var 'b'. >[deleted]... > >::rt1.c >double store_length( double measurements) >{ measurements= 2; It is always a good idea to initialize 'double' var using floating point representation. ie. 2.0 instead of 2 -- Jin Li >> Gin & Tonic mix well University of Toronto Computing Services << No bugs, no bucks jinli@gpu.utcs.utoronto.ca uunet!utgpu!jinli>> ls is a typo
ark@alice.UUCP (Andrew Koenig) (03/25/89)
In article <39722@csvax1.cs.tcd.ie>, omahony@csvax1.cs.tcd.ie (Donal O'Mahony - OMAHONY@cs.tcd.ie) writes: > #include <stdio.h> > main() > {double a,b; > > a = 1.234; > b = store_length(a); > printf("b=%f\n",b); > } If store_length returns a double, you must declare it as such: #include <stdio.h> double store_length(); main() { double a,b; a = 1.234; b = store_length(a); printf("b=%f\n",b); } In ANSI C, it looks like this: #include <stdio.h> double store_length(double); main() { double a,b; a = 1.234; b = store_length(a); printf("b=%f\n",b); } Failure to declare such functions is a common C pitfall. -- --Andrew Koenig ark@europa.att.com
Tim_CDC_Roberts@cup.portal.com (03/25/89)
In <39722@csvax1.cs.tcd.ie>, omahony@csvax1.cs.tcd.ie (Donal O'Mahony) writes: > Can anone explain the problem with the following program (made up of > 2 files). > > ::main.c > #include <stdio.h> > main() > {double a,b; > > a = 1.234; > b = store_length(a); > printf("b=%f\n",b); > } > > ::rt1.c > double store_length( double measurements) > { measurements= 2; > printf("Returning measurements=%f\n",measurements); > return(measurements); > } > > When these two programs are linked and run, they do not print 'b=2,' > as I would expect. Why? Once again, I am sure there will be 24 other replies to this, all of which will cross in the net. In main.c, since you did not declare that store_length returns a double, the compiler will assume it returns an int. It will then try to cast that to a double, thereby rendering it undecipherable. Modify main.c thusly: #include <stdio.h> double store_length (double); main() {double a,b; a = 1.234; b = store_length(a); printf("b=%f\n",b); } (Note to those who will warn me about the ANSI header: it can be deduced from his file rt1.c that he is using a compiler which supports the prototypes.)
ftw@masscomp.UUCP (Farrell Woods) (03/27/89)
In article <39722@csvax1.cs.tcd.ie> omahony@csvax1.cs.tcd.ie (Donal O'Mahony - OMAHONY@cs.tcd.ie) writes: >Can anone explain the problem with the following program (made up of >2 files). [example deleted] You get garbage for output, right? In main.c, you have no prototype in scope for the function ``store_length'' in rt1.c. The compiler defaults to casting the argument to store_length to an int, where you would have preferred it to pass a double. The solutions are: explicitly cast the argument to double, or (better still) create a prototype in main.c for the function store_length. -- Farrell T. Woods Voice: (508) 392-2471 Concurrent Computer Corporation Domain: ftw@masscomp.com 1 Technology Way uucp: {backbones}!masscomp!ftw Westford, MA 01886 OS/2: Half an operating system
ftw@masscomp.UUCP (Farrell Woods) (03/27/89)
In article <39722@csvax1.cs.tcd.ie> omahony@csvax1.cs.tcd.ie (Donal O'Mahony - OMAHONY@cs.tcd.ie) writes: >Can anone explain the problem with the following program (made up of >2 files). I forgot to mention: the returned type of ``store_length'' is supposed to be of type double, but since that fact is not mentioned in main.c, the compiler assumes it's return type to be int. Sticking a prototype in main.c before you use store_length would be the best solution. The argument passing in your example is still wrong, but it wasn't the reason you got answers which surprised you. -- Farrell T. Woods Voice: (508) 392-2471 Concurrent Computer Corporation Domain: ftw@masscomp.com 1 Technology Way uucp: {backbones}!masscomp!ftw Westford, MA 01886 OS/2: Half an operating system
evil@arcturus.UUCP (Wade Guthrie) (03/28/89)
In article <9099@alice.UUCP>, ark@alice.UUCP (Andrew Koenig) writes: > Failure to declare such functions is a common C pitfall. ^^^^^^^^^ or Trap? Wade Guthrie evil@arcturus.UUCP Rockwell International Anaheim, CA (Rockwell doesn't necessarily believe / stand by what I'm saying; how could they when *I* don't even know what I'm talking about???)