Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
can anyone explain the use of %s in an objective c code.
When do you use it? Why do you use it?
Its is the placeholder for example let say you are in a classroom you put your bags or books as placeholder which represent you Let try understand by code. <code> String name= "your name" printf(my name is %s",name); </code> The string variable will be substitute at the placeholder. Note:there is nothing like string type in C
%s is for a string of characters. Earlier in this course we've used %c for characters, %d for integers, %f for floats. Here we use %s for a string as we're pointing to several characters bound together in an array so %c wouldn't work anymore.
Can you be more specific? There is many ways you can use that, did you mean String Format Specifiers in general or just %s ?
not string format specifiers, just %s.
specifically in this piece of code
char sahil[ ] = "sahil"; letter = &sahil; printf("%c is in %s\n",*letter, sahil);
Now, when I try to do that example of code you showed, it came out to be incorrect.
But when I put [ ] after String name [ ] = "your name" printf(my name is %s",name);
Then the code was performed well in Xcode.
What does relation does [ ] have with %s?
Thank you very much
Varuzhan Darbinyan8,360 Points
%s is for array of characters. Here is more information about Format Specifiers. https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html