C 代码不显示在输出零号中
C code doesn't show in the output zero number
当我编译以下源代码时,输出中的零显示不正确。为什么?我希望输出文本具有以下格式:
01 02 03 04
05 06 07 08
不是 1 2 3 4....
我该如何解决这个问题?
#include "stdio.h"
int main(int argc, char const *argv[])
{
int a[3][4] = {
{00, 01, 02, 03} , /* initializers for row indexed by 0 */
{04, 05, 06, 07} , /* initializers for row indexed by 1 */
{08, 09, 10, 11} /* initializers for row indexed by 2 */
};
for (int i = 0; i < 3; ++i)
{
for (int t = 0; t < 4; ++t)
{
printf("%d\t", a[i][t] );
}
printf("\n");
}
return 0;
}
0 在数字之前告诉编译器,这个数字是八进制的。您不能对数字执行此操作,只能对字符或 printf 中的正确格式执行此操作。实际上你的代码是不正确的,不应该编译,因为八进制中没有数字 8
和 9
。
查找填充为 0:
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.
并查找字段宽度:
The field width
An optional decimal digit string (with nonzero first digit) specifying
a minimum field width. If the converted value has fewer characters
than the field width, it will be padded with spaces on the left (or
right, if the left-adjustment flag has been given). Instead of a
decimal digit string one may write "*" or "*m$" (for some decimal
integer m) to specify that the field width is given in the next
argument, or in the m-th argument, respectively, which must be of type
int. A negative field width is taken as a '-' flag followed by a
positive field width. In no case does a nonexistent or small field
width cause truncation of a field; if the result of a conversion is
wider than the field width, the field is expanded to contain the
conversion result.
解决方案使用:
printf("%02d\b", a[i][t]);
并从文字 (01, 02...) 中删除 0,否则它们将被视为八进制数。
当我编译以下源代码时,输出中的零显示不正确。为什么?我希望输出文本具有以下格式:
01 02 03 04
05 06 07 08
不是 1 2 3 4....
我该如何解决这个问题?
#include "stdio.h"
int main(int argc, char const *argv[])
{
int a[3][4] = {
{00, 01, 02, 03} , /* initializers for row indexed by 0 */
{04, 05, 06, 07} , /* initializers for row indexed by 1 */
{08, 09, 10, 11} /* initializers for row indexed by 2 */
};
for (int i = 0; i < 3; ++i)
{
for (int t = 0; t < 4; ++t)
{
printf("%d\t", a[i][t] );
}
printf("\n");
}
return 0;
}
0 在数字之前告诉编译器,这个数字是八进制的。您不能对数字执行此操作,只能对字符或 printf 中的正确格式执行此操作。实际上你的代码是不正确的,不应该编译,因为八进制中没有数字 8
和 9
。
查找填充为 0:
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.
并查找字段宽度:
The field width
An optional decimal digit string (with nonzero first digit) specifying a minimum field width. If the converted value has fewer characters than the field width, it will be padded with spaces on the left (or right, if the left-adjustment flag has been given). Instead of a decimal digit string one may write "*" or "*m$" (for some decimal integer m) to specify that the field width is given in the next argument, or in the m-th argument, respectively, which must be of type int. A negative field width is taken as a '-' flag followed by a positive field width. In no case does a nonexistent or small field width cause truncation of a field; if the result of a conversion is wider than the field width, the field is expanded to contain the conversion result.
解决方案使用:
printf("%02d\b", a[i][t]);
并从文字 (01, 02...) 中删除 0,否则它们将被视为八进制数。