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; Hi! i dont know what is the difference between the 2 masks (the result on visual c++ 2010 is the ...

  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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    Try numbers other than ten and you might just find out yourself.
    CommonTater likes this.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    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
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    So, now you have your answer... It doesn't matter (in VS2010).

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    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"

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

    Why it doesn't matter in VS 2010?

    Why it doesn't matter in VS 2010? in GCC/GPP i have to put the 0?

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by RLeePlusPlus View Post
    Why it doesn't matter in VS 2010? in GCC/GPP i have to put the 0?
    Because that's the way the libraries are written.

    Really... I'm no more telepathic than you are... I have no idea why programmer A decides one thing and programmer B decides another.
    Just accept these things as they are, use them, and get on with it.

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

    ok...

    man, you really S U CK S... telepatic? Its not about to accept and use! Its about WHY IS DIFFERENT or IS REALLY DIFERENT? What a stupid reply... leave this for who want to discuss. You should be banished for answer questions like this.

    and don't reply this, put your idead in you A SS

  14. #14
    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.

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    Quote Originally Posted by CommonTater View Post
    Because that's the way the libraries are written.

    Really... I'm no more telepathic than you are... I have no idea why programmer A decides one thing and programmer B decides another.
    Just accept these things as they are, use them, and get on with it.
    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.

    RLeePlusPlus they do give different results, you're just not reading the documentation to see what values would make it give different results. I've already told you one answer and yes it will give a different result in Visual Studio.
    Now stop wasting everyone's time and actually read the link posted. If you don't understand part of what it says then ask a specific question about that part.
    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"

Page 1 of 2 12 LastLast
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