C - printf() not working but puts() is working fine

前端 未结 3 655
暗喜
暗喜 2021-01-24 23:31
void read_class_information(head* beginning, int scale_type) {
    puts(\"hello\");
    // printf(\"hello\");
}

I have a simple function called by main

3条回答
  •  遥遥无期
    2021-01-25 00:04

    By default, stream buffering is set to line buffered, which means nothing is really sent to the stream until a new line character \n is found. The three buffering methods are:

    • _IONBF: unbuffered
    • _IOLBF: line buffered
    • _IOFBF: full buffered

    You can change the buffering method for any stream. In this case, you may want to change the buffering method for stdout:

    setvbuf(stdout, (char *)NULL, _IONBF, 0);
    

    In this way, you don't need to fflush(stdout); everytime you want to print something without a newline. This has some performance issues which may or not affect to you, so you decide which is better for you.

    As usual, you have access to the documentation executing man setvbuf (if you have the docs installed, of course).

提交回复
热议问题