About the execution results of the sprintf function in different environments, the sprintf Function
Author: iamlaosong
Today we found a problem with the sprintf function. The execution results of the same statement in different environments are different. A time string must be six digits. If there are less than six digits, add 0. Use the following statement:
Strcpy (tt, "2345 ");
Printf ("= % s =", tt );
Sprintf (t1, "% 06 s", tt );
Printf ("= % s =", t1 );
Execution result of the preceding statement VC: = 2345 = 002345 =
Execution result of the same statement in Linux: = 2345 = 2345 =
0 is not supplemented, and space is supplemented. To solve this problem, the implementation of 0 supplement is as follows:
K = strlen (tt );
If (k <6)
{
Strcpy (t1, "000000 ");
Strncpy (t1 + 6-k, tt, k );
Printf ("= % s =", tt );
Strcpy (tt, t1 );
Printf ("= % s =", tt );
}