Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Problems on String
  • Practice String
  • MCQs on String
  • Tutorial on String
  • String Operations
  • Sort String
  • Substring & Subsequence
  • Iterate String
  • Reverse String
  • Rotate String
  • String Concatenation
  • Compare Strings
  • KMP Algorithm
  • Boyer-Moore Algorithm
  • Rabin-Karp Algorithm
  • Z Algorithm
  • String Guide for CP
Open In App
Next Article:
C program to reverse the content of the file and print it
Next article icon

C Program to Print the Length of a String Using %n Format Specifier

Last Updated : 15 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In C, strings are arrays of characters terminated by a null character (‘\0’) and the length of a string is the number of characters before this null character. In this article, we will learn how to find the length of the string using %n format specifier.

The %n is a special format specifier for printf that stores the number of characters printed so far into an integer variable, allowing you to track the string’s length without printing anything. So, if we print only the string using printf, we can find the length of the string using %n format specifier.

C
#include <stdio.h>  int main(){     int len = 0;      // Print the string and use %n to store number   	// of characters printed in 'len'     printf("Geeks%n", &len);    	// print length     printf(" = %d", len);      return 0; } 

Output
Geeks = 5

Explanation: In this program, the string “Geeks” is printed using printf and %n specifier is inserted at the end. It stores the number of characters printed by printf inside variable x which in this case is 5.




Next Article
C program to reverse the content of the file and print it

V

vishwajeet_Kamble
Improve
Article Tags :
  • C Programs
  • C/C++ Puzzles
  • DSA
  • Strings
Practice Tags :
  • Strings

Similar Reads

  • C Program to Print the Length of a String using Pointers
    C language provides a built-in function strlen() but in this article, we will learn how to find and print the length of the string using pointers in C. The easiest method to find the length of a string using pointers is by calculating difference between the pointers to the first and last character o
    2 min read
  • C Program to Find the Length of a String
    The length of a string is the number of characters in it without including the null character (‘\0’). In this article, we will learn how to find the length of a string in C. The easiest way to find the string length is by using strlen() function from the C strings library. Let's take a look at an ex
    2 min read
  • Lex program to find the Length of a String
    Problem: Write a Lex program to find the Length of a String Explanation: FLEX (Fast Lexical Analyzer Generator) is a computer program that generates lexical analyzers and was written by Mike Lesk and Eric Schmidt. Lex reads an input stream specifying the lexical analyzer and outputs source code impl
    1 min read
  • C program to reverse the content of the file and print it
    Given a text file in a directory, the task is to print the file content backward i.e., the last line should get printed first, 2nd last line should be printed second, and so on.Examples: Input: file1.txt has: Welcome to GeeksforGeeks Output: GeeksforGeeks to WelcomeGeeksforGeeks Input: file1.txt has
    3 min read
  • C Program to Compare Two Strings Using Pointers
    In C, two strings are generally compared character by character in lexicographical order (alphabetical order). In this article, we will learn how to compare two strings using pointers. To compare two strings using pointers, increment the pointers to traverse through each character of the strings whi
    2 min read
  • C Program to Print the First Letter of Each Word
    In a string that contains multiple words, each word is separated by a whitespace. In this article, we will learn how to print the first letter of each word using a C program. The simplest method to print the first letter of each word is by using a loop. Let’s take a look at an example: [GFGTABS] C #
    3 min read
  • C program to find the frequency of characters in a string
    Given a string S containing lowercase English characters, the task is to find the frequency of all the characters in the string. ExamplesInput: S="geeksforgeeks" Output: e - 4 f - 1 g - 2 k - 2 o - 1 r - 1 s - 2 Input: S="gfg" Output: f - 1 g - 2Approach Follow the steps to solve the problem: Initia
    2 min read
  • C Program to Find the Size of int, float, double and char
    Write a C program to find the size of the data types: int, float, double, and char in bytes and print it on the output screen. Examples Input: charOutput: Size of char: 1 byte Input: intOutput:Size of int: 4 bytes Different Methods to Find the Size of int, float, double and char in CWe can find the
    4 min read
  • Length of a String Without Using strlen() Function in C
    The length of a string is the number of characters in it without including the null character. C language provides the strlen() function to find the lenght of the string but in this article, we will learn how to find length of a string without using the strlen() function. The most straightforward me
    3 min read
  • How to Convert an Integer to a String in C?
    Write a C program to convert the given integer value to string. Examples Input: 1234Output: "1234"Explanation: The integer 1234 is converted to the string "1234". Input: -567Output: "-567"Explanation: The integer -567 is converted to the string "-567". Different Methods to Convert an Integer to a St
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences