Hello, the output of this code is 'ef' but if we add up a "newline" print after this print then the output becomes 'efd'. Then why in earlier scenerio the 'd' was not getting printed ?
Thanks in advance.

#include<stdio.h>

int main()
{
        printf("\nab\bcd\ref");
}

You print \n. Then you print ab\bcd on the second line. \b is the backspace leaving acd. Then you print \r which results in going to the start of the current line. Then you print ef. The lin already contained bcd so it will now contain efd with the position being on the d. If you add a printf("\n"); it would overwrite the d so the second line would contain bc then.

I'm not sure why the posted fragment prints efd for you. Maybe I'm wrong here. (Can't compile at the moment..)

Member Avatar

I think you might wanna check your output, because I executed your code on codepad.com and i got following output.

156eaa9475a094c5fe519b25fdf6d7e7

Member Avatar

I think you might wanna check your output.

As '\n' drops cursor one line down.
Then prints ab.
Then '\b' backspaces one character and prints cd. So now output is acd.
Finally '\r' returns carriage to beggining and overwrites ac with ef, so final output will be efd.

Hence output will be one line followed by efd.

Member Avatar

Tested in linux.

54ead8599cfdf6b88b1481a466afb827

@gonebe your output is wrong

@Rahul47 the website may be having some different comiling system, the web output is not correct.
plz check it on windows or linux system creating a .c file

@gonebe your output is wrong

No it's not. Using gcc I get \nefd as output when printing \nab\bcd\refd and the output is \nefg when printing \nab\bcd\refdg. The newline doesn't overwrite the 'd' which I also edited in my last post, unsure why it didn't went through.

the web output is not correct.
plz check it on windows or linux system creating a .c file

Based on what knowledge? As stated in the other thread about this it seems pretty likely compilers deal with this in different ways. Aside from it being a "puzzle" I don't see much practical use for this by the way..

Member Avatar

plz check it on windows or linux system creating a .c file

Ummm. . . I have checked windows output with Dev C++ Compiler and Linux no need for compiler as echo statement implements whitespaces.

Member Avatar

Aside from it being a "puzzle" I don't see much practical use for this by the way..

Well you never know when you come across a problem with it's practical use, having knowledge about different things is not a waste. :-)

Member Avatar

@Gnobe: You should check your first reply, I think you committed mistake there.

you said 'bcd' instead of 'acd'

The lin already contained bcd so it will now contain efd with the position being on the d.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.