Thread: slash-b doesn't work when followed by slash-n

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

    Question slash-b doesn't work when followed by slash-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,005
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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,057
    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. comma and slash problem
    By orhanli1 in forum Windows Programming
    Replies: 1
    Last Post: 02-24-2011, 07:33 PM
  2. subnet string to slash notation?
    By homer_3 in forum C Programming
    Replies: 8
    Last Post: 01-12-2011, 05:49 PM
  3. Replies: 1
    Last Post: 12-07-2010, 06:53 AM
  4. Replace _last_ slash in directory name string
    By rkooij in forum C Programming
    Replies: 11
    Last Post: 08-22-2006, 06:11 AM
  5. Can I hide the slash in a console window?
    By Marcos in forum C Programming
    Replies: 3
    Last Post: 07-24-2003, 06:53 AM