Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > Integer multipl...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 9 Topic 24485 of 27695
Post > Topic >>

Integer multiplication truncation

by Mamluk Caliph <mamluk.caliph@[EMAIL PROTECTED] > Feb 2, 2008 at 04:46 AM

The following code executes as I would expect on gcc:

#include <inttypes.h>
#include <stdio.h>

int main(int argc, char **argv){
   uint16_t apa         = 10000;
   uint16_t kaka        = 7000;
   uint32_t mazarin;

   mazarin = apa*kaka;
   printf("%lu \n",mazarin);
   return 0;
}

I.e. it will print out the value 70000000. However, when I compile
this another compiler the result is truncated and I have to modify the
code to the following to make it run:

#include <inttypes.h>
#include <stdio.h>

int main(int argc, char **argv){
   uint16_t apa         = 10000;
   uint16_t kaka        = 7000;
   uint32_t mazarin;

   mazarin = (uint32_t)apa*kaka;
   printf("%lu \n",mazarin);
   return 0;
}


Could anybody explain this? Is this due to C99 improvements regarding
integer overflow? Is there any predefined macro one could use to
determine this special case?

(Note: The extended types in inttypes.h were added for the second
compiler.)


Regards
/Michael
 




 9 Posts in Topic:
Integer multiplication truncation
Mamluk Caliph <mamluk.  2008-02-02 04:46:32 
Re: Integer multiplication truncation
=?UTF-8?q?Harald_van_D=C4  2008-02-02 14:02:11 
Re: Integer multiplication truncation
Mamluk Caliph <mamluk.  2008-02-02 06:57:09 
Re: Integer multiplication truncation
Thad Smith <ThadSmith@  2008-02-02 10:26:32 
Re: Integer multiplication truncation
Keith Thompson <kst-u@  2008-02-02 13:17:29 
Re: Integer multiplication truncation
Keith Thompson <kst-u@  2008-02-02 12:58:31 
Re: Integer multiplication truncation
=?UTF-8?q?Harald_van_D=C4  2008-02-02 22:35:17 
Re: Integer multiplication truncation
Keith Thompson <kst-u@  2008-02-02 13:55:22 
Re: Integer multiplication truncation
Mamluk Caliph <mamluk.  2008-02-03 04:02:46 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Oct 14 11:41:17 CDT 2008.