#include <stdio.h>
int main ()
{
int c;
printf ("the value of %nc :
", &c);
return 0;
}
Output : the value of 0
Per C 2018 7.21.6.1 8, for the conversion specifier n
:
The argument shall be a pointer to signed integer into which is written the number of characters written to the output stream so far by this call to
fprintf
[orprintf
].…
Thus, the effect of printf ("the value of %nc : ", &c);
is to write the characters “the value of c : ” to output and to put the number of characters in “the value of ” in c
, which is 13.
For starters you may not split a string literal with an intermediate new line character. So this call
printf ("the value of %nc :
", &c);
is syntactically invalid. Either write
printf ("the value of %nc : \n ", &c);
or write
printf ("the value of %nc : \n"
"", &c);
In the call above the function printf
does not output the value of the variable c
itself. You need an additional call of the function printf
to output the value of the variable c
.
If you want to do this in one line then you can write as it is seen in the demonstrative program below.
#include <stdio.h>
int main(void)
{
int c;
printf( "%d\n", ( printf ("the value of %nc : ", &c ), c ) );
return 0;
}
The program output is
the value of c : 13
Or if you want to include a new line character in the outputted string literal you can rewrite the call pf printf
the following way
printf( "%d\n", ( printf ("the value of %nc : \n ", &c ), c ) );
In this case the program output will look like
the value of c :
13
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.