Recently while solving one thread I was stuck with following problem.
Am getting two different outputs for same code. One was executed on my machine and another one on codepad.com.

Anyone with any suggestion why this might be happening ?

Codepad.com's Output
7316aa7bc992df41bee38de22b55db18

My Machine's Output
ac76eff29302f4b2d1466525c3675909

Thanx.

It is how compliant C compilers interpret backslash (escaped) characters in a string. Let's look at your printf string from a stringent C/C++ perspective: "\nab\bcd\ref"

The "\n" should be translated to a new-line (CR/LF on Windows, and LF on Unix/Linux). This should drop the cursor down one line, to the beginning of the next line. The "\b" should be a back-space, and "\r" would be a carriage-return (go to beginning of the line). These are non-printing characters, but manipulate the cursor position.

So, strictly speaking, this string should display as "ef" with a blank line before. Why? First new-line drops cursor one line down on display. Then it prints "ab", then it backspaces over the "b", continuing to print "cd", then it goes back to the beginning of the line with the "\r" erasing the output, and prints "ef". So, assuming that the "\b" is actually a backspace, then the output should be a blank line followed by "ef".

IE, nether of these examples you provided are C-standard compliant! Unfortunately, Microsoft has its own idea of "standards compliance"... This is why, when we want to put directory paths in C/C++ strings, we use forward slashes - no improper interpretation will happen, and MS compilers will handle the forward slash directory separators correctly, even though it uses back-slashes itself for such separators. :-)

@rubberman: Should't output be blank line follwed by efd ?

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 ?

This guy had same problem.

Edited 3 Years Ago by Rahul47: mistakes

then it goes back to the beginning of the line with the "\r" erasing the output

Not no any computer I have used. \r simply moves the cursor back to the beginning of the line, everything on the line remains intact.

Not no any computer I have used. \r simply moves the cursor back to the beginning of the line, everything on the line remains intact.

Yea, exactly and after that it overwrites with what is followed by \r . Right ?

This article has been dead for over six months. Start a new discussion instead.