Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

    Thanks in Advance,

    Ashley

  • #2
    Regular Coder Apothem's Avatar
    Join Date
    Mar 2008
    Posts
    380
    Thanks
    36
    Thanked 25 Times in 25 Posts
    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.
    Last edited by Apothem; 12-31-2011 at 07:20 PM.

  • #3
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Quote Originally Posted by Apothem View Post
    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
    OracleGuy

  • #4
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    •