This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

IAR for MSP430 function printf

Hi everyone,

Why

uint8_t c;
c = ~0x0A; printf("%u", (unsigned int)c);

outputs value greater than 8bit variable can hold? for example the output might be something like this: 65525.


Is there is an approach to print 8bit variable's values using printf function?

  • With %u, you are telling printf() that you have a value of type unsigned int. However, with a variadic function like this, the actual type used by the compiler is signed int (this is where the sign extension comes from).

    You must explicitly cast the value to unsigned:

    printf("%u", (unsigned int)c);

  • I just changed the code accordingly, unfortunately I get the same outcomes. In the original post you will be able to see the changes I have made.

  • When I run the code you have provided, the IAR tools print "245". Did you include the standard header file `stdio.h' in your application? If not, the printf function would look for the argument in the wrong location.

    -- Anders Lindgren, IAR Systems

**Attention** This is a public forum