Technical Support

C51: PROBLEMS USING PRINTF


Information in this article applies to:

  • C51 All Versions

QUESTION

I can get the printf command to work with integers but not long integers or 8-bit integers. Why?

ANSWER

You must use the optional characters B or b and L or l immediately preceding the type character to respectively specify char or long versions of the integer types d, i ,u, o, x and X.

The following printf correctly prints 1, 2, and 3 as integers:

printf ("%d %d %d", (int) 1, (int) 2, (int) 3);

The following printf correctly prints 1, 2, and 3 as long integers:

printf ("%Ld %Ld %Ld", 1L, 2L, 3L);

The following printf correctly prints 1, 2, and 3 as chars:

printf ("%Bd %Bd %Bd", (char) 1, (char) 2, (char) 3);

The type casts on the numeric constants are important because they specify the number of bytes required for each numeric constant.

FORUM THREADS

The following Discussion Forum threads may provide information related to this topic.

Last Reviewed: Wednesday, May 05, 2004


Did this article provide the answer you needed?
 
Yes
No
Not Sure