c stdout print without new line?

2019-01-12 06:48发布

问题:

i want to print "CLIENT>" on stdout in c, without new line.
printf("CLIENT>");
does not print enything. how do i solve this?

int main (){
printf("CLIENT>");
}

回答1:

Try fflush(stdout); after your printf.

You can also investigate setvbuf if you find yourself calling fflush frequently and want to avoid having to call it altogether. Be aware that if you are writing lots of output to standard output then there will probably be a performance penalty to using setvbuf.



回答2:

Call fflush after printf():

int main (){
    printf("CLIENT>");
    fflush( stdout );
}


回答3:

On some compilers/runtime libraries (usually the older ones) you have to call fflush to have the data physically written:

#include <stdio.h>
int main( void )
{
  printf("CLIENT>");
  fflush(stdout);
  return 0;
}

If the data has newline in the end, usually fflush isn't needed - even on the older systems.



标签: c printf stdout