Thread: Sprintf masks Doubt(s)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    14

    Sprintf masks Doubt(s)

    Hi!

    i dont know what is the difference between the 2 masks (the result
    on visual c++ 2010 is the same):

    Code:
    sprintf(msg, "Number %2.2d",10);
    sprintf(msg, "Number %02.2d",10);
    and this mask too (what 0* means?)

    Code:
    sprintf(msg, "Number %0*d",10);
    Thanks!!

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Try numbers other than ten and you might just find out yourself.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    Like what? i tried others too but with or without 0 (2.2 / 02.2) i got the same result. this is not a homework or something that i want you to do for me... If i'm asking here is because i really want to know the difference. So if you will say "try others to find yourself", please, don't answer my post, this is for people who want to learn and exchange experiences...

    anyway, thank you!

    And i still dont know the difference, so please, if anyone knows just tell me.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    So did you actually try 100,1000,10000,100000 and so on?

    If you want to learn, you do the typing, compiling, running the code, observing the results.
    Then (and only then), do you post your code and results, and ask for more clarification.

    We're not going to just write a whole test program just for you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You can find your answers here: printf - C++ Reference
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    14

    Yes i tried

    i have tried with this:

    Code:
        sprintf(msg, "Teste %02.2d",100);
        sprintf(msg, "Teste %02.2d",100000);
        sprintf(msg, "Teste %02.2d",100000000);
    
    
        sprintf(msg, "Teste %2.2d",100);
        sprintf(msg, "Teste %2.2d",100000);
        sprintf(msg, "Teste %2.2d",100000000);
    And still the same result for both.

    1 - And again, i dont want you to write a program for me, i just want to know whats the difference between 2 masks(o2.2 and 2.2).

    2 - I looked at the reference before and i understood that the 0 is used for left padding... Buf even if i dont use the 0 i still got the padding of 0 on the .2 number...

    i just want to know something like "ITS FOR PADDING" or "ITS FOR THIS OR THAT", dont a program write if you dont want to your "majesty" ^^

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by RLeePlusPlus View Post
    i have tried with this:

    Code:
        sprintf(msg, "Teste %02.2d",100);
        sprintf(msg, "Teste %02.2d",100000);
        sprintf(msg, "Teste %02.2d",100000000);
    
    
        sprintf(msg, "Teste %2.2d",100);
        sprintf(msg, "Teste %2.2d",100000);
        sprintf(msg, "Teste %2.2d",100000000);
    And still the same result for both.

    1 - And again, i dont want you to write a program for me, i just want to know whats the difference between 2 masks(o2.2 and 2.2).

    2 - I looked at the reference before and i understood that the 0 is used for left padding... Buf even if i dont use the 0 i still got the padding of 0 on the .2 number...

    i just want to know something like "ITS FOR PADDING" or "ITS FOR THIS OR THAT", dont a program write if you dont want to your "majesty" ^^
    Ummm... why don't you try changing all those 2s to 8s and see what happens?

    Really... rather than getting all ........ed off, do some real experimenting.

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    14

    still got the same results

    i still got the same results, with or without the '0' preceding the width... Just for information, i'm using VS2010 express

    thank you

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by RLeePlusPlus View Post
    i still got the same results, with or without the '0' preceding the width... Just for information, i'm using VS2010 express

    thank you
    FFS go and read the link I posted, specifically the documentation about the width specifier!

    Once you do that, it will be clear that you will see a difference using a single digit value, like 7.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    So, now you have your answer... It doesn't matter (in VS2010).

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Oh my... yet another short fuse who can't deal with reality.

    Look sport... Some compilers and libraries do things differently... only the authors of those libraries know why and for the most part they're not talking. What do you want me to tell you? The only available answer is that we don't know why... we are thus left to work with what is before us.

    You can talk about it till you're blue in the face... nothing is going to change that basic fact.

    Now... get that foul temper of yours under control, or get lost.

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You understand well. You're welcome.

  13. #13
    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".

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

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-12-2010, 08: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, 02:03 PM
  5. Map masks?
    By Kavity in forum Game Programming
    Replies: 8
    Last Post: 11-11-2001, 03:00 AM