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 Control Flow - For Loops

Zacahry Mar
Zacahry Mar
156 Points

When to use %d and when to use %c

I'm currently on section Control Flow- For Loops For example:

int main () { int a =11; int b = 9; int minimum; char min_value;

           if (a<b) {
               minimum = a; 
               min_value = 'a';

           } else {
                 minimum = b;
                 min_value = 'b';
           }

              printf(%c %d is the minimum \n", min_value, minimum); 
              return 0;
          }

I'm still confused as to when I use %c and when do i use %d, and in which order i use them in my printf statement....

I noticed if i switched the order of %c and %d i get a completely different value....

Could someone please explain it to me?

%d is used for integers...but what is %c and what does it correlate too

I've been having a problem understanding what values correlate to what exactly...

For instance how to read ? printf(%c %d is the minimum \n", min_value, minimum);

3 Answers

Stone Preston
Stone Preston
42,016 Points

%d is used for ints and %c is used for chars. so if you want to print an int, use %d, if you want to print a char, use %c

Here is a list of the C format specifiers and what they correspond to.

the order you use them in depends on the order in which you want to print the values. if you want to print your char variable first and then after that print your int variable, use %c then %d. if you want your int to be first use %d then %c.

Remember that the values of the variables are inserted into your string in place of the format specifiers. If I had a char variable and an int variable and printed them like so:

char someChar = 'x';
int someInt = 2;
printf("im going to print this char: %c and then this int: %d", someChar, someInt);

the output of that would be "Im going to print this char: x and then this int 2"

"I noticed if i switched the order of %c and %d i get a completely different value...."

thats because char values can be used as integers. when a char is used as an integer (say if you use a %d format specifier but pass it a char variable in printf) it just returns its ascii code.

Pierre Thalamy
Pierre Thalamy
4,494 Points

Hi Zacahry, As mentioned on this useful page, %c is used to print an ASCII character, where as %d is used to display a signed integer.

Here is how to read the example you gave, considering that a < b:

printf(%c %d is the minimum \n", min_value, minimum);
/* Prints: a 11 is the minimum */

You can see that the variable, minimum is an int, whereas min_value is a char that can be used to store any number no more than one byte long, such as an ASCII character. Note that an ASCII character is in fact a number from 0 to 255, try printing printf("%d", 'a'); to see what happens, and refer to this table to know what output value to expect.

I hope that was clear! Let me know if you have any question.

Zacahry Mar
Zacahry Mar
156 Points

thanks both of you