I am trying to compile a c++ program for esp8266 on the pio ide in vscode. I need to use printf with uint32_t, aka a long unsigned int . I have googled extensively and have tried many specifiers and I can’t find any that work. It compiles ok but I get a warning like this …
warning: format '%lu' expects argument of type 'long unsigned int',
but argument 4 has type 'uint32_t' {aka 'unsigned int'} [-Wformat=]
I’ve tried %u, %lu, %d, %ld, and none work. What specifier should I use?
I’ve been doing that by casting to (unsigned long int) which was a real pita with 6 or 7 variables. (unsigned) is less painful. I guess I’ll use that. But there must be a proper specifier for each standard type. If not it’s a bug as far as I’m concerned.
Generally, it is convenient and efficient to use the default int whenever possible. I tested with NodeMcuv2 which is 8266EX. “Long unsigned int” and “unsigned” are both 4 bytes.
struct {
unsigned v1; // default 4 bytes
long unsigned int v2; // also 4 bytes
}my{1234,4321};
Serial.printf("size int %u, size long %u\r\n",
sizeof(my.v1),sizeof(my.v2));
Serial.printf("%u, %lu\r\n",my.v1, my.v2);
Serial.println(String(my.v1) + "\t" + String(my.v2));