Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialSahil Gangele
6,851 Points%s
can anyone explain the use of %s in an objective c code.
When do you use it? Why do you use it?
Thank you
6 Answers
Mohd Maqbool Alam
Full Stack JavaScript Techdegree Student 5,715 PointsIts 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
Antoine Guenet
2,843 Points%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.
Mohammad Baqer
4,260 PointsCan you be more specific? There is many ways you can use that, did you mean String Format Specifiers in general or just %s ?
Sahil Gangele
6,851 Pointsnot string format specifiers, just %s.
specifically in this piece of code
char sahil[ ] = "sahil"; letter = &sahil[2]; printf("%c is in %s\n",*letter, sahil);
Sahil Gangele
6,851 PointsNow, 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
Mohd Maqbool Alam
Full Stack JavaScript Techdegree Student 5,715 PointsThat's is array string is a chunks of character bind together in array.using array we declare how much character we want Suppose you want store your name so you need 14 characters last one for /0
Varuzhan Darbinyan
8,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