Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c stdout print without new line?

Tags:

c

printf

stdout

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>");
}
like image 912
Hashan Avatar asked Sep 15 '11 13:09

Hashan


People also ask

Does printf in C print a new line?

The printf statement does not automatically append a newline to its output. It outputs only what the format string specifies. So if a newline is needed, you must include one in the format string.

Does puts add a newline?

Puts automatically adds a new line at the end of your message every time you use it. If you don't want a newline, then use print .

What is printf \n in C?

The escape sequence \n means newline. When a newline appears in the string output by a printf, the newline causes the cursor to position to the beginning of the next line on the screen.

How to print string without newline in Python?

The new line character in Python is \n . It is used to indicate the end of a line of text. You can print strings without adding a new line with end = <character> , which <character> is the character that will be used to separate the lines.


3 Answers

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.

like image 72
Dave Goodell Avatar answered Nov 15 '22 07:11

Dave Goodell


Call fflush after printf():

int main (){
    printf("CLIENT>");
    fflush( stdout );
}
like image 22
MByD Avatar answered Nov 15 '22 07:11

MByD


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.

like image 25
Igor Avatar answered Nov 15 '22 05:11

Igor