views:

83

answers:

3

How can I print "\n" to the console by using NSLog or printf function?

Basically I was trying to print a String with value "Hello\nWorld". My requirement is, I want to print it AS IT IS. But when I tried to print, it was printing like,

Hello
World   

not as

Hello\nWorld
+7  A: 

Escape it using another backslash.

NSLog(@"Hello\\nWorld");
BoltClock
Output is Hello\World.Not as I expected.
prathumca
@prathumca: I get `Hello\nWorld` as output.
BoltClock
@BoltClock: Surprisingly this is working on Simulator not on iPhone. Why?
prathumca
@BoltClock: After cleaning all my targets, this seems to be working. thanks for your time.
prathumca
+2  A: 

Try "Hello\\nWorld". If you want a literal \ in a string you need to escape it with another \ .

Sven
A: 

I use C strings (except in the very rare case where I'm debugging something specific to Unicode):

NSLog( [ NSString stringWithFormat:@"%s", "Hello\nWorld" ] );

Make the above into a macro taking C parameters, and it becomes portable between C and Obj C.

hotpaw2