Like Tree1Likes

Sprintf masks Doubt(s)

This is a discussion on Sprintf masks Doubt(s) within the C Programming forums, part of the General Programming Boards category; Originally Posted by iMalc Can you stop feeding conslusions of his own poor experimentation back at him as though they ...

  1. #16
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by iMalc View Post
    Can you stop feeding conslusions of his own poor experimentation back at him as though they were answers? It irks me almost as much it must irk him.
    He's stupidly listening to your interpretation of his results instead of actually reading the documentation, and is incorrectly concluding that Visual Studio is broken, which it is not.
    Of course VS is broken... but this probably isn't one of the ways...

    The point was that there are indeed differences in behaviour between libraries. Supposedly C-99 standardizes much of this, but Microsoft is not adhering to that standard as it would break a whole lot of Windows code if they did.

    I already told him to try his experiment again changing his 2 character field widths to 8... where he would clearly see what happens... But he perists, probably without even trying it.

    Now, when he is told in a slightly comedic way that we don't know why some things are different... because we were not privy to the library programmer's decision making... he blows a gasket, with you right behind him.

    I'm sorry... but really guys... this is just a little over the top!

  2. #17
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    1 - I read the documentation before i post this question and i read it again when you said to. specifically on the width it says:

    (number) Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger.
    * The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

    right before it, on "flags" it says:

    0 - Left-pads the number with zeroes (0) instead of spaces, where padding is specified (see width sub-specifier).

    So.. back to my question, with or without 0 i got the same results.... The number is padded with 0s.

    So, i asked, why? And i don't want to know "why this compiler do this wat and that one do taht way", i just want to know if is only this difference that makes it the way it is! Simple like that, i dont want to take anyone's time..

    And by the way CommontTater, i'm not hot fuse but answer questions with "telepatic" ? Come on, respect.

  3. #18
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    I said that i tried it, and get answers like yours is over the top...

  4. #19
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    It doesn't get much more complicated than what you've been told.

    The idea of integer precision is pretty silly so the C library makes out integer precision to be "pad with zeros". Integer width is different: basically, that measure counts the number of columns that an integer should take up, and it works like precision does, except instead of padding with zeros, it pads with white space. When the precision or width is smaller than the digit length of the number, no padding occurs.

    You can use them in combination for formatted output. Use width to make tabular columns, and use precision to pad zeros to numbers. It's pretty easy to play with.

    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    
    int main()
    {
       int i, prec, w;
       srand(time(NULL));
       for ( i = 0; i < 120; i += 5 ) {
          prec = rand()%12;
          w = rand()%20;
          printf("%*.*d\n", w, prec, i);
       }
       return 0;
    }
    
    /* output - yours may differ slightly:
                    0
    0000005
    000000010
          15
             20
        00000025
       0030
    00000000035
            0000040
                   045
     0000000050
                    55
    0000060
    000065
           0000000070
          000000075
    80
     85
     0090
               95
             100
            000000105
         00000110
     000115
    */
    I apologize if I've been short with you before. I was trying to be helpful, but hopefully this is more so.

    edit - And, if you make a width like this "%05d" for example; like the documentation says, it will pad the width with zero instead of space. So that is why the zero flag is supported.
    Last edited by whiteflags; 11-26-2011 at 03:11 PM.

  5. #20
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    I understand... so 0 is used to specify the pad when you put just the width, right? like "%03d". When you use width.precision, the precision of the integer will be always padded with 0s...

    So, there is no need for "%02.2d" because .2 will guarantee that the result is shorter than the precision it will be padded with 0s. Am i right?


    Code:
    	sprintf(msg, "%05d",10);    //[00010]
    	sprintf(msg, "%5d",10);     //[   10]
    	sprintf(msg, "%05.5d",10);  //[00010]  No reason for 0,  because .5 will pad with 0's, right?
    	sprintf(msg, "%5.5d",10);   //[00010]
    Thank you for your answer!

  6. #21
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    You understand well. You're welcome.

  7. #22
    Registered User
    Join Date
    Nov 2011
    Posts
    31
    "%2d" means print the integer value 2 spaces to the left of the last character printed in the string, ".2" means to 2 decimal places. "Decimal place" does not mean "zeroes".

  8. #23
    Registered User
    Join Date
    Nov 2011
    Posts
    31
    Mmmmmmm, should've read more thread. *prepares self for condescension

  9. #24
    Registered User
    Join Date
    Nov 2011
    Posts
    31
    Look at this. See how many things I'm wrong about? SEE?!

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-12-2010, 07:02 PM
  2. Doubt in using % symbol under sprintf
    By cbalu in forum Linux Programming
    Replies: 2
    Last Post: 09-11-2009, 04:37 AM
  3. LAN IP masks?
    By whackaxe in forum Networking/Device Communication
    Replies: 7
    Last Post: 07-12-2004, 04:30 AM
  4. Input Masks - PASSWORDS
    By destiny_believe in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2004, 01:03 PM
  5. Map masks?
    By Kavity in forum Game Programming
    Replies: 8
    Last Post: 11-11-2001, 02:00 AM

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