Skip to content
Related Articles

Related Articles

Improve Article
C program to print the length of a String using %n format specifier
  • Last Updated : 11 Oct, 2019

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


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.

Below is the implementation of the above approach:

// C program to print
// the length of a String
// using %n format specifier
#include <stdio.h>
// Driver code
int main()
    char str[100] = "Geeks for Geeks";
    int len = 0;
    printf("%s%n", str, &len);
    printf(" = %d", len);
    return 0;
Geeks for Geeks = 15

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer DSA Live Classes 

My Personal Notes arrow_drop_up
Recommended Articles
Page :