Page 1 of 1

Weird behaviour of prinft(\n)

Posted: Tue Jan 30, 2018 11:51 pm
by wogoos
Dear readers
I'm using printf() in my code for ESP8266 and found a weird phenomenon. In the setup printf(\n) generates me a CRLF but ones entered in the Loop() in one of the function calls it doesn't work any more. In this function call I have a
printf("Connected to: SSID: \t %-20s\n",WiFi.SSID(wifi.netindex).c_str() ); which works fine but ones i add a \n to the beginning and change it to printf("\nConnected to: SSID: \t %-20s\n",WiFi.SSID(wifi.netindex).c_str() ); it doesn't display anything at all :o
Does any one can give me and idea what going on.

An other strange behavior I saw with printf() in combination with Serial.print(). if you mix them than the screen output is not generated in the sequence the functions are used in the code. Print lines later in the code are earlier displayed. Are the printf() streams and Serial.print() streams independent processed by the RTOS of the ESP? and is this the cause of the problem?
I hope one of you can help me.
Thanks Oscar Goos

Re: Weird behaviour of prinft(\n)

Posted: Thu Feb 01, 2018 4:04 pm
I don't recall seeing this.
Which SDK version is it?

Re: Weird behaviour of prinft(\n)

Posted: Sun Feb 11, 2018 6:30 pm
by Topaazy
Your problem is undefined behaviour, and that's why you're getting the warnings. Warnings are indications that something is actually wrong.
printf is a variadic function, so it needs some information about the types of the arguments. That's the job of the format specifiers (eg. %d).
%d tells printf to expect an int typed argument, which your warning messages are telling you: warning: format %d expects argument of type int
However, the types you're giving it aren't ints. They're uint32_t and float, which your warning messages are also telling you: but argument 2 has type uint32_t [-Wformat] and but argument 3 has type float [-Wformat].
There's a number of solutions. The best would be to use the correct format specifier! (duh). That's "%d" for int, "%"PRIu32 for uint32_t and "%f" for double (which floats get promoted to). In the case of uint32_t, you could cast to an unsigned long and print using "%lu", instead.

Re: Weird behaviour of prinft(\n)

Posted: Wed Jul 10, 2019 4:12 pm
by Jared Thomas
Experiencing the same problem, nothing displayed.. Strange phenomenon I must say.