简体   繁体   中英

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

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!

stdout by default is 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 (ie "%d\\n" ), or calling fflush on stdout after printing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM