6

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>");
}
Hashan
  • 175
  • 2
  • 2
  • 8
  • Possible duplicate of [Why does printf not flush after the call unless a newline is in the format string?](http://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin) – phuclv Oct 31 '16 at 17:05

3 Answers3

9

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.

Dave Goodell
  • 2,113
  • 16
  • 17
  • he asked without a newline. printf still prints a newline regardless of flushing. – Trevor Hickey Apr 03 '13 at 16:13
  • 1
    `printf` does not include a newline automatically. For example, you can write `printf("abc");printf("def");` and the string `abcdef` will appear (with no newlines in the middle or at the end) on the standard output. – Dave Goodell Apr 04 '13 at 17:27
5

Call fflush after printf():

int main (){
    printf("CLIENT>");
    fflush( stdout );
}
MByD
  • 129,681
  • 25
  • 254
  • 263
2

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.

Igor Oks
  • 24,930
  • 25
  • 85
  • 112
  • "Usually the old ones"? Not true. It's a feature known as buffering which is employed in most if not all modern environments, and which in fact might be *lacking* in older systems (although I can't think of any!) – gamen Sep 16 '11 at 11:16