Microsoft C 5.1 question

Scott "The Pseudo-Hacker" Neugroschl abcscnge at csuna.UUCP
Sun Jan 22 05:12:41 AEST 1989


In article <6951 at june.cs.washington.edu> ka at june.cs.washington.edu (Kenneth Almquist) writes:
]]    if(minute < 10)
]]        sprintf(min_str, "0%d", minute);
]]    else
]]        sprintf(min_str, "%d", minute);
]]
]]    if(second < 10)
]]        sprintf(sec_str, "0%d", second);
]]    else
]]        sprintf(sec_str, "%d", second);
]]
]]    printf("%2d:%s:%s", hour, min_str, sec_str);
]
]This can be shortened to
]
]     printf("%2d:%.2d:%.2d", hour, minute, second);
]
]				Kenneth Almquist


How about
     printf("%2d:%02d:%02d", hour, minute, second);

K&R (page 146) specify that the 0 is the pad character to pad out the
2 digit integer in this case.  Seems to solve all the problems. 


-- 
Scott "The Pseudo-Hacker" Neugroschl
UUCP:  ...!sm.unisys.com!csun!csuna!abcscnge
-- "Beat me, whip me, make me code in Ada"
-- Disclaimers?  We don't need no stinking disclaimers!!!



More information about the Comp.lang.c mailing list