This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

printf outputs wrong result when do multiplication

volatile unsigned char a;

a = 3;
printf("%u\n", a * 100);

The output is 11264. Why?

Any reply would be greatly appreciated.

Parents
  • Hi Per Westermark,

    Thanks for you quick reply.

    I check the option Enable ANSI integer promotion rules and it is enabled. If I disable it, the output is the same. It seems that Keil complier does not promote varible a or constant 3 to an integer.

    If I change code to this:

    printf("%u\n", (unsigned int)a * 100);
    


    or

    printf("%u\n", a * (unsigned int)100);
    


    The result is correct.

Reply
  • Hi Per Westermark,

    Thanks for you quick reply.

    I check the option Enable ANSI integer promotion rules and it is enabled. If I disable it, the output is the same. It seems that Keil complier does not promote varible a or constant 3 to an integer.

    If I change code to this:

    printf("%u\n", (unsigned int)a * 100);
    


    or

    printf("%u\n", a * (unsigned int)100);
    


    The result is correct.

Children