Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

knobcreekman

macrumors newbie
Original poster
Dec 29, 2008
4
0
hello all, I am new to this forum and new to programming as you all will be able to tell by my question. So, I am going through the "Learn C on the Mac" book, and I am at the part that discusses backslash combinations. He says that "\r" will place the cursor back at the beginning of the same line and if followed by another printf() function the original line of text will be overwritten. So:

#include <stdio.h>

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

return 0;
}

should actually only display the string within the second printf(). However, this does not work. It still prints both strings. Is this just a typo in the book and in the source code provided? Thanks for any help you can provide; I also welcome any advice you have for a wannabe programmer who is starting late in life for a career change. Thanks!
 

knobcreekman

macrumors newbie
Original poster
Dec 29, 2008
4
0
Hey thanks for the info and the link

so how do you do it... if the \b and \r are ignored by the compiler then how do you achieve the effect that those two slash combinations are supposed to produce?
 

chown33

Moderator
Staff member
Aug 9, 2009
10,740
8,416
A sea of green
The problem isn't the compiler at all. The code compiles just fine.

The problem is Xcode's Console window. It interprets the output of \r as a newline, which advances to the start of the next line.

When the sample program is run in Terminal.app, it exhibits the documented behavior: \r returns on the same line.

I did test builds using all available compilers in Xcode 3.2, and they all compiled programs that ran with identical behavior. That is, newline from \r in Xcode console window, but same-line from \r in Terminal window.
 

knobcreekman

macrumors newbie
Original poster
Dec 29, 2008
4
0
Thank you for your research and your help! I really appreciate it. Does this mean that the only place the code will not produce the desired output is within xcode's console? For instance, If I included that code in a future project that was a stand alone app, would the stand alone app behave like xcode's console or like terminal in terms of output? Forgive my lack of education on the subject, please.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,740
8,416
A sea of green
Thank you for your research and your help! I really appreciate it. Does this mean that the only place the code will not produce the desired output is within xcode's console?
I won't say Xcode's console is the only place. It's just one known place. There may be others, but you'd have to test it and see what happens.


For instance, If I included that code in a future project that was a stand alone app, would the stand alone app behave like xcode's console or like terminal in terms of output? Forgive my lack of education on the subject, please.

If you mean a double-clickable app that runs with windows and menus and buttons and such, then they don't have consoles. Code like that would probably never be used in such an app.

If you mean a command-line tool to be run in Terminal, then it will run like it runs in Terminal.

Frankly, carriage-control characters like \r and \b are almost never relied on to perform a specific universal carriage-control operation. Especially not when the program may not know exactly what device it's writing output to. When you want to perform fancy carriage-control in classical terminal-like environments, you usually use a library like ncurses. You could look that up and read about it, but I don't see much point at this time unless you want to be horribly confused.

I just don't see this as being a big obstacle in the overall strategy of learning to write Mac programs.
 

robvas

macrumors 68040
Mar 29, 2009
3,240
629
USA
For simple programs like that, just do everything from Terminal

Type 'nano' to open a text editor
(yes, emacs or vi are better, but don't worry about that now)

Enter your C program, save it as test.c

Then type:

gcc test.c -o test

Then type:

./test

It'll run your program!
 

knobcreekman

macrumors newbie
Original poster
Dec 29, 2008
4
0
yeah if this isn't even something that I would ever really use then I won't worry about it. That's the problem with just starting out... I'm not educated enough to discern what is important and what's not. Again, I really appreciate everyone's help
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.