Weird behaviour of prinft(\n)

Posts: 1
Joined: Tue Jan 30, 2018 9:32 pm

Weird behaviour of prinft(\n)

Postby wogoos » Tue Jan 30, 2018 11:51 pm

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

User avatar
Posts: 23
Joined: Tue Dec 05, 2017 2:53 pm
Location: Pune, India

Re: Weird behaviour of prinft(\n)

Postby » Thu Feb 01, 2018 4:04 pm

I don't recall seeing this.
Which SDK version is it?
- Pratik Panda
Nerd and Embedded Design Consultant

Posts: 1
Joined: Sun Feb 11, 2018 5:39 pm

Re: Weird behaviour of prinft(\n)

Postby Topaazy » Sun Feb 11, 2018 6:30 pm

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.

Who is online

Users browsing this forum: No registered users and 1 guest