Microsoft C 5.1 question

Kenneth Almquist ka at june.cs.washington.edu
Mon Jan 16 22:46:45 AEST 1989


]    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);

A "." introduces a precision, which specifies the minimum number of
digits to print.
				Kenneth Almquist



More information about the Comp.lang.c mailing list