"Learn C on the Mac" newb question

Discussion in 'Mac Programming' started by knobcreekman, Sep 2, 2010.

  1. knobcreekman macrumors newbie

    Dec 29, 2008
    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!
  2. Miglu macrumors member

    Jan 22, 2010
  3. knobcreekman thread starter macrumors newbie

    Dec 29, 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?
  4. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    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.
  5. knobcreekman thread starter macrumors newbie

    Dec 29, 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.
  6. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    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.

    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.
  7. robvas macrumors 68030

    Mar 29, 2009
    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:


    It'll run your program!
  8. knobcreekman thread starter macrumors newbie

    Dec 29, 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

Share This Page