Like Tree2Likes
  • 1 Post By anduril462
  • 1 Post By dheaven

The %d command in C programming

This is a discussion on The %d command in C programming within the C Programming forums, part of the General Programming Boards category; Hello, I am a relatively new programmer and I had a question about something my teacher went over regarding the ...

  1. #1
    Do you C what I C? jamesallen4u's Avatar
    Join Date
    Oct 2011
    Posts
    43

    The %d command in C programming

    Hello,

    I am a relatively new programmer and I had a question about something my teacher went over regarding the %d. Here is an example of the code we were doing:

    Code:
    #include <stdio.h>
    
    int main()
    {
        int k,m;
        
        system ("cls");
    
        k = 0;
      
        while ( k < 10 )
        {
             printf("Testing, testing 123");
             k = k + 1;
         }
    
        printf("\n\n\t\tThe value of k after the above while command is %2d"); 
    
    }
    I understand that 10 will be substituted in for %2d but I also tried executing with %d and got the same output. So what is the significance of the "2" in %2d and what does it do? Thanks a lot.
    Last edited by jamesallen4u; 10-01-2011 at 06:19 PM.

  2. #2
    Registered User
    Join Date
    Sep 2011
    Posts
    19
    Good question ! I use it for example when I do 5/2. I write %.2lf instead of only %lf. That way, it shows 2 digits after the dot. So it's 2.5 instead of 2.50000000.

    But in your case, I can't tell.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,295
    First things first:

    1. You said you would return an int from main. Do so. Zero is standard for success.
    2. system("cls") is OS specific. You should avoid it whenever possible.
    3. If you're actually going to call system(), you need to #include <stdlib.h>
    4. You never use m. Use it or get rid of it.



    Quote Originally Posted by jamesallen4u View Post
    I understand that 10 will be substituted in for %2d but I also tried executing with %d and got the same output.
    Actually, something will be substituted, but it might not be 10, since you didn't pass anything to printf to put where the %2d is. It's undefined behavior. You'll just get a garbage value. It may be 10, it may not, so you can't rely on it. You need to do printf("...%2d", k);

    So what is the significance of the "2" in %2d and what does it do? Thanks a lot.
    It's called the field width specifier. It's name is a big hint as to what it does. Read the relevant section in the documentation for printf: printf(3): formatted output conversion - Linux man page. Play with the value and see what happens if you make it something like %20d or %04d (yes, the I mean for the zero to be on the left of the 4).
    jamesallen4u likes this.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    23
    %2d means that if the number is smaller than two spaces (__), there would be an extra space left to the left.
    So, if you have

    Code:
    printf("%d", 1);
    //The output will be:
    //1
    But if you have

    Code:
    printf("%2d",1);
    //The output will be:
    // 1

    You can read more about printf and how strings are formatted:
    Foobar: Console I/O using stdio in C/C++
    Foobar: Formatted Strings in C
    jamesallen4u likes this.

  5. #5
    Do you C what I C? jamesallen4u's Avatar
    Join Date
    Oct 2011
    Posts
    43
    You never use m. Use it or get rid of it.
    I know I have an unused variable, I was just showing an example code.
    Actually, something will be substituted, but it might not be 10, since you didn't pass anything to printf to put where the - is. It's undefined behavior. You'll just get a garbage value. It may be 10, it may not, so you can't rely on it. You need to do printf("...-", k);
    And I know for sure that 10 will be substituted in for -, because, note that I have restricted k<10.

    // EDIT: I realize my mistake now and I have fixed it.

    It's called the field width specifier. It's name is a big hint as to what it does. Read the relevant section in the documentation for printf: printf(3): formatted output conversion - Linux man page. Play with the value and see what happens if you make it something like d or d (yes, the I mean for the zero to be on the left of the 4).
    Thanks for the document, I tried to read it and I didn't get much out of it. But I did try to play with the numbers and I realized that after plugging in values>3, the number just kept spacing out. Can you please try to explain what a "field width specifier" is in layman's terms so I can understand? Here were my outputs.

    For %3d:

    Name:  output 1.png
Views: 15407
Size:  13.1 KB

    For M:


    Name:  output 2.png
Views: 15341
Size:  12.9 KB


    For d


    Name:  output 3.png
Views: 15322
Size:  13.2 KB
    Last edited by jamesallen4u; 10-01-2011 at 03:53 PM.

  6. #6
    Do you C what I C? jamesallen4u's Avatar
    Join Date
    Oct 2011
    Posts
    43
    Thanks dheaven, I understand now.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jamesallen4u View Post
    Hello,

    I am a relatively new programmer and I had a question about something my teacher went over regarding the %d. Here is an example of the code we were doing:

    Code:
    #include <stdio.h>
    
    int main()
    {
        int k,m;
        
        system ("cls");
    
        k = 0;
      
        while ( k < 10 )
        {
             printf("Testing, testing 123");
             k = k + 1;
         }
    
        printf("\n\n\t\tThe value of k after the above while command is %2d"); 
    
    }
    I understand that 10 will be substituted in for %2d but I also tried executing with %d and got the same output.
    Actually with the source quoted above the output will be some random number... you have to TELL it what to print...
    Code:
        printf("\n\n\t\tThe value of k after the above while command is %2d", k  );
    See the difference between what you did and what I would do? Now the value of k will be substituted into the %2d ... although in your case it would be better to just use %d as a value less than 10 would have an extra space in it.

    So what is the significance of the "2" in %2d and what does it do? Thanks a lot.
    It says "use at least 2 spaces to display this value"...

  8. #8
    Do you C what I C? jamesallen4u's Avatar
    Join Date
    Oct 2011
    Posts
    43
    Actually with the source quoted above the output will be some random number... you have to TELL it what to print...
    Sorry about that, I actually forgot to type the ,k in the thread but I had it in my orig. source code. Also thanks for the explanation.
    Last edited by jamesallen4u; 10-01-2011 at 06:18 PM.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jamesallen4u View Post
    Sorry about that, I actually forgot to type the ,k in the thread but I had it in my orig. source code. I have edited that in my first post. Also thanks for the explanation.
    PLEASE... never edit your first post! You just made half of this conversation into pure gibberish and denied others the chance to learn from any of it.

  10. #10
    Do you C what I C? jamesallen4u's Avatar
    Join Date
    Oct 2011
    Posts
    43
    Ok, I fixed it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Command Promt showing in window programming - Compiler dependent?
    By random_cpp_user in forum C++ Programming
    Replies: 3
    Last Post: 10-23-2010, 04:24 AM
  2. Command line how to handle optional command.
    By ovid in forum C++ Programming
    Replies: 1
    Last Post: 03-07-2010, 10:41 PM
  3. C Programming: give command a 5 sec interval to run
    By kcirtap in forum C Programming
    Replies: 2
    Last Post: 12-06-2009, 03:33 AM
  4. Windows programming without command prompt
    By arckeda in forum C Programming
    Replies: 9
    Last Post: 06-04-2008, 06:09 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21