Member Avatar for Rahul47

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. :-)

Member Avatar for Rahul47

@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.

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.

Member Avatar for Rahul47

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 ?

Member Avatar for Rahul47

Tested in linux.

46cd699b4cc58283b0e3cbd2cdd7c213

Be a part of the DaniWeb community

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