PDA

View Full Version : "Learn C on the Mac" newb question




knobcreekman
Sep 2, 2010, 09:57 AM
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!



Miglu
Sep 2, 2010, 10:46 AM
This problem has been reported in http://apress.com/book/errata/1108. It seems that gcc does not support some of the backslash combinations anymore.

knobcreekman
Sep 2, 2010, 12:27 PM
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
Sep 2, 2010, 01:31 PM
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
Sep 2, 2010, 03:42 PM
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
Sep 2, 2010, 04:23 PM
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
Sep 2, 2010, 05:44 PM
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
Sep 2, 2010, 06:09 PM
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