Questions about the newline character \n

Discussion in 'iOS Programming' started by redchannel, Jun 18, 2009.

  1. redchannel macrumors regular


    Nov 23, 2008
    I have an example of code that I need help understanding:

    #import <Foundation/Foundation.h>


    int main (int argc, const char *argv [])

    NSAutoreleasePool *pool =[ [NSAutoreleasePool alloc] init];

    NSLog [@"Testing...\n...1\n...2\n...3");

    [pool drain];

    retuen 0;


    is the \n (newline command) another way to say NSLog [@"Insert text here"] without having to type in the NSLog stuff (sorry i dont know the exact terminology of this thing) ?

    I know its a new line on the output. When the program launches will it simply say

    ...3 ?

    on the scree ? Is this just a program to display text ?
  2. wlh99 macrumors 6502

    Feb 7, 2008
    Newline is like the enter key. Output after the newline will start on the next line. Without it ..

    NSLog (@"One ");
    NSLog (@"Two");

    One Two

    The program actually does nothing. NSLog writes to the log (usually the console view that is available in XCode) when testing a program in the Debug configuration. In a release configuration NSLog is ignored.

    To actually write to the iPhone screen is much more complicated.

    If you are interested in learning iPhone or Mac programming, read the stickies and invest in a recomended book. You should be much further than this snippet by the first couple pages.
  3. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    No, it doesn't. It produces something like this:
    2009-06-18 17:49:48.729 AppName[7265:20b] One
    2009-06-18 17:49:48.730 AppName[7265:20b] Two

    produces something like this:
    2009-06-18 17:52:49.905 AppName[7304:20b] Testing...
  4. Saladinos macrumors 68000


    Feb 26, 2008
    Newline characters are one of several special glyphs. These glyphs exist because simply typing in the effect they represent may cause problems in certain situations.

    For example, to include a quotation mark, type \". Typing " would confuse the compiler, which would think you meant to close the string. Similarly, new lines can cause errors and the newline character could simply be ignored by the compiler.

    And just in case, you were wondering, if you want to type a normal \, type \\. So \\n would show up as \n rather than a newline.
  5. kainjow Moderator emeritus


    Jun 15, 2000
    NSLog() works on Release and Debug. If you want it to not work on Release you need to do something like:
    #if DEBUG
    #define Log NSLog
    #define Log 
    And use Log() instead of NSLog().

Share This Page