Questions tagged [format-string]

A format string is a string that contains placeholders such as %s or %d where variables are inserted into the string at runtime. Given that the handling of format strings may vary across programming languages, using a language tag to specify which language the question is about is also recommended when using this tag.

A format string is an implementation of string interpolation where a string contains various placeholders, such as %d or %s, that are replaced by variables of the specified type, typically via library function. An example is the C printf() function:

int x = 10;
float pi = 3.14159
const char *str = "world";

printf("Hello %s %d %f\n", str, x, pi); // Hello world 10 3.14159

They may also contain arguments such as to specify how many digits of a float to print:

printf("%.2f\n", pi); // 3.14

This tag is useful for questions about the usage or behavior of format strings, such as finding the correct format specifier to print a certain type in a desired way.

Related Tags:

239 questions
335
votes
13 answers

How to escape the % (percent) sign in C's printf

How do you escape the % sign when using printf in C? printf("hello\%"); /* not like this */
Chris_45
  • 8,769
  • 16
  • 62
  • 73
156
votes
12 answers

What is the use of the %n format specifier in C?

What is the use of the %n format specifier in C? Could anyone explain with an example?
josh
  • 13,793
  • 12
  • 49
  • 58
105
votes
3 answers

Platform independent size_t Format specifiers in c?

I want to print out a variable of type size_t in C but it appears that size_t is aliased to different variable types on different architectures. For example, on one machine (64-bit) the following code does not throw any warnings: size_t size =…
Ethan Heilman
  • 16,347
  • 11
  • 61
  • 88
68
votes
5 answers

How can a Format-String vulnerability be exploited?

I was reading about vulnerabilities in code and came across this Format-String Vulnerability. Wikipedia says: Format string bugs most commonly appear when a programmer wishes to print a string containing user supplied data. The programmer may …
Atul Goyal
  • 3,511
  • 5
  • 39
  • 59
45
votes
4 answers

How can I use a percent % in FormatString without it multiplying by 100?

I would like to format an integer as a percent without it multiplying by 100 as shown here. Because my source is an int, dividing it first by 100 is not a valid option. Is this possible? [DisplayFormat(DataFormatString = "{0:#%}")]
Keith Adler
  • 20,880
  • 28
  • 119
  • 189
39
votes
2 answers

How to check that two format strings are compatible?

Examples: "Something %d" and "Something else %d" // Compatible "Something %d" and "Something else %f" // Not Compatible "Something %d" and "Something %d else %d" // Not Compatible "Something %d and %f" and…
Erik B
  • 40,889
  • 25
  • 119
  • 135
37
votes
4 answers

What is the difference between %f and %lf in C?

I have seen these two parameters in a C example in a C book, but the author didn't elaborate what the difference between the two is. I know that %f specifies that a float should take its place. I have tried looking this up but have had a hard time…
committedandroider
  • 8,711
  • 14
  • 71
  • 126
13
votes
3 answers

Is it possible to use format strings to align NSStrings like numbers can be?

I'm using NSLog() to print some tabular data consisting of an NSString and an associated integer. Assume I know the length of the longest word. Is there a way using format strings to get this kind of column alignment: word:tree rank:5 …
willc2
  • 38,991
  • 25
  • 88
  • 99
12
votes
1 answer

How do I format a string with string interpolation in Scala as a fixed width string?

I'm interfacing with a really old system and the file I need to generate needs a field that is a formed from a string but needs to be exactly 15 in width. I want something like this: val companyName = "FooBar, Inc" // 11 chars f"$companyName%s" To…
myyk
  • 1,537
  • 1
  • 15
  • 35
11
votes
1 answer

How to repeat characters in Python without string concatenation?

I'm currently writing a short program that does frequency analysis. However, there's one line that is bothering me: "{0[0]} | " + "[]" * num_occurrences + " Total: {0[1]!s}" Is there a way in Python to repeat certain characters an arbitrary number…
Spaxxy
  • 171
  • 1
  • 2
  • 6
11
votes
4 answers

jqplot format tooltip values

I want to have a tooltip hover highlight thingy in jqplot. The problem is that I want it to give more detail then on the axes. So the formatter should be different. I can't get it to display the seconds to: There's a JS fidle here! I want the…
Jeroen
  • 1,638
  • 3
  • 23
  • 48
10
votes
2 answers

Print arguments of a function using Clang AST

I want to get the arguments passed to a function. for example, if I have the call printf("%d%d", i, j); the output should be %d%dij I am able to get to function calls using VisitCallExpr() in RecursiveASTVisitor. Also able to get the number of…
10
votes
4 answers

Way to format strings with "?" parameters to full string in java?

For example I want to implement class with method public class Logger { public void info(String message, String[] params) { } } If input is new Logger().info("Info: param1 is ? , param2 is ?", new String[] {"a", "b"}); Output must be…
qwazer
  • 7,174
  • 7
  • 44
  • 69
10
votes
5 answers

UnicodeDecodeError using Django and format-strings

I wrote a small example of the issue for everybody to see what's going on using Python 2.7 and Django 1.10.8 # -*- coding: utf-8 -*- from __future__ import absolute_import, division, unicode_literals, print_function import time from django import…
Sven R. Kunze
  • 1,909
  • 2
  • 13
  • 17
10
votes
1 answer

Why doesn't gcc -Wformat warn about printf %d on an unsigned int?

The following program has undefined behavior: #include int main(void) { unsigned int x = -100; // This is fine, becomes UINT_MAX - 100 printf("%d\n", x); // This is undefined behavior. return 0; } C99 7.19.6.1p8 states %d…
Chris Young
  • 15,627
  • 7
  • 36
  • 42
1
2 3
15 16