Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Sep 2, 2010, 09:57 AM   #1
knobcreekman
macrumors newbie
 
Join Date: Dec 2008
"Learn C on the Mac" newb question

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 is offline   0 Reply With Quote
Old Sep 2, 2010, 10:46 AM   #2
Miglu
macrumors member
 
Join Date: Jan 2010
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.
Miglu is offline   0 Reply With Quote
Old Sep 2, 2010, 12:27 PM   #3
knobcreekman
Thread Starter
macrumors newbie
 
Join Date: Dec 2008
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?

Last edited by kainjow; Sep 2, 2010 at 01:44 PM. Reason: merged posts
knobcreekman is offline   0 Reply With Quote
Old Sep 2, 2010, 01:31 PM   #4
chown33
macrumors 603
 
Join Date: Aug 2009
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.
chown33 is offline   0 Reply With Quote
Old Sep 2, 2010, 03:42 PM   #5
knobcreekman
Thread Starter
macrumors newbie
 
Join Date: Dec 2008
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.
knobcreekman is offline   0 Reply With Quote
Old Sep 2, 2010, 04:23 PM   #6
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by knobcreekman View Post
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.


Quote:
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.
chown33 is offline   0 Reply With Quote
Old Sep 2, 2010, 05:44 PM   #7
robvas
macrumors 68000
 
Join Date: Mar 2009
Location: 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!
robvas is offline   0 Reply With Quote
Old Sep 2, 2010, 06:09 PM   #8
knobcreekman
Thread Starter
macrumors newbie
 
Join Date: Dec 2008
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
knobcreekman is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
iPhone "iCloud" lock question (NOT an "i need help") Vertigomap iPhone 5 Feb 27, 2014 06:39 AM
"Fun with Chess" game - Kids and and parents can play and learn omsonie iPad Apps 2 Jun 11, 2013 10:11 AM
"Find my Mac" Question Vashtbh Mac Applications and Mac App Store 0 Dec 15, 2012 11:20 AM
Start a new tab similar to "iOS blog" and "Mac blog" but make it "IPhone Leaks?" Dewroo Site and Forum Feedback 2 Aug 23, 2012 09:47 AM
Steam For Mac Help: Newb Question Prof. Community Discussion 3 Jul 23, 2012 11:07 AM

Forum Jump

All times are GMT -5. The time now is 01:24 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC