Path: utzoo!mnetor!uunet!husc6!uwvax!umn-d-ub!umn-cs!meccts!viper!john From: john@viper.Lynx.MN.Org (John Stanley) Newsgroups: comp.lang.c Subject: Re: Whats wrong with this code?? (MS-DOS/TC 1.0) Message-ID: <743@viper.Lynx.MN.Org> Date: 23 Mar 88 16:25:32 GMT References: <4935@sigi.Colorado.EDU> Reply-To: john@viper.Lynx.MN.Org (John Stanley) Distribution: na Organization: DynaSoft Systems Lines: 48 In article <4935@sigi.Colorado.EDU> murillo@boulder.Colorado.EDU (Rodrigo Murillo) writes: >OK, I give up. Can some one tell me why the expresion below (see comments) >yields a result that is different than what the initial results would suggest? (part of question deleted) >/* relevant structure is: > >struct dfree { > unsigned df_avail; /* available clusters */ > unsigned df_total /* total cluster */ > unsigned df_bsec; /* bytes per sector */ > unsigned df_sclus; /* sectors per cluster */ You forgot the closing brace here, but I assume that was just an oversight in copying the example... > /* THIS IS THE OFFENDING EXPRESION */ > /* why is this number different that the numbers from above would > produce? My calculator produces the right value, but the expression > below does not! */ > > bytesf = dfreep.df_avail * dfreep.df_sclus * dfreep.df_bsec; > > printf("Disk free: %u\n", bytesf); The problem is a fairly common one when multiplying large integers. If you take the numbers displayed by your sample program and multiply them out by hand (calculator is ok too.. :) I suspect you'll discover the number you created was greater than 65535. When this happens in the example you gave, the number gets truncated. It doesn't matter that you told the compiler you're saving the number into a long, what matters is that the resultant type from multiplying two integers together is another integer. You program (doing just what you told it to do) multiplys out the three integers, truncating when the results exceeds 65535, converts the integer to a long and stores it into the long. The safe way to fix this is: bytesf = (long)dfreep.df_avail * (long)dfreep.df_sclus * (long)dfreep.df_bsec; --- John Stanley (john@viper.UUCP) Software Consultant - DynaSoft Systems UUCP: ...{amdahl,ihnp4,rutgers}!meccts!viper!john