Path: utzoo!attcan!uunet!mcvax!hp4nl!philmds!leo From: leo@philmds.UUCP (Leo de Wit) Newsgroups: comp.sys.atari.st Subject: Re: problem with mwc atoi(), version 2.0.1 Message-ID: <808@philmds.UUCP> Date: 16 Sep 88 11:36:44 GMT References: <12562@ncoast.UUCP> Reply-To: leo@philmds.UUCP (Leo de Wit) Distribution: comp Organization: Philips I&E DTS Eindhoven Lines: 37 In article <12562@ncoast.UUCP> btb@ncoast.UUCP (Brad Banko) writes: | |What is wrong with the following code? [some lines deleted]... |#include |/* #include */ | |#define MAXVAL 32767 | |main(argc,argv) | int argc; char *argv[]; |{ | int i, x; | extern int atoi(); | | for (i=1; i+1<=argc; ++i) { | x = rand(); | printf("%d %d %s %d %d %d\n", | x, i, argv[i], (double) x / MAXVAL, atoi(argv[i]), | (double) x / MAXVAL * atoi(argv[i]) + 1); | } | | putchar('\n'); |} | |The code compiles, but produces bad (atoi()?) results... why? Why are |the atoi(argv[i]) values bad (negative)? The arguments supplied to printf() are not conforming to the format: (double) x / MAXVAL is of type double (probably 8 bytes on the stack), while you try to print it as an integer (%d) (2 bytes in mwc ?). So the first three arguments print correctly, but the last three are taken wrongly from the stack, and besides are interpreted wrong (as integer, but they are double,int, double. Using a cast (int)(double expr.) for the double expressions should solve your problem. Leo.