From: Alicia Carla Longstreet Newsgroups: comp.lang.c,comp.os.msdos.djgpp Subject: Re: having trouble with long numbers Date: Wed, 13 Aug 1997 10:39:36 -0400 Organization: The Computer Solution Lines: 100 Message-ID: <33F1C728.2690@ici.net> References: <01bc9c51$0ceeec80$78ed1fcc AT darkstar> Reply-To: carla AT ici DOT net NNTP-Posting-Host: d-ma-fallriver-42.ici.net Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk Michèle C. Dupré wrote: > > (using DJGPP on a Win95 Box) > > Can't decide which variable to use with which scanf code. > > I want to be able to have more than 10 digits print out (if the age were > 10,000 years for example). > I am new to C (and programming in general) and teaching myself, so any > criticism will be appreciated. > > Also can someone direct me to the FAQ(so that I can RTFM)? > Thanx. > Below is my code. > > Cut------------------------------Cut > #include > > int main() > { > int daysper, daystotal, secondsperday, years; > long long totalseconds; Depending on the platform ints can be anywhere from 16 bits on up. The C Standard only requires that it be able to hold numbers from -32768 to 32767 (signed int) or 0 to 65535 (unsigned int). Your use of int to hold 86,400 is not portable and may not even work on your current platform, this would be true even if you used unsigned int. long long is also not standard (I only know of 1 platform/OS that supports this, which is why I suspect that your ints won't work on your platform). > daysper = 365; > secondsperday = 86400; /* If you system uses 16 bit signed ints for int, this will NOT be what you think it will be! */ > printf("My second handmade program!\n\nTo tell you the number of seconds > (estimate) you have lived.\n\n"); > printf("Enter your age in years: "); > scanf("%d", &years); I don't like scanf() or most of the related functions, you are better off using: char yr_str[4]; fgets(yr_str, 3, stdin); sscanf("%d", &years); You can even go so far as to actually validate the string in yr_str before converting it to an int. > daystotal=(daysper * years); > printf("Total days = %d\n\n", daystotal); There are actually approximatly 365.248 days in every year, this is why we need a leap year every 4 years and do NOT need a leap year every 400 years. By using 365 you will be off in the number of days by ((int)(years/4.0)) and in the number of seconds by (((int)(years/4.0))*(secondsperday)), for a 40 year old person this would be 864,000 seconds. You my consider this insignificant, I don't. > totalseconds=(daystotal * secondsperday); > printf("The total number of seconds is: %i\n",totalseconds); > > return 0; > } > End------------------------------End #include #include int main( int argc, char **argv ) { int years=0; unsigned long totalseconds; /* this works up to about 130 odd years */ char yr_str[4]; printf( "My second handmade program!\n\n" ); printf( "To tell you the number of seconds (estimate) you have lived.\n\n"); printf("Enter your age in years: "); fgets( yr_str, 4, stdin ); sscanf( yr_str, "%d", &years ); printf( "Total days = %f\n", ((float)years)*365.248 ); totalseconds = ( (double)years*(365.248*86400.0) ); printf( "The total number of seconds is: %lu\n",totalseconds ); return (EXIT_SUCCESS); } -- ********************************************************* * Alicia Carla Longstreet carla AT ici DOT net * * Supporter of the campaign against grumpiness on c.l.c * ********************************************************* * Children, * * We spend two years teaching them to talk... * * and 20 years trying to get them to shut up! * ********************************************************* The money spent on an abortion, as any woman may find out, can better be spent on other things, like taking the kids to an amusment park, or on vacation.