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

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
krishankantray
  • 73
  • 1
  • 2
  • 8
  • You need to go through the basics of the language. That line specifically, it is to tell the system what data to display and in what format. – Khalil Khalaf Apr 16 '17 at 04:55
  • BTW, the C++ header file should be ``. – Thomas Matthews Apr 16 '17 at 05:02
  • 2
    This looks like C code. C and C++ are different languages, don't spam tags. If you use a C++ compiler, change the tag, but don't add both unless **both** languages are definitively involved. Said that: What diod you find out? What is the problem with the `printf` documentation? Finally: **never ever** use `gets`! It has been removed from the standard 6 years ago and was strongly deprecated since 1999 as a severe security risk.. – too honest for this site Apr 16 '17 at 05:05
  • 1
    the key here would be to understand what the %n format specifier does. If only there were some way to look that up – bruceg Apr 16 '17 at 05:39
  • 1
    Note that [`gets()` is too dangerous to be used — ever!](http://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used) – Jonathan Leffler Apr 16 '17 at 05:49

2 Answers2

4

%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

Matthew Ciaramitaro
  • 1,184
  • 1
  • 13
  • 27
2

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

krishankantray
  • 73
  • 1
  • 2
  • 8
Shivam Sharma
  • 1,015
  • 11
  • 19