News:

As usual while waiting for the next release - don't forget to check the nightly builds in the forum.

Main Menu

Strange output in CodeBlocks with gcc in Windows

Started by jimshen, March 06, 2006, 12:57:42 PM

Previous topic - Next topic

jimshen

I use CB in windows and the compiler is gcc. When I build and run the program below, I found the result is strange. Can anyone give me an explain?

#include <stdio.h>
int main()
{
    int a=5,b=6,c=7;
    printf("%d\n%d %d\n %d %d\t\b%d\n",a,b,c,a,b,c);
    printf("%d %d\t\b%d\n",a,b,c);
    return 0;
}

The result is:

5
6 7
75 6
5 67

I tried DevC++ which use MinGW as compiler and got same result. It seems that is the problem of MinGW.


The result should be (When use Turbo C in DOS or gcc in Linux)
5
6 7
5 6  7
5 6   7

Michael

[url="http://img207.imageshack.us/img207/9728/411948picture4em.png"]http://img207.imageshack.us/img207/9728/411948picture4em.png[/url]

jimshen

#2
Quote from: Michael on March 06, 2006, 01:14:29 PM
Hmmm, what about the \b?

Best wishes,
Michael


\b and \t.

Michael

Quote from: jimshen on March 06, 2006, 01:18:01 PM
\b is backspace escape character

Ah yes. Thanks :). I have found in google too (was not so easy though). I never use it anyway.

AFAIK, the output of the second printf is correct. You do a \t and then a \b.

Best wishes,
Michael
[url="http://img207.imageshack.us/img207/9728/411948picture4em.png"]http://img207.imageshack.us/img207/9728/411948picture4em.png[/url]

jimshen

#4
Quote from: Michael on March 06, 2006, 01:32:47 PM
Ah yes. Thanks :). I have found in google too (was not so easy though). I never use it anyway.

AFAIK, the output of the second printf is correct. You do a \t and then a \b.

Best wishes,
Michael


The result is not same as the result in linux. Please see the first message I modified.

Michael

Quote from: jimshen on March 06, 2006, 01:37:38 PM
The result is not same as the result in linux. Please see the first message I modified.

Yes, I know. I have read the modified version already and tried myself in ubuntu :). I have done some tests, both in Window and in Linux. When I only use \b, the behaviour seems to be the same. But when I use \t\b then the result is different.

Best wishes,
Michael
[url="http://img207.imageshack.us/img207/9728/411948picture4em.png"]http://img207.imageshack.us/img207/9728/411948picture4em.png[/url]

jimshen

Quote from: Michael on March 06, 2006, 01:53:10 PM

But when I use \t\b then the result is different.

Best wishes,
Michael


That's the problem trouble me.

thomas

Don't blame the compiler, it does exactly what you tell it to. If you do a hexdump of the executable, you will see that the text location in question reads 25 64 09 08 25 64. It does exactly what you asked for.

It is the MSVCRT which does not handle it correctly. Obviously, Microsoft does not expect anyone to mix tabs and backspaces (I could not imagine a good reason to do so either).
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

jimshen

Quote from: thomas on March 06, 2006, 02:04:38 PM
Don't blame the compiler, it does exactly what you tell it to. If you do a hexdump of the executable, you will see that the text location in question reads 25 64 09 08 25 64. It does exactly what you asked for.

It is the MSVCRT which does not handle it correctly. Obviously, Microsoft does not expect anyone to mix tabs and backspaces (I could not imagine a good reason to do so either).

Thank you very much. I build the program in Visual c++ and got the same result. It seems that is the problem of MSVCRT.
The code is in a book I'm reading now.