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) Fundamentals of C Variables

Challenge Task 2 of 2 Variable Assignment

Isn't this correct, if not please explain how and why and include the correct way.

float radius=14.5; printf("%f A ball with a radius of 14.5 inches.\n"radius);

1 Answer

Pierre Thalamy
Pierre Thalamy
4,494 Points

It seems that you need some explanations on how the printf function works.

Here is the correct solution to the exercise:

float radius = 14.5;
printf("A ball with a radius of %f inches.\n", radius);

As you can see, 14.5 is not explicitly specified in the string, but %f appears instead. It will be replaced by the value of the float variable positioned after the comma.

Below is another example on how to use the printf function.

int a = 1;
float b = 2.0;
char c = 'T';

printf("a: %d, b: %f, c: %c\n", a, b, c); // prints "a: 1, b: 2.0, c: T
printf("c: %c, a: %d, b: %f\n", c, a, b); // prints "c: T, a: 1, b: 2.0

Feel free to ask if you have any question.

Thanks so much but I have another question, so we use the %f and such to replace the variable then name it after?

Pierre Thalamy
Pierre Thalamy
4,494 Points

Yes, you use format specifiers such as %f for a float variable, %d for an int, %c for a char, etc... You can refer to this link to find other format specifiers.

Basically, the variable arguments have to appear in the same order as their related format specifiers appear in the string. This is why I provided you with my second example.

Is that clear?