Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Cocoa’s printf-style formatting not work as expected when using %@ conversion specifier and field width?

My understanding of printf-like format strings is that you can prefix any conversion specifier with a minimum field width. This does not seem to work for Cocoa’s %@ specifier. Example:

NSLog(@"'%5@'", @"foo");
NSLog(@"'%5s'", [@"foo" UTF8String]);

Output:

… 'foo'
… '  foo'

Is this the intended behavior?

like image 233
Nikolai Ruhe Avatar asked Mar 23 '10 15:03

Nikolai Ruhe


People also ask

What will happen if you use wrong formatting characters in printf?

Oh yes - printf depends on the format string to determine the size and type of the variable to fetch next. When the format string is wrong it may try to fetch a variable that isn't even there, with all consequences that may have.

What happens if the variable type and the format specifier do not match?

Any printf format/argument mismatch will cause erroneous output, so you cannot rely on anything once you do that.

What is the format specifier used for printing character array using printf statement?

String Format Specifier %s The %s format specifier is implemented for representing strings. It is used in the printf() function for printing a string stored in the character array variable.


1 Answers

%@ is only for objective-c object. Thus, the field width will be invalid, if the object is not NSString.

I didn't know that %5@ is formatted to be the same as %@.

like image 51
Jesse Armand Avatar answered Oct 04 '22 11:10

Jesse Armand