Raspberry Pi: printf() doesn't work with wiringPi

4.5k views Asked by At

I'm trying a simple code that using wiringPi as here:

#include<wiringPi.h>
#include<stdio.h>

int main(void){
    int i;

    wirintPiSetup();
    pinMode(0,OUTPUT);   //a single LED
    pinMode(8,INPUT);    //tactile switch

    for(;;){
        delay(500);
        //push tactile switch and LED is turning on
        if(digitalRead(8)) digitalWrite(0,0);
        else digitalWrite(0,1);
        printf("%d",digitalRead(8));
    }
}

I expected a result of printf() is output to console, but it doesn't work. printf() couldn't run in same time with wiringPi APIs?

no warnings at compile. and CPU consumption is always under 4%. running on Raspbian.

Thanks for your time!

1

There are 1 answers

4
Drew McGowen On BEST ANSWER

stdout by default is typically line-buffered, meaning it tries to put off writing data to the underlying file until a newline. But since you never print a newline, stdout will just buffer your text until it runs out of space.

You can fix this by either adding a newline in the format string (i.e. "%d\n"), or calling fflush on stdout after printing.