user6883777 user6883777 - 3 months ago 13
C Question

String length finding program. What does the line - printf ("%s %n", str, &l); mean in the following program?

#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.

Answer Source

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