Author Topic: Strange output in CodeBlocks with gcc in Windows  (Read 7167 times)

Offline jimshen

  • Multiple posting newcomer
  • *
  • Posts: 11
Strange output in CodeBlocks with gcc in Windows
« on: March 06, 2006, 12:57:42 pm »
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
« Last Edit: March 06, 2006, 01:26:05 pm by jimshen »

Offline Michael

  • Lives here!
  • ****
  • Posts: 1608
Re: Strange output in CodeBlocks with gcc in Windows
« Reply #1 on: March 06, 2006, 01:14:29 pm »
Hmmm, what about the \b?

Best wishes,
Michael

Offline jimshen

  • Multiple posting newcomer
  • *
  • Posts: 11
Re: Strange output in CodeBlocks with gcc in Windows
« Reply #2 on: March 06, 2006, 01:18:01 pm »
Hmmm, what about the \b?

Best wishes,
Michael


\b and \t.
« Last Edit: March 06, 2006, 01:32:24 pm by jimshen »

Offline Michael

  • Lives here!
  • ****
  • Posts: 1608
Re: Strange output in CodeBlocks with gcc in Windows
« Reply #3 on: March 06, 2006, 01:32:47 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

Offline jimshen

  • Multiple posting newcomer
  • *
  • Posts: 11
Re: Strange output in CodeBlocks with gcc in Windows
« Reply #4 on: March 06, 2006, 01:37:38 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.
« Last Edit: March 06, 2006, 01:39:37 pm by jimshen »

Offline Michael

  • Lives here!
  • ****
  • Posts: 1608
Re: Strange output in CodeBlocks with gcc in Windows
« Reply #5 on: March 06, 2006, 01:53:10 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

Offline jimshen

  • Multiple posting newcomer
  • *
  • Posts: 11
Re: Strange output in CodeBlocks with gcc in Windows
« Reply #6 on: March 06, 2006, 01:58:09 pm »

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

Best wishes,
Michael


That's the problem trouble me.

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: Strange output in CodeBlocks with gcc in Windows
« Reply #7 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).
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline jimshen

  • Multiple posting newcomer
  • *
  • Posts: 11
Re: Strange output in CodeBlocks with gcc in Windows
« Reply #8 on: March 06, 2006, 02:29:06 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.