I don't know how this code is working?
#include<stdio.h>
int main()
{
char *s = "PRO coder";
int n = 7;
printf("%.*s", n, s);
return 0;
}
The result I am getting is "PRO cod"
I don't know how this code is working?
#include<stdio.h>
int main()
{
char *s = "PRO coder";
int n = 7;
printf("%.*s", n, s);
return 0;
}
The result I am getting is "PRO cod"
printf
format string %.*s
takes two arguments, *
for the number and finally s
for a string, so it prints the first 7 characters of the string pointer s
. In general, anytime there is a number, you may use *
instead to read it as an argument.
%7s
would print seven characters or more if the string were longer, while %.7s
would print up to seven characters. So sometimes one would write "%*.*s", 7, 7, s
to print exactly 7 characters.