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) Functional Programming in C Scope

Why the video didn't mention %s? and what is it?

I know when using printf we have to use % followed by a letter. But we haven't seen anything like %s. Can someone explain it to me please?

from the previous videos I know that: use %d for the int use %f for the float use %c for the char

1 Answer

Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 11,075 Points

%s is what we refer to in the programming community as a String Format Specifier. The printf statement basically means "print formatted" and its value is a string (that contains these String Format Specifiers) followed by variables or values that relate to the String Format Specifier, in the same order as the specifiers themselves.

%s is the String Format Specifier -

Here is an example below

char [] name = "Dane";
printf("My name is %s", name)

//output -----> My name is Dane

Another example with numbers the String Format Specifier for integers is %d, notice how the variables after the string are in the same orders as the modifiers, and notice how we can input a value for a String Format Specifier variable instead of a variable itself?

int age = 19;
char [] name = "Dane";

printf("My name is %s and I am %d years old, I am part %s and %s, and am a proud high school graduate from class %d", name, age, "Jamaican", "Bahamian", 2013);

//Output ---> My name is Dane and I am 19 years old, I am part Jamaican and Bahamian, and am a proud high school graduate from class 2013

If you still don't understand or want to ask any questions feel free to ask, I or others will be happy to respond!

P.S Excuse the syntax, its been a while since I used Objective-C, if I am wrong with syntax, I will fix it.