printf not print string after \n (Compiler GCC)

534 Views Asked by At

See my code

char t[]= "{\n abcdeffgjejgjergnjkengkknkn \n";
printf("%s",t);

char t1[]= "{ abcdeffgjejgjergnjkengkknkn \n aaffdefa";
printf("%s",t1);

Actual Output:

{
{ abcdeffgjejgjergnjkengkknkn

Expected output:

{
 abcdeffgjejgjergnjkengkknkn 
{ abcdeffgjejgjergnjkengkknkn 
 aaffdefa

Can any one help me why string is not getting print after \n (LF)?

Compiler - arm-none-eabi

Library header - Newlib

IDE: MCUExpresso

1

There are 1 best solutions below

2
Some programmer dude On

By default stdout (where printf writes) is line buffered. That means the output buffer is flushed (actually written) either when it's full or when you print a newline.

That's why the second part of the output isn't printed, because it's not enough to fill the buffer and you have no newline to flush the buffer.

You can flush explicitly yourself by calling fflush:

printf(...);
fflush(stdout);