8 years, 10 months ago.

Compiler-Question: format modifier for uint8_t in printf/scanf?


I have a variable of type uint8_t and want to use it in sscanf, where I read a HEX-String and want to convert to uint8_t:

char buf[30]; 
uint8_t   len = 0;

When I compile I get a Warning at line 4:

Warning: Argument is incompatible with corresponding format string conversion in ...

(It works but I want to get rid of the warning)

http://stackoverflow.com/questions/6993132/format-specifiers-for-uint8-t-uint16-t says I have to include <inttypes.h> and use this one:

#include <inttypes.h>
sscanf(buf,"%2" SCNu8"" ,&len);

But this gives me a compile-error.

Error: Expected a ")" in ...

So what is the right way to read from a string a HEX-Value and assign it to uint8_t?


1 Answer

8 years, 10 months ago.

Hello Carl,

please read the following sscanf reference http://www.tutorialspoint.com/c_standard_library/c_function_sscanf.htm. The type is x ,so it should match the argument type : int *.

The answer for you question could be :

sscanf(buf,"%2hhx",&len); //hh for unsigned char


Accepted Answer

Hello Martin! Thank you for the hint! Yes this works and gives no compiler-warning!

posted by Karl Zweimüller 18 May 2014