karthikbalaguru said:
Interesting !!
Finally, it boils down to two possibilities.
Does it mean that gcc V 4.3.2 has the fixes and hence
gives the correct output . But, not gcc V 3.4.4 (Older
version of gcc) ?
Or,
Does it mean that gcc based on windows environment (cygwin),
give output similar to Visual C++ 2008 Express edition, but
not the gcc of the linux OS ?
No and, respectively, no.
Here's the program:
#include<stdio.h>
int main(void)
{
printf("\n12");
printf("\b34");
printf("\r56");
return 0;
}
First of all, the differing versions of gcc are unlikely to matter.
The output of the program depends on the runtime library, not on the
compiler; the compiler should just generate a sequence of calls to
printf. printf is part of the runtime library, not the compiler. The
behavior of printf is unlikely to change across versions of the
runtime library.
If you're seeing different output on Cygwin vs. Linux, I strongly
suspect it's because you have a different shell prompt -- and in fact
the default bash prompt on Cygwin starts with a new-line. The actual
output is probably the same in both cases; it's just being overwritten
differently. Assuming that the response to the control characters is
what's typical for both systems, here's what should happen when the
program runs:
It prints a new-line, leaving an empty line and jumping to the
next line.
It prints "12".
The cursor moves back one column (without erasing anything),
leaving it over the '2'.
It prints "34"; the '3' overwrites the '2', leaving "134".
It prints a '\r', moving the cursor to the beginning of the
line, on top of the '1'.
It prints "56". The '5' overwrites the '1', and the '6'
overwrites the '3', leaving us with "564", and the cursor on top
of the '4'.
The program terminates.
So the net result of the program is to display a blank line followed
by "564", and to leave the cursor sitting on top of the '4'. What
happens next is outside the program's control. On both Cygwin and
Linux, assuming you ran the program from the shell, the shell then
prints its prompt. On Cygwin, where the prompt begins with a
new-line, the cursor moves to the next line and (what's left of) the
program's output remains visible. On Linux, where the prompt doesn't
begin with a new-line, the prompt is written on the same line as the
end of the program's output, and the trailing '4' is clobbered.
You can get exactly the same behavior on Cygwin and Linux if you set
the prompt (in bash, "PS1=whatever") to the same value on both.
A couple more things to try (these commands are system-specific, but
will work on both Cygwin and Linux; assume the program's executable is
"./prog"):
./prog ; sleep 5
# Wait 5 seconds before printing the prompt.
./prog | cat -A
# Print control characters in a visible way; end-of-line is $,
# backspace is ^H, return is ^M.
./prog > prog.out
view prog.out
# Or use your preferred editor to examine the output.
The standard allows some freedom in what output the program will
produce. On a system where the last line of a text stream must be
terminated by a new-line, the behavior is undefined. But on the
particular systems in question, this doesn't apply; the program will
just print the specified characters to stdout. There's a difference
between the output the program produces (which is well defined on
these systems) and what appears in your terminal window. This *can*
be affected by terminal settings, but that's not what's happening in
this case (though it was a good guess). The difference is explained
by the setting of the shell prompt -- something entirely outside the
scope of C.