\b doesn't work when followed by \n

This is a discussion on \b doesn't work when followed by \n within the C Programming forums, part of the General Programming Boards category; Hi. I'm writing the code for Exercise 2 from Chapter 1 of K&R's book. I found this problem when i ...

  1. #1
    C Beginner()
    Join Date
    Oct 2004
    Posts
    22

    Question \b doesn't work when followed by \n

    Hi. I'm writing the code for Exercise 2 from Chapter 1 of K&R's book. I found this problem when i try to show an example of how \b works. To guide you into my way of thinking i'll post the code i've written so far:

    Code:
    #include <stdio.h>
    
    main()
    {
        printf("\\a: Audible bell.\a\n");
        printf("\\b: Backspace..\b\n");
    }
    I hoped that the second printf statement would print "\b: Backspace." to output and then print the newline character. However, it printed "\b: Backspace.." and then the newline character . I was surprised by this so i decided to put a space between \b and \n. I compiled the corrected program and now it works as i expected it to. Can anyone tell me why the \b\n combination does not work? I'm using gcc v. 3.4.5. Besides, the combination \a\n works since i hear the beep from the speaker and see how the newline character is printed to output.

    Thanks.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by rllovera
    I hoped that the second printf statement would print "\b: Backspace." to output and then print the newline character. However, it printed "\b: Backspace.." and then the newline character . I was surprised by this so i decided to put a space between \b and \n. I compiled the corrected program and now it works as i expected it to. Can anyone tell me why the \b\n combination does not work?
    Who says it didn't work correctly? I think it's just your expectation that is wrong. Going back a space does not necessarily mean to delete the previous character. It just means go back a space.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    C Beginner()
    Join Date
    Oct 2004
    Posts
    22
    Quote Originally Posted by Dave_Sinkula
    Who says it didn't work correctly? I think it's just your expectation that is wrong. Going back a space does not necessarily mean to delete the previous character. It just means go back a space.
    Ok. But if i write

    Code:
    printf("\\b: Backspace..\b\n");
    it should print only the first dot after the word "Backspace" since the second dot should be replaced in the output by the newline character that comes after the backspace character. This doesn't happen. How do you explain this behavior?
    By the way, i realize that the \b sequence does not delete a character. I just expected that this piece of code would not display the second dot after the "Backspace" word for the reason that i mentioned in the paragraph above. I hope i'm making my point clearly. Sorry if i'm not, english is not my mother tongue.

    Thanks.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    Both newlines and backspace (and some others like form feed and vertical tab) just move the cursor around, they don't actually have any visible representation (other parts of C regard them as white space).

    It's not like moving the cursor around in your text editor and pressing return when there are characters to the right of the insert point.

    Try
    "..\b?\b\n"

  5. #5
    C Beginner()
    Join Date
    Oct 2004
    Posts
    22
    Quote Originally Posted by Salem
    Both newlines and backspace (and some others like form feed and vertical tab) just move the cursor around, they don't actually have any visible representation (other parts of C regard them as white space).

    It's not like moving the cursor around in your text editor and pressing return when there are characters to the right of the insert point.

    Try
    "..\b?\b\n"
    I guess your point is that the first \b allows the replacement of the second dot by ? while the second \b does not allow the replacement of ? by a newline character. Am i right?

    Thanks.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    That's right. printting a \b moves the cursor to the left one character, but it doesn't erase anything. If you want to move the cursor to the left and erase the character, use something like this:
    Code:
    printf("\b \b");
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    C Beginner()
    Join Date
    Oct 2004
    Posts
    22
    Quote Originally Posted by dwks
    That's right. printting a \b moves the cursor to the left one character, but it doesn't erase anything. If you want to move the cursor to the left and erase the character, use something like this:
    Code:
    printf("\b \b");
    Ok. Thanks for the tip, dwks. It's so easy not giving these kind of things the importance they have because they seem "a ridiculous thing for begginers" that, sometimes, you lose some important lesson to be learned. I'm aware that we're talking about printing a backspace after all, but there are some subtleties (such as the one that led to this thread) which are there and are easy to overlook if you are not careful.

    Thanks again to you all.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21