NO

Author Topic: putchar('\b') in win32 console program  (Read 7213 times)

inobor

  • Guest
putchar('\b') in win32 console program
« on: November 07, 2012, 11:17:36 AM »
Hello!

Sorry, I am a bit confused with following

int main( void ) {
   putchar('a');
   putchar('b');
   putchar('c');
   putchar('d');
   putchar('\b');
}

When I execute this in IDE, all looks like:
abcPress any key to continue...
which is expectable. But if run the command.com (Windows XP) and then run
the exe file, the output is:
abcd
If I redirect output to file Name.exe > test.txt
I still see the 08H byte at the end. This is most likely related to some sort of console
buffering issues, but the exactly source of output difference?

Thank you

CommonTater

  • Guest
Re: putchar('\b') in win32 console program
« Reply #1 on: November 07, 2012, 12:00:23 PM »
It most likely has to do with the way the console handles newlines... since your sequence of characters has no newline at the end when in an open console, it has to add one to display it's prompt.
 
The \b, backspace (0x08) moves the cursor back one space but does not erase the character until you overwrite it... In the IDE's captive console the overwrite is done by the P in the "Press any key" prompt.  In the open console, there's nothing to overwrite it, along comes the CR/LF pair before the prompt and your last character pops back up.

If you do this...
Code: [Select]
int main( void ) {
   putchar('a');
   putchar('b');
   putchar('c');
   putchar('d');
   putchar('\b');
   putchar(' ');        // overwrite the 'd'
}
...it should give you the same result both times. 
 

inobor

  • Guest
Re: putchar('\b') in win32 console program
« Reply #2 on: November 07, 2012, 12:47:22 PM »
Thank you, Tater

If I got it right, to use \b "which would delete the preceding character"
based on
http://en.wikipedia.org/wiki/Backspace#Backspace_key_in_computers
is correct only if after \b I putchar another letter?

CommonTater

  • Guest
Re: putchar('\b') in win32 console program
« Reply #3 on: November 07, 2012, 01:23:20 PM »
No the sequence of \b<space> won't delete the previous character... and it won't back move anything that's behind it.  All it does is replace the previous character with a space.
 
The thing is that the console's control sequences were used intitially to imitate typewriter behaviour... 

Thus when you backspace (\b) you are simply moving the cursor back one space...  that does not remove the character from the screen, it simply moves the cursor... The character will remain visible until something overwrites it.  Just like on your good old Underwood typewriter.  (matter of fact, the old right margin bell is still there too ...  try  putchar('\x07'); )

Here's a slightly more advanced version of your program, with time delays added so you can see the individual steps...

Code: [Select]
#include <stdio.h>
#include <windows.h>
 
int main (void)
  {
    putchar('a');
    putchar('b');
    putchar('c');
    putchar('d');
    Sleep(2000);  // two second time delay
    putchar('\b');
    Sleep(2000);
    putchar(' ');
    Sleep(2000);
    puts("");
   
    return 0;
  }

For a couple of seconds during the second delay you should see: abcd  with the cursor under the d.  Then when the delay expires you will see the d disappear when the space is printed.
 
The Wikipedia article is about the end-user behaviour of various keyboard keys. 
 
As a programmer you are exposed to a deeper level than end-users see.  Most times we will take multiple steps to recreate something the end user does with a simple click or keypress.  For example: Consider what needs to be done to insert one character in the middle of a character array (string).  It's not a trivial task...
 
As programmers our job is to make this kind of stuff easy for the end users...
 
« Last Edit: November 07, 2012, 01:54:03 PM by CommonTater »

inobor

  • Guest
Re: putchar('\b') in win32 console program
« Reply #4 on: November 07, 2012, 05:59:03 PM »
Great, Tater

Thank you again & have handshake \w/
Sorry, should be _sleep(seconds), not Sleep(milliseconds)

Inobor

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: putchar('\b') in win32 console program
« Reply #5 on: November 07, 2012, 07:22:40 PM »
Sorry, should be _sleep(seconds), not Sleep(milliseconds)
Well, out of personal experience, I would say that should rather be sleep (hours) instead...  ;D

As to the initial issue, you need to realize that the "\b" and similar "special characters" are from a time when computers used character based text terminals rather than bit-mapped oriented displays. Specially in a Windows console window, a lot of those don't work as expected anymore, as screen output is more "optimized" nowadays and does not properly emulate those old times character based commands...

I haven't tested it myself yet, but I am sure if you would run this in a plain DOS machine (real iron, not some VM), at least with an installed ANSI.SYS (or equivalent), it would still work as you expected...

Ralf

inobor

  • Guest
Re: putchar('\b') in win32 console program
« Reply #6 on: November 07, 2012, 08:08:09 PM »
Thanks, Ralf

Well, this is quite surprising about Windows console.
I thought it's gonna work for K&R book exercises

CommonTater

  • Guest
Re: putchar('\b') in win32 console program
« Reply #7 on: November 08, 2012, 12:33:50 AM »
Great, Tater

Thank you again & have handshake \w/
Sorry, should be _sleep(seconds), not Sleep(milliseconds)

Inobor

_sleep() is from the C library and works in seconds. 
Sleep() is from the Windows API (hense the #include <windows.h>) and works in milliseconds.
 
Don't worry about the Windows console with K&R ... many many people have learned C with that combo without any hassles.  Just make sure you have the latest K&R (for ANSI C) and you should do fine. Nobody uses the console ctrl-key codes anymore... just print line by line and you'll do fine.
 
Also give this a read... http://forum.pellesc.de/index.php?topic=4644.0  It might help you a little.
 
« Last Edit: November 08, 2012, 12:38:08 AM by CommonTater »

inobor

  • Guest
Re: putchar('\b') in win32 console program
« Reply #8 on: November 08, 2012, 05:46:17 AM »
Thank you for link :)
I may share something as well - but I believe you guys have it already.
Otherwise, if not, it make sense to put it where everyone may see it.
http://www.di-mgt.com.au/src/trapsandpitfalls.pdf

Copyrighted tutorials:
http://www.tutorialspoint.com/cprogramming/cprogramming_tutorial.pdf
http://www.zetcode.com/gui/winapi/
http://www.iu.hio.no/~mark/CTutorial/CTutorial.html#Top
« Last Edit: November 08, 2012, 10:43:55 AM by Stefan Pendl »