printf() requires a \n for output?

aekalman
Posts: 7
Joined: Fri Oct 27, 2017 10:34 pm

printf() requires a \n for output?

Postby aekalman » Fri Oct 27, 2017 10:41 pm

Hello. This is my first post.

I am able to successfully build and run the hello_world application on a SparkFun ESP32 Thing module (26MHz main xtal).

However, I observer that whereas these lines in the example:

Code: Select all

    for (int i = 10; i >= 0; i--) {
        printf("Restarting in %d seconds...\n", i);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
        }
work correctly, these altered lines do not output the "." character:

Code: Select all

    int i=10;
    printf("Restarting in %d seconds \n", i);
    for (; i >= 0; i--) {
        printf(".");
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
but these do (but are not in the format that I want):

Code: Select all

    int i=10;
    printf("Restarting in %d seconds \n", i);
    for (; i >= 0; i--) {
        printf(".\n");
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
IOW, it appears that a call to printf() does not result in output to the terminal until a \n is injected into printf()'s arguments.

Based on my previous experience with printf() on embedded systems, this is not normal behavior. Is this intentional? Is there some option I need to set to ensure that whatever I send via printf() is output to the terminal, without requiring a \n?

Thanks,

ESP_igrr
Posts: 2072
Joined: Tue Dec 01, 2015 8:37 am

Re: printf() requires a \n for output?

Postby ESP_igrr » Sun Oct 29, 2017 6:34 am

By default, output streams have line buffering enabled.

You can configure buffering for any particular stream using setvbuf function.

More info:
http://en.cppreference.com/w/c/io/setvbuf
https://github.com/espressif/newlib-esp ... vbuf.c#L20

For example,

Code: Select all

setvbuf(stdout, NULL, _IONBF, 0);
disables buffering on stdout.

Note that you will generally get better performance if buffering is used. You can also use fflush(stdout) to flush the buffer without sending a newline.

aekalman
Posts: 7
Joined: Fri Oct 27, 2017 10:34 pm

Re: printf() requires a \n for output?

Postby aekalman » Fri Nov 03, 2017 9:18 pm

Thank you -- I suspected something like that, but didn't know where to look.

Thanks again,

Who is online

Users browsing this forum: Google [Bot] and 100 guests