PDA

View Full Version : printf backslash r and b not working in XCode




Len Wine
Feb 22, 2009, 05:28 PM
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.



HiRez
Feb 22, 2009, 06:41 PM
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).

autorelease
Feb 22, 2009, 11:11 PM
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().

MacEll
May 27, 2009, 07:48 PM
> 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?

svennijs
Jul 17, 2009, 05:00 PM
>

This seems to be the case, but only for some people.

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

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.

TazExprez
Dec 26, 2010, 12:54 AM
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.

cherry su
Dec 28, 2010, 04:22 PM
\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.

Berlic
Jan 18, 2011, 02:08 PM
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!

FloridaJo
Nov 10, 2011, 05:52 AM
I can confirm that on Xcode 3.2.6 with OSX 10.6.8 that \b does not work, but \r does. :)

rossipoo
Nov 10, 2011, 11:08 AM
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.