Here is the prototype for printf
:
int printf(const char *format, ...);
We can see that printf
returns an int
.
The documentation indicates that:
Upon successful return, these functions return the number of
characters printed (excluding the null byte used to end output to
strings).
You asked why the output is "String6". Well:
printf("String");
This first prints String
but does not print a newline character. Since String
is 6 characters, printf
returns 6, which you store in k
:
printf("%d",k);
This then prints 6
(on the same line).
Try running this program:
#include <stdio.h>
int main(void)
{
int bytes_printed = printf("%s\n", "String");
// 7 = 1 + 6
printf("printf returned: %d\n", bytes_printed);
return 0;
}
Output:
String
printf returned: 7
main()
should bemain(void)
)