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 trial

iOS Objective-C Basics (Retired) Pointers and Memory Pointer Power

%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

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[2]; 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

That'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