Keil Logo


Information in this article applies to:

  • C51 Version 5 and Later


If the sizeof operator is located within a printf statement, a bogus number is printed. For example if x is declared to be an integer,

printf("%u", sizeof(x));

will print 512. However, the following will print correctly:

unsigned a = sizeof(x);
printf("%u", a);


Usually constants (the result of sizeof is a constant) are treated as an integer (the ANSI standard treats constants as integers). In an 8-bit CPU, performance suffers when every number is treated as a integer. Therefore, if a constant number can be represented in a single byte (as a char), the Keil compiler treats it that way. This is even true for the values return by sizeof.

If you pass a constant value to a normal function where the parameter type is known, the parameters are implicitly cast to the required type. Only when the parameter type is not known (var arg '...' parameter in printf) the C compiler assumes that it can pass the constant number as a character. Since printf expects an integer, the output results are incorrect.


The best way to work around this problem is to explicitly cast constant values being passed to printf. For example:

printf("%u", (unsigned int) sizeof(x));


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

Last Reviewed: Tuesday, June 8, 2004

Did this article provide the answer you needed?
Not Sure
  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.