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 > Re: Integer mul...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 6 of 9 Topic 24485 of 28006
Post > Topic >>

Re: Integer multiplication truncation

by Keith Thompson <kst-u@[EMAIL PROTECTED] > Feb 2, 2008 at 12:58 PM

Mamluk Caliph <mamluk.caliph@[EMAIL PROTECTED]
> writes:
> 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:
[snip]

The types uint16_t and uint32_t are declared in <stdint.h>, not in
<inttypes.h>.  It happens that <inttypes.h> includes <stdint.h>, which
is why your program compiles, but the purpose of <inttypes.h> is to
define format conversions for use with the *printf and *scanf
functions.

If you just want the types, you should include <stdint.h> rather than
<inttypes.h>.

But the macros in <inttypes.h> are one possible solution to your
problem.  The trouble, IMHO, is that they're ugly.  For example, your
printf call would be:

    printf("%" PRIu32 "\n", mazarin);

Another solution is to convert the value to a type for which you know
the format.  Since unsigned long is guaranteed to be a least 32 bits,
you can safely use "%lu" *if* you explicitly convert the value:

    printf("%lu\n", (unsigned long)mazarin);

For wider types you might need to use "%zd" or "%zu" with intmax_t or
uintmax_t, defined in <stdint.h> -- but only if your printf
implementation sup****ts them (not all do).

-- 
Keith Thompson (The_Other_Keith) <kst-u@[EMAIL PROTECTED]
>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
 




 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 Fri Nov 21 12:49:46 CST 2008.