printf backslash r and b not working in XCode

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

  1. Len Wine macrumors newbie

    Feb 22, 2009
    Toano, VA
    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:

    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:

    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. HiRez macrumors 603


    Jan 6, 2004
    Western US
    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. autorelease macrumors regular

    Oct 13, 2008
    Achewood, CA
    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. MacEll macrumors newbie

    May 27, 2009
    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:

    You can download the source code in question at:

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

    Jul 17, 2009
    Brussels, Belgium
    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. TazExprez macrumors regular

    Feb 4, 2010
    New York
    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. cherry su macrumors 65816

    cherry su

    Feb 28, 2008
    \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. Berlic macrumors newbie

    Jan 18, 2011
    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. FloridaJo macrumors newbie


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

    Jun 7, 2009
    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