Given string str. The task is to find the length of the string using %n format specifier 
Input: Geeks For Geeks Output: 15 Input: Geeks Output: 5
Approach: To find the length of string, we use special format specifier ā%nā in printf function. In C printf(), %n is a special format specifier which instead of printing something causes printf() to load the variable pointed by the corresponding argument with a value equal to the number of characters that have been printed by printf() before the occurrence of %n. 
C
// C program to print// the length of a String// using %n format specifierĀ
#include <stdio.h>Ā
// Driver codeint main(){Ā Ā Ā Ā char str[100] = "Geeks for Geeks";Ā Ā Ā Ā int len = 0;Ā
Ā Ā Ā Ā printf("%s%n", str, &len);Ā Ā Ā Ā printf(" = %d", len);Ā
Ā Ā Ā Ā return 0;} |
Geeks for Geeks = 15
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
