我正在做书的第三章学习C艰苦的方式,由泽德肖。我正在探索printf的不同字符串格式选项。我遇到了在“%#”符号后面放置的以下标志:
该值应转换为“备用形式”。对于o转换,输出字符串的第一个字符为零(如果不是0,则以0作为前缀)。对于x和X转换,非零结果将字符串"0x“(或X转换的"0X”)加在其中。对于a,A,e,E,f,F,g和G变换,结果总是包含一个小数点,即使没有数字跟随它(通常,只有在数字跟随的情况下,这些转换的结果中才会出现小数点)。对于g和G转换,尾随零点不会像它们那样从结果中移除。对于其他转换,结果是未定义的。
“%#”标志似乎是针对printf语句的一种类型转换。但我不确定。有人知道这个%#标志在C中的printf函数中的实际使用情况是什么样的吗?我可以看到字符"A“的任何更改或转换,这是我在最后一次打印语句中使用的标志%#。
这是我的密码:
#include <math.h>
int main(int argc, char *argv[])
{
int age = 32; // age and height not initialized
int height = 182;
printf("I am %d cm tall.\n", age);
printf("I am %d years old.\n", height);
printf("I wanna go to \"Panama\"\n"); // scaping double quotes
printf("I am %2$d tall and %1$d years old\n", age, height); // how to use positional arguments in printf statement
// how to use positional arguments in printf statement
printf("I am using the number pi: %'.2f\n", 3.1415939);
// Testing the %# formatting
printf("Testing for A: %# \n", 'A');
return 0;
}我收到的输出:
I am 32 cm tall.
I am 182 years old.
I wanna go to "Panama"
I am 182 tall and 32 years old
I am using the number pi: 3.14
Testing for A: %# 发布于 2019-09-15 08:20:35
您提供了合法的#标志,但没有指定任何合法转换字符(idouxXsc等.)。在这种情况下,printf的行为是未定义的。
这里有几个(未经检验的)例子:
printf("%#x\n", 17);
printf("%#X\n", 17);
printf("%#f\n", 1.42);
printf("%#A\n", 1.42);参考:
man 3 printf检查系统上的man可以帮助您理解C标准没有定义的行为。https://stackoverflow.com/questions/57942250
复制相似问题