printf backslash r and b not working in XCode

Discussion in 'Mac Programming' started by Len Wine, Feb 22, 2009.

  1. macrumors newbie

    Joined:
    Feb 22, 2009
    Location:
    Toano, VA
    #1
    I am relearning C after a long layoff. Going through the book "Learn C on the Mac." One of the sample programs goes through using the backslash controls in the printf function. Here is the code:

    #include <stdio.h>

    int main (int argc, const char * argv[]) {
    printf( "0000000000\r" );
    printf( "11111\n" );

    printf( "0000\b\b11\n" );

    printf( "Here's a backslash...\\...for you.\n" );
    printf( "Here's a double quote...\"...for you.\n" );

    printf( "Here are a few tabs...\t\t\t\t...for you.\n" );

    return 0;
    }

    and here is the result when built and run in XCode:

    0000000000
    11111
    000011
    Here's a backslash...\...for you.
    Here's a double quote..."...for you.
    Here are a few tabs... ...for you.

    Here is the result when run in the Terminal:

    1111100000
    0011
    Here's a backslash...\...for you.
    Here's a double quote..."...for you.
    Here are a few tabs... ...for you.

    It appears that \r and \b are not working in XCode. \r is causing a new line and \b seems to have no effect. I suspect that there is a setting in XCode preferences that is causing this but I can't find it. I have tried compiling with the different compilers offered in XCode but there hasn't been any change. This isn't a big issue as I can't see myself using either \r or \b but I'm curious why they don't work in XCode. Several other folks have reported this on the author's blog but he hasn't been able to reproduce it.
     
  2. macrumors 601

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #2
    I think the difference is probably accounted for because C apps compiled through Xcode use GNU C/OpenBSD libraries, whereas command line tools in Terminal are using POSIX libraries, and what's supported by printf is slightly different between the two. I'm just guessing at this though. If you really need the backslash functionality, you could probably create a method that wraps the command-line version in an NSTask.

    If you look up printf in the Xcode documentation, note the difference between printf(1) and printf(3).
     
  3. macrumors regular

    Joined:
    Oct 13, 2008
    Location:
    Achewood, CA
    #3
    Nonsense. There is only one standard library implementation on the Mac. Besides, Xcode is just a frontend to the same GCC you use from the terminal.

    The likely culprit is that Xcode's console just doesn't interpret the carriage return and backspace escape sequences.

    printf(1) and printf(3) are two totally different things. printf(1) is a command-line tool for printing formatted output. printf(3) is the documentation for the actual C function in the standard library. Section 1 of the manual is for command-line utilities. Section 3 is for the C standard library. There is no difference between programs compiled from Xcode and programs written in a text editor and compiled from the terminal. There is only one C library and only one implementation of printf().
     
  4. macrumors newbie

    Joined:
    May 27, 2009
    #4
    Xcode console window - printf \r and \b don't work

    > The likely culprit is that Xcode's console just doesn't interpret
    > the carriage return and backspace escape sequences.

    This seems to be the case, but only for some people. According to Dave Mark, the author of the book "Learn C on the Mac", Xcode's console window interprets \r and \b correctly. However several readers had problems with his example 05.03 - slasher, as can be seen by Errata postings at: http://www.apress.com/book/errata/1108

    You can download the source code in question at:
    http://www.apress.com/book/downloadfile/4208

    Can you explain why some people see the problem, and others don't?
     
  5. macrumors newbie

    Joined:
    Jul 17, 2009
    Location:
    Brussels, Belgium
    #5
    I also started programming C. I use the same book and Xcode 3.1.3. And I encounter the same problem with \b and \r.
     
  6. macrumors member

    Joined:
    Feb 4, 2010
    Location:
    New York
    #6
    I am also having this problem and I'm reading the same book. Has anyone solved the problem? Sorry for resurrecting an old thread. Thanks for any help.
     
  7. macrumors 65816

    cherry su

    Joined:
    Feb 28, 2008
    #7
    \r is supposed to output a new line
    \b deletes the character before it. Since you had "0000\b\b11\n", you correctly got 0011 because the two \b's deleted two 0s.
     
  8. macrumors newbie

    Joined:
    Jan 18, 2011
    #8
    I confirm that \r and \b do not work in XCode's console. Sick!
    I spent almost 4 hours to find out the problem until I discovered this topic.
    I use fresh XCode 3.2.5

    When I execute my app in Terminal -- everything work as expected!
     
  9. macrumors newbie

    Joined:
    Dec 27, 2008
    #9
    I can confirm that on Xcode 3.2.6 with OSX 10.6.8 that \b does not work, but \r does. :)
     
  10. macrumors regular

    Joined:
    Jun 7, 2009
    #10
    Wirelessly posted (Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3)

    This isn't all that surprising. The Xcode console is not a terminal. If you need this functionality, just open your application in the Terminal application instead.

    I wouldn't expect this behavior to be added anytime soon.
     

Share This Page