Most of these answers explain what %n does (which is to print nothing and to write the number of characters printed thus far to an int variable), but so far no one has really given an example of what use it has. Here is one:

int n;
printf("%s: %nFoo\n", "hello", &n);
printf("%*sBar\n", n, "");

will print:

hello: Foo
       Bar

with Foo and Bar aligned. (It's trivial to do that without using %n for this particular example, and in general one always could break up that first printf call:

int n = printf("%s: ", "hello");
printf("Foo\n");
printf("%*sBar\n", n, "");

Whether the slightly added convenience is worth using something esoteric like %n (and possibly introducing errors) is open to debate.)

Answer from jamesdlin on Stack Overflow
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › standard › base-types › standard-numeric-format-strings
Standard numeric format strings - .NET | Microsoft Learn
The precision specifier determines the number of digits in the result string. To pad a result string with leading or trailing spaces, use the composite formatting feature and define a width component in the format item.
🌐
C# Corner
c-sharpcorner.com › UploadFile › c25b6d › numerical-format-specifier-in-C-Sharp
Numeric ("N") Format Specifier In C#
November 20, 2023 - The numeric format specifier "N" converts a number to a string format as "-d,ddd,ddd.ddd…" where the "-" sign denotes the negative number symbol, "," denotes a separator between these groups, "d" denotes any number (0-9), and "." represents ...
🌐
Oracle
docs.oracle.com › javase › tutorial › java › data › numberformat.html
Formatting Numeric Print Output (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)
The %d specifies that the single variable is a decimal integer. The %n is a platform-independent newline character. The output is: ... The printf and format methods are overloaded. Each has a version with the following syntax: public PrintStream format(Locale l, String format, Object...
🌐
W3Schools
w3schools.com › python › ref_string_format.asp
Python String format() Method
Remove List Duplicates Reverse a String Add Two Numbers · Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Training ... The format() method formats the specified value(s) and insert them inside the string's placeholder.
Find elsewhere
🌐
Nevron
helpsharepointvision.nevron.com › Numeric_Format_Strings.html
Numeric Format Strings
A standard numeric format string takes the form Axx, where A is an alphabetic character called the format specifier, and xx is an optional integer called the precision specifier. The precision specifier ranges from 0 to 99 and affects the number of digits in the result.
🌐
Python
docs.python.org › 3 › library › string.html
Common string operations — Python 3.14.6 documentation
In particular, string.printable is not printable in the POSIX sense (see LC_CTYPE). ... A string containing all ASCII characters that are considered whitespace. This includes the characters space, tab, linefeed, return, formfeed, and vertical tab.
🌐
Reddit
reddit.com › r/learnjava › %n \n difference?
r/learnjava on Reddit: %n \n difference?
September 12, 2020 -

In the context of strings:

%n and \n both make newline characters. I have made some test code to see if there is any difference between using %n and \n other than that %n can only be used when formatting strings, and I don't see anything:

public class Practice {
	
   public static String stuff () {
      String x = "blah";
		
      return "blah blah blah \n" +                  //Starts us off
             String.format("%s %s %s%n", x, x, x) + //Using %n to do newline
             String.format("%s %s %s\n", x, x, x) + // Using \n to do newline
             "blah blah blah \n";                   //Checks if newline was made
   }
	
   public static void main(String args[]) {
		System.out.print(stuff());
			
   }

The comments that I made inside the stuff() method should explain how the program works. Is there really any difference between %n and \n?

🌐
Apple Developer
developer.apple.com › library › archive › documentation › Cocoa › Conceptual › Strings › Articles › formatSpecifiers.html
String Format Specifiers
February 11, 2014 - Note that you can also use the “n$” positional specifiers such as %1$@ %2$s. For more details, see the IEEE printf specification. You can also use these format specifiers with the NSLog function.
🌐
GitHub
github.com › dotnet › docs › blob › main › docs › standard › base-types › standard-numeric-format-strings.md
docs/docs/standard/base-types/standard-numeric-format-strings.md at main · dotnet/docs
Supported by: Integral types only (.NET 8+). Precision specifier: Number of digits in the result string. More information: The Binary ("B") Format Specifier. 42 ("B") -> 101010 255 ("b16") -> 0000000011111111 ... Result: A currency value. Supported by: All numeric types.
Author   dotnet
🌐
Nim
nim-lang.org › docs › strformat.html
std/strformat
StandardFormatSpecifier = object fill*, align*: char ## Desired fill and alignment. sign*: char ## Desired sign. alternateForm*: bool ## Whether to prefix binary, octal and hex numbers ## with `0b`, `0o`, `0x`. padWithZero*: bool ## Whether to pad with zeros rather than spaces. minimumWidth*, precision*: int ## Desired minimum width and precision. typ*: char ## Type like 'f', 'g' or 'd'. endPosition*: int ## End position in the format specifier after ## `parseStandardFormatSpecifier` returned. Type that describes "standard format specifiers". Source Edit · proc alignString(s: string; minimumWidth: int; align = '\x00'; fill = ' '): string {. ...raises: [], tags: [], forbids: [].}
🌐
C# Corner
c-sharpcorner.com › UploadFile › mahesh › format-string-in-C-Sharp
C# String Formatting Explained With Practical Examples
January 31, 2026 - As you can see from the above code example, {0} is at the last potion in the string and the second argument is a DateTime.Now. In this case, string portion {0} will be replaced by the value of DateTime.Now in the string. Figure 1 is the result of above code in Listing 1. ... We can insert multiple objects or expressions in a string to give it a desired format.
🌐
BreakInSecurity
axcheron.github.io › exploit-101-format-strings
Exploit 101 - Format Strings - BreakInSecurity
April 22, 2018 - This string is then, by default, printed on the standard output. For example, the C library function int printf(const char *format, ...) sends formatted output to stdout: printf("Color %s, number1 %d, number2 d, hex %#x, float %5.2f, unsigned value %u.\n", "red", 123456, 89, 255, 3.14159, 250); // It will print the following line Color red, number1 123456, number2 00089, hex 0xff, float 3.14, unsigned value 250.
🌐
IBM
ibm.com › docs › en › netezza
string.format (formatstring, ···) - IBM Documentation
January 8, 2026 - The format string follows the same rules as the printf family of standard C functions. The only differences are that the options/modifiers *, l, L, n, p, and h are not supported and that there is an extra option, q.
🌐
OWASP Foundation
owasp.org › www-community › attacks › Format_string_attack
Format string attack | OWASP Foundation
In this case, if a Format String parameter, like %x, is inserted into the posted data, the string is parsed by the Format Function, and the conversion specified in the parameters is executed. However, the Format Function is expecting more arguments as input, and if these arguments are not supplied, the function could read or write the stack.