Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Everyone knows that, at least in C, you use the printf family of functions to print a formatted string. And these functions use a percent sign (%) to indicate the beginning of a format specifier. For example, %d means to print an int, and %u means to print an unsigned int. If you're unfamiliar with how the printf function and format placeholders work, or simply need a refresher, the Wikipedia article is a good place to start.

My question is, is there a particularly compelling reason why this was originally or should be chosen in the future as the format specifier?

Obviously the decision was made a long time ago (very likely for a predecessor of even the C language), and it's been more or less "standard" ever since then (not only in C, but also in a vast array of other languages that have adopted its syntax to varying degrees), so it's far too late to ever change. But I'm still curious if anyone has any insight on why this choice might have been made in the first place, and whether it still makes sense as the choice if one is designing a new language with similar functionality.

For example, with C# (and the other family of .NET languages), Microsoft made a slightly different decision regarding the operation of the string formatting functions. Although some degree of type safety can be enforced there (unlike with the implementation of printf in C), and therefore it is unnecessary to include an indication of the type of the corresponding parameter, they decided to use zero-indexed pairs of curly braces ({}) as format specifiers, like so:

string output = String.Format("In {0}, the temperature is {1} degrees Celsius.",
                              "Texas", 37);
Console.WriteLine(output);

// Output:
//     In Texas, the temperature is 37 degrees Celsius.

The documentation for the String.Format method contains more information, as does this article on composite formatting in general, but the exact details are rather unimportant. The point is simply that they abandoned the long-standing practice of using % to indicate the beginning of a format specifier. The C language could just have easily used {d} and {u}, but it didn't. Anyone have any thoughts on why, whether this decision makes sense in retrospect, and whether new implementations should follow it?

Obviously there's no character that could be chosen that wouldn't have to be escapable so that it could be included in the string itself, but that problem is quite well solved already by just using two of them. What other considerations are relevant?

share|improve this question

migrated from stackoverflow.com Aug 5 '12 at 15:17

This question came from our site for professional and enthusiast programmers.

4  
The escaping problem is not solved by using two characters. It just means you have one more character to escape. –  Juhana Aug 5 '12 at 6:36
2  
I am curious. Certainly, it would be possible to use {u} instead of %u but would it have any significant advantage? It seems like a largely arbitrary choice. –  Charles Bailey Aug 5 '12 at 8:27
10  
@JarrodRoberson so you are saying they deliberately chose the {} syntax so that people learning C# would not start learning anything else? I find it very hard to believe that was a major, if even any, part of their design decision. Can you backup your statement somehow? –  stijn Aug 5 '12 at 18:24
5  
Interestingly, Python abandoned (a much superior form of) % formatting in favour of something akin to .NET’s {} formatting because the latter offers more flexibility. –  Konrad Rudolph Aug 7 '12 at 12:43
2  
Why is the sky blue, and why is the word "blue" named blue? They had to pick something. –  user29079 Aug 8 '12 at 13:08
show 8 more comments

2 Answers 2

up vote 9 down vote accepted

As @Secure notes, C's printf function is inspired by BCPL's writef function. And if you look at the wikipedia page for BCPL, it has an example that shows that BCPL writef also used % to introduce a format specifier.

So we can infer that C used % either because BCPL did, or for the same reasons that BCPL did. My gut feeling is that it was simply that % is one of the least commonly used ASCII characters ... or so the authors thought. It is also likely that they didn't spend a lot of time in weighing the various alternatives. At the time, both BCPL and C were obscure languages, and the authors most likely had more important things to deal with.

However, there is a minor spanner in the works. While C was inspired by BCPL, it is not entirely clear whether the C borrowed BCPL I/O libraries or the other way around. I dimly recall that BCPL's I/O libraries went through a process of evolution about the time that the infix byte indexing operator was added to the language. (Actually, I think I know who would know about that.)

share|improve this answer
add comment

The Wikipedia entry doesn't contain much historical information, not specific to printf, but to escape characters in general.

http://en.wikipedia.org/wiki/Escape_character

Early reference to the term "escape character" is found in Bob Bemer's IBM technical publications. Apparently, it is he who invented this mechanism, during his work on the ASCII character set.

My guess is: The backslash was already used for string literals and another character was needed for the format strings. Most likely they chose the character with the assumed least frequency of normal usage and occurrence.

BTW, another related article is linked there with a term I've nether heard before:

http://en.wikipedia.org/wiki/Leaning_toothpick_syndrome

The article for printf has some more info snippets, but not about the reasons.

http://en.wikipedia.org/wiki/Printf

C's variadic printf has its origins in BCPL's writef function.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.