Find the answer to your Linux question:
Results 1 to 5 of 5
hi everbody when i use this code in unix/windows and linux it apear in different output: ... char* ch="12"; printf("%05s",ch); ... in windows and unix it.ll be "00012" but in ...
  1. #1
    Just Joined!
    Join Date
    Nov 2008
    Posts
    1

    Unhappy Printf

    hi everbody

    when i use this code in unix/windows and linux it apear in different output:
    ...
    char* ch="12";
    printf("%05s",ch);
    ...
    in windows and unix it.ll be "00012" but in linux " 12".
    if there any solution for linux to print "00012" in output?

  2. #2
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    man 3 printf

  3. #3
    Linux Newbie danielsmw's Avatar
    Join Date
    Nov 2006
    Location
    Clemson, SC / Charleston, SC
    Posts
    110
    That is strange. In my Unix and OS X shells, it runs like you'd expect, but in Gentoo it uses blank padding instead of zero-padding. Check the man page, but it doesn't help too much; it clearly says that the value should be zero padded when you use %0, unless you also use the '-' flag (which you aren't).

    Hmm....
    Registered Linux User: #479567
    Asking a question? Read this page first.
    Now... sudo make me a sandwich.
    ratiocinativeroot.blogspot.com

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Check the man page, but it doesn't help too much; it clearly says that the value should be zero padded when you use %0, unless you also use the '-' flag (which you aren't).
    Um, not so much. My man page says of the zero flag:
    Code:
           0      The value should be zero padded.  For d, i, o, u, x, X, a, A, e,
                  E,  f, F, g, and G conversions, the converted value is padded on
                  the left with zeros rather than blanks.  If the 0  and  -  flags
                  both  appear,  the  0  flag is ignored.  If a precision is given
                  with a numeric conversion (d, i, o, u, x, and X), the 0 flag  is
                  ignored.  For other conversions, the behavior is undefined.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  5. #5
    Linux Newbie danielsmw's Avatar
    Join Date
    Nov 2006
    Location
    Clemson, SC / Charleston, SC
    Posts
    110
    Ah! I skipped past the end after I checked that 's' wasn't in that list of conversion flags. Good call.
    Registered Linux User: #479567
    Asking a question? Read this page first.
    Now... sudo make me a sandwich.
    ratiocinativeroot.blogspot.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...