Keil Logo

Problems Using printf

The printf functions are implemented using a variable-length argument list. Arguments specified after the format string are passed using their inherent data type. This can cause problems when the format specification expects a data object of a different type than was passed. For example, the following code:

printf ("%c %d %u %bu", 'A', 1, 2, 3);

does not print the string "A 1 2 3". This is because the Cx51 Compiler passes the arguments 1, 2, and 3 all as 8-bit byte types. The format specifiers %d and %u both expect 16-bit int types.

To avoid this type of problem, you must explicitly define the data type to pass to the printf function. To do this, you must type cast the above values. For example:

printf ("%c %d %u %bu", 'A', (int) 1, (unsigned int) 2, (char) 3);

If you are uncertain of the size of the argument that is passed, you may cast the value to the desired size.

  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.