Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: The Use Of %D In C Programming

1. ## The Use Of %D In C Programming

Hey Everybody,

I am new to all this and learning C from 'The C Programming Language' by Brian Kernighan which I picked up on Amazon. I am really enjoying it and often find myself loosing track of time.

I have had some experience with programming but nothing too serious, only the odd experiment with PHP to create some simple websites.

I wondered if someone could explain this for me, a beginner to understand.

Code:
```#include <stdio.h>
int power(int m, int n);
/* test power function */
main()
{
int i;
for (i = 0; i < 10; ++i)
printf("%d %d %d\n", i, power(2,i), power(-3,i));
return 0;
}

/* power: raise base to n-th power; n >= 0 */
int power(int base, int n)
{
int i, p;
p = 1;
for (i = 1; i <= n; ++i)
p = p * base;
return p;
}```
This is a extract from the book, could someone please explain this following line of code:
Code:
`printf("%d %d %d\n", i, power(2,i), power(-3,i));`
I understand that I am using the 'printf()' function to display the result of 'power()'. However, I don't under the stand the use of '%d' and how this effects the result. Is this like introducing integer results which 'printf()' can use within the function?

I have also seen variations of the above, such as '%6d', '%6.2' etc..

I tried a quick google search, but didn't really fully understand what it was they were trying to describe.

Ashley

• `printf("%d %d %d\n", i, power(2,i), power(-3,i));`

Basically is the evaluation output of the following values:
i 2^i (-3)^i
From when you input i = 0 to when i = 9. If you do not know what powers are, you probably should not play with it until one of your high school teachers (or elementary/middle school teacher) teaches you it.

For example:
0 1 1 // because x^0 is 1
1 2 -3 // because x^1 is itself
.
.
.
8 256 6561 // 2^8 is 256, (-3)^8 is 6561
9 512 -19683 // 2^9 is 512, (-3)^9 is -19683

And I BELIEVE this is what `%10.5d` does (each - is really a space):
`----------%d-----`

Basically, `%10d` means add 10 spaces before the digit. `%.5d` means add 5 spaces after the digit. Combine it together and the two addition of spaces occur.

• Originally Posted by Apothem
`printf("%d %d %d\n", i, power(2,i), power(-3,i));`

Basically is the evaluation output of the following values:
i 2^i (-3)^i
From when you input i = 0 to when i = 9. If you do not know what powers are, you probably should not play with it until one of your high school teachers (or elementary/middle school teacher) teaches you it.

For example:
0 1 1 // because x^0 is 1
1 2 -3 // because x^1 is itself
.
.
.
8 256 6561 // 2^8 is 256, (-3)^8 is 6561
9 512 -19683 // 2^9 is 512, (-3)^9 is -19683

And I BELIEVE this is what `%10.5d` does (each - is really a space):
`----------%d-----`

Basically, `%10d` means add 10 spaces before the digit. `%.5d` means add 5 spaces after the digit. Combine it together and the two addition of spaces occur.
I don't think the OP is looking for an explanation of exponents but the purpose of the %d in the printf string literal.

The %d is a format specification which is filled in by the other arguments passed into the printf function. %d means the argument is a decimal number. And as Apothem mentioned at the end of their post you can apply modifiers to it so it formats the number in a specific way.

You use them to insert the values of variables into the output.

There are many different types you can use beyond the %D. See: http://msdn.microsoft.com/en-us/library/56e442dc.aspx and http://msdn.microsoft.com/en-us/library/hf4y5e3w.aspx

• Thanks, that has made it really much clearer.

That looks like a useful resource. I will take a proper look later but it appears its like some sort of data formatting?

If that's the case is the syntax something like the following:

Code:
`printf("Type of Data Format" Data);`
If there was multiple parameters would this work left to right, if that makes sense to you?

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•