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"


|