#include <stdio.h>
int main ()
{
int c;
printf ("the value of %nc :
", &c);
return 0;
}
输出:值为0
Per C 2018 7.21.6.1 8,对于转换说明符n
:
参数应为指向有符号整数的指针,到目前为止,通过对
fprintf
[或printf
]的调用,该整数已写入输出流中的字符数。因此,printf ("the value of %nc : ", &c);
的作用是将字符“ c的值”写入输出,并将字符数放入c
的“ c的值”中,即13。
printf ("the value of %nc :
", &c);
在语法上无效。要么写
printf ("the value of %nc : \n ", &c);
或写
printf ("the value of %nc : \n" "", &c);
在上面的调用中,函数printf
不会输出变量c
本身的值。您需要另外调用函数printf
才能输出变量c
的值。如果您想在一行中执行此操作,则可以按照下面的演示程序中的说明进行编写。
#include <stdio.h>
int main(void)
{
int c;
printf( "%d\n", ( printf ("the value of %nc : ", &c ), c ) );
return 0;
}
程序输出为
the value of c : 13
或者,如果要在输出的字符串文字中包含换行符,则可以通过以下方式重写调用pfprintf
printf( "%d\n", ( printf ("the value of %nc : \n ", &c ), c ) );
在这种情况下,程序输出将看起来像
the value of c : 13