#include <stdio.h>
int main()
{
char str[100];
int l;
gets(str);
printf("%s %n",str,&l);
printf("%d",l-1);
return 0;
}
This program is for finding the length of a string.
#include <stdio.h>
int main()
{
char str[100];
int l;
gets(str);
printf("%s %n",str,&l);
printf("%d",l-1);
return 0;
}
This program is for finding the length of a string.
%s
says output the string str
and %n
says print nothing, rather store the number of characters written so far into the memory address of l
.
for reference on printf and all %
specifiers see here
USE OF %n format specifier in'C' :
Ques. What is %n in c & what did it do?
Ans. • %n is a special format specifier. • It loads the corresponding argument with a value equal to the number of characters that have been printed by printf() before the occurrence of %n.
Sample
#include<stdio.h>
int main()
{
int a;
printf("I am shivam %nsharma ", &a);
printf("%d", a);
return 0;
}
output: I am shivam sharma 12