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

Regarding usage of printf for serial communication

I am writing some serial data transfer apllication, i was seeing some examples given by keil. in that hello world example i have seen. i that they have used printf function which will print in serial window.... there if i am using printf("%x",variable say 'a');there it is appeding a zero at the end of variable. please clarify y it is happening so?

Parents
  • ANSI C specifies that values passed as parameters are promoted to "integers" if they are smaller. In Keil C, an int is 16 bits. Passing bytes as integers creates significant overhead on a 8-bit processor. So, by default, Keil does not pass byte-sized arguments as integers.

    There's an "enable ANSI integer promotion" checkbox somewhere if you want strict standards compliance at the expense of efficiency.

    Variable argument functions like printf() have a problem: how do they know the width of the passed arguments? For printf(), the format string provides the answer. Standard C has the %lx and %x formats. Note that you have to explicitly tell printf() that it's a "long integer" and not an "integer", because they might be different sizes. Keil extends this notion with a 'b' prefix for single-byte integers. You need to use "%bx", "%bu", etc., for the arguments you pass as a byte. Otherwise, printf can't find the correct arguments.

    If the format string and actual argument sizes do not match, printf will generally output garbage from the variable-argument buffer, which often enough will happen to be zero.

Reply
  • ANSI C specifies that values passed as parameters are promoted to "integers" if they are smaller. In Keil C, an int is 16 bits. Passing bytes as integers creates significant overhead on a 8-bit processor. So, by default, Keil does not pass byte-sized arguments as integers.

    There's an "enable ANSI integer promotion" checkbox somewhere if you want strict standards compliance at the expense of efficiency.

    Variable argument functions like printf() have a problem: how do they know the width of the passed arguments? For printf(), the format string provides the answer. Standard C has the %lx and %x formats. Note that you have to explicitly tell printf() that it's a "long integer" and not an "integer", because they might be different sizes. Keil extends this notion with a 'b' prefix for single-byte integers. You need to use "%bx", "%bu", etc., for the arguments you pass as a byte. Otherwise, printf can't find the correct arguments.

    If the format string and actual argument sizes do not match, printf will generally output garbage from the variable-argument buffer, which often enough will happen to be zero.

Children
No data