Talk:Printf

Old VfD discussion

 * The following discussion is preserved as an archive. Please do not modify it. Subsequent comments should be made on the appropriate discussion page.  No further edits should be made to this discussion.

from VfD:

Wikipedia is not a manual. (And if it were, it certainly wouldn't be a C manual) --fvw* 21:42, 2004 Nov 27 (UTC)


 * Keep - I think an entry on printf in general should exist, but it shouldn't be particularly language specific or implementation-centric. In short, it should be more of an overview of the concept of printf, not it's specific use and features. Should be a cleanup Zachlipton 22:27, 27 Nov 2004 (UTC)


 * Move to Wikibooks for C language. Exactly the place. Mikkalai 22:49, 27 Nov 2004 (UTC)
 * Good thinking. I would say to move it there and then use the Wikipedia entry for a more general look at printf. Zachlipton 23:08, 27 Nov 2004 (UTC)
 * Delete. Wikipedia is not your local manpages. Anyone needing this already has the documentation. There really isn't much, conceptually, behind printf that isn't language specific. --Improv 23:21, 27 Nov 2004 (UTC)
 * I think there is something to printf (maybe not a lot, but certainly something) that is the concept of the function and not related to the implementation. I'll try to mock up a better version of this entry on it's talk page when I get a chance. Zachlipton 01:54, 28 Nov 2004 (UTC)
 * I disagree, but let me know when you make said mockup -- I am willing to listen to arguments otherwise. --Improv 05:27, 1 Dec 2004 (UTC)
 * Definitely there is something --- look how hard it is to implement printf as a function, or even as a hard-coded language mechanism in a strongly statically typed language (such as, e.g., Objective Caml). This is not yet another standard library function... --Mikon 20:22, 16 December 2006 (UTC)
 * Keep - agree with Zach. But add C-specific stuff to Wikibooks. --Whosyourjudas (talk) 01:19, 28 Nov 2004 (UTC)
 * Delete, basic example of "Wikipedia is not a manual" Gazpacho 01:52, 28 Nov 2004 (UTC)
 * Delete: As others have said: no man pages. Geogre 01:57, 28 Nov 2004 (UTC)
 * Keep. More likely than other functions to up in everyday conversation. - RedWordSmith 03:41, Nov 28, 2004 (UTC)
 * This is an exception to the rule. Keep and cleanup.  Agree with Zachlipton.  &mdash;[[en:RaD Man|RaD Man (talk)]] 03:45, 28 Nov 2004 (UTC)
 * Delete: DCEdwards1966 05:16, Nov 28, 2004 (UTC)

A quick update: I went ahead and put the C-specific bits into Wikibooks (there has not been very much progress on the C wikibook, but I made space and included it). I'll try to rewrite the existing Wikipedia entry when I have the time. If someone else wants to do it, of course go right on ahead. Zachlipton 01:40, 29 Nov 2004 (UTC)
 * Cleanup as suggested by Zachlipton. Printf-like functions are common enough that an article on the basic theory has merit beyond the 'man page' level. -- Antaeus Feldspar 05:31, 28 Nov 2004 (UTC)
 * Keep. Most notable of all C functions. It would be hard to read other articles about C unless there is a description of printf available one wikilink away. jni 11:57, 28 Nov 2004 (UTC)
 * Keep - that's a classic. Although I think the article doesn't need all these switches. Grue 18:31, 28 Nov 2004 (UTC)
 * Keep. It's famous enough unlike sscanf. This is one of the few exceptions to the rule. Jeltz 20:48, 2004 Nov 28 (UTC)
 * Forgot to add that if it should be kept the man stuff obviously has to go to wikibooks. But there si potential for having a dictionary article about printf or to make it a redirect to a page about printfunctions in general. Jeltz 20:52, 2004 Nov 28 (UTC)
 * Keep. Notable function. ElBenevolente 00:19, 29 Nov 2004 (UTC)
 * Keep, printf isn't just any function, it's the function. Like others have said, it's the exception to the rule, and thoroughly necessary to discussions of C related topics. Shane King 23:26, Nov 30, 2004 (UTC)


 * Strong keep. Notable function.  Certainly should have the options included with it. --Idont Havaname 06:24, 29 Nov 2004 (UTC)
 * keep and note that printf is used in other languages too. Possibly remove the list of format specifications and link to it elsewhere (wikibooks or other sources).  ~leif &#9786; HELO 06:44, 29 Nov 2004 (UTC)


 * Keep, notable. [[User:GRider|GRider\talk]] 19:45, 29 Nov 2004 (UTC)
 * Rename, a page for printf doesn't really have a place here - however, the concept of formatted output is one that many languages have in common - as is actually mentioned by the article. I suggest renaming this whole topic to Formatted output or some similar name and removing all the discussion on printf-specific format flags instead showing a few different ways of specifying flags - like Common Lisp's tilde-character vs C's percent-width-flags. A link to the specification of printf (such as the SUSv3 link at the bottom) should be sufficient coverage of printf's C API. The article should further be divided into sections and needs some general cleanup. olsner 08:39, 14 January 2007 (UTC)

end moved discussion


 * The above discussion is preserved as an archive. Please do not modify it. Subsequent comments should be made on the appropriate discussion page, such as the current discussion page. No further edits should be made to this discussion.

Unattributed edits
The edits by User:68.100.130.21 on this page were by me. --Graue 21:31, 11 October 2005 (UTC)

v variants
the v-variants need to be explained better. Why are they necessary? I can understand that this has to do with the fact that you cannot simply char* f(char* s, ...){ printf(s, ...); // what should the arguments be??? } but a code example for how vprintf solves that would be nice. --MarSch 13:33, 13 April 2006 (UTC)

Error in sprintf example
In the sprintf example, related to buffer overflows, it says: "If username in the above example exceeds 50 characters in length..." Having 35 characters on username will be enough to overflow the buffer. They forgot to count the characters in "Your name is ", period, newline and terminating null character.

Implimentation of printf
I think it would be very helpful to have an implimentation of printf - how it works at a lower level. I would think that it uses write at the most basic portable level, but I have no idea where to find if thats true, or how it works. Fresheneesz 22:07, 17 September 2006 (UTC)

According to the spec, printf places its output on the stdout stream (which is a global variable of type FILE*) which is usable with eg fputc, fputs or fwrite. So somewhere along the line, printf must use one of those functions. Here's a link to glibc's implementation of vfprintf (in glibc, vfprintf is called by printf to do the actual work). It's not pretty! olsner 08:39, 14 January 2007 (UTC)

ru:printf
Could some one translate russian article? —The preceding unsigned comment was added by 89.223.67.131 (talk) 14:00, 14 January 2007 (UTC).

'*' width identifier (pad to width given in argument)
The current explanation is confusing although strictly speaking correct. The statement that the value n is the next argument means that n should be the next argument that printf will parse from its argument list. Because printf at the point when it reads the '*' has not read the data value yet; this means that the width argument should come before the associated data value. Thus, the example is correct. I can't figure out how to re-word this so that it will be clear immediately clear even if the reader doesn't understand how printf works internally. --24.226.31.7 05:08, 8 February 2007 (UTC)


 * I've rewritten the explanations of "*". See if it works any better. --Deryck C. 02:53, 10 February 2007 (UTC)

Typo
echo -n -e "$FOO\t$BAR'

Are the quotations right? I don't think so, but I do not know whether " or ' is correct in Shebang (Unix)? --Lazer erazer 19:31, 10 April 2007 (UTC)


 * They weren't right; and they should've been  in order to have the   character assume the appropriate semantics (using   would've protected the   from shell interpretation).  I fixed. -- tiny plastic Grey Knight   ⊖  13:18, 2 August 2007 (UTC)

history
How about some history here? I failed to learn the thing I wanted to learn when I came to read this article: Where did the name "printf" come from? What's the 'f' for? Why isn't it simply named "print"? I remember being originally told that the 'f' stood for 'function', because it was the print function, but that makes little sense since *every* library call in C is a function. It wasn't fopenf or strcpyf or mallocf. So what's the origin of the 'f'?


 * As far as I'm aware it means "print formatted text". I don't have a source for that, though. -- tiny plastic Grey Knight   ⊖  11:40, 2 August 2007 (UTC)

I would be very interested to know when printf was first introduced. My own memory is that I invented Lisp's "format" without any knowledge of "printf" whatsoever. Unix systems did not show up (at least not prominently) at MIT (at least at the MIT AI Lab) until after "format" was in place. Ken Thompson and Dennis Richie were working on Multics at the time ioa_ was introduced, suggesting strongly that printf was derived from ioa_, as was Lisp's "format". I particularly mention this because in Guy Steele and Richard Gabriel's paper on "The Evolution of Lisp", they say that "format" came from Fortran. When I told Steele that it was from ioa_, he asked whether ioa_ might have been influenced by Fortran. I got in touch with the original author of ioa_, Stan Dunten, who told me that specifiers like "F5.2" are "clearly from Fortran", but not the general idea of the control string with a variable number of arguments. -- DanielWeinreb 26 Oct 2008 —Preceding unsigned comment added by DanielWeinreb (talk • contribs) 14:39, 26 October 2008 (UTC)

%q
Lua has a  specifier in its   function (which is basically just like PHP's  ) which accepts a string argument and prints it with all special characters (like quotes, for instance) escaped with backslashes (no quote delimiters are placed on the result unless you include them in the format string yourself). Does this variant exist in any other languages? Is it worth mentioning? -- tiny plastic Grey Knight  ⊖  11:40, 2 August 2007 (UTC)
 * Bash's  builtin supports it. —82.36.30.173 (talk) 00:47, 10 August 2012 (UTC)

rewrites
I made a new, smaller, tidier lead paragraph, and moved the old one down into a "printf in different languages" section. That bit reads a bit oddly now that it's a section, but then again a lot of the article wouldn't suffer from a rewrite. I think a slightly more language-agnostic approach would be nice, with any particularly notable language idiosyncracies collected into a section lower down. -- tiny plastic Grey Knight  ⊖  13:11, 2 August 2007 (UTC)

in Java
The reason that I said that Java's implementation of  gives it the functionality of both   and fprintf it that it is possible to open a PrintStream object to write to a file and call that object's   method to write to that file (similar in practice to , but more syntactically similar to  ). Because  and (obviously)   are both   objects, they both support. --Btx40 (talk) 21:07, 28 April 2008 (UTC)

Should it not be mentioned that Java 1.5 and later also support a form of  in the form of   method. It does differ in name, but the application of the function is nearly identical. --Zanthra (talk) 07:20, 20 May 2008 (UTC)


 * It has aleady been mentioned. I'm not sure if the Java implementation is the only one to throw exception on an error condition. (There is the .NET impementation, but that uses a substantially different syntax) --Btx40 (talk) 18:58, 21 May 2008 (UTC)


 * Actually, I added the mention of it shortly after posting in this. --64.175.43.196 (talk) 22:29, 21 May 2008 (UTC)

Then it should also mention MFC's CString::Format. AndrewWPhillips (talk) 04:58, 9 November 2010 (UTC)

Precision
The section on precision uses "*" in the beginning of the sentence, shouldn't it be ".*" instead? The flag ".*" is used in the example  statement in the second sentence. 64.94.55.231 (talk) 18:02, 16 June 2008 (UTC)

Call for examples
I want to replace code like the following (in Java, but PHP and C and C# and C++ are also important ;-) ...

System.out.println("Your age is " + age + " and your weight is " + weight);

with something like this ...

System.out.println("Your age is %int and your weight is %int", age, weight);

Please construct a few examples like this, for the weary programmer-in-a-hurry who doesn't want to wade through several paragraphs of formal specifications and historical detail. Just tell me how to use this functionality. --Uncle Ed (talk) 16:11, 23 August 2008 (UTC)


 * Would %d (for decimal integer) do it? And why doesn't Wikipedia have an article on the decimal integer (if only as a formatting concept)? --Uncle Ed (talk) 16:20, 23 August 2008 (UTC)


 * There's a pretty good example in the image at the head of the article. Honestly, though, I think the article needs a major overhaul. -- BenRG (talk) 17:43, 23 August 2008 (UTC)


 * Yes, I saw that. Let's overhaul the article together, shall we? --Uncle Ed (talk) 00:50, 26 August 2008 (UTC)

Controversies?
There is distinct controversy over the use of printf in languages that just pass an untyped buffer around and expect the developer to give clear instructions in the other parameters regarding how to divide it up. C and C++ do this, but I don't know how many other languages provide direct interaction with memory the way they do. This becomes especially problematic with C++'s pointer-redirection. It is a common issue to edit the format portion of a printf statement and an integer be accidentally cast as a pointer to string, resulting in crashes that only occur under odd conditions.

Is this something that can be included? Regardless of whether this belongs in the general article on printf, I'd still like to suggest that risks, gotchas, and work-arounds be moved to a discrete section of this article. Robert Rapplean (talk) 18:35, 8 August 2009 (UTC)

This is a good point, though I will say that C++ only has printf by virtue of being (almost) a superset of C. In C++ you are supposed to use operator<< which is safer. (I am also not sure what you mean by "C++'s pointer-redirection" since pointers in C++ are the same as in C.) On the other hand lint (and others) can detect this sort of problem when the printf format string is a literal (as it is most of the time). AndrewWPhillips (talk) 05:05, 9 November 2010 (UTC)

non-printf info extraneous
I don't understand why any one would be interested in the historical information regarding the fact that Fortran and COBOL and Multics don't have printf. —Preceding unsigned comment.

I agree. The way Fortran, COBOL, Lisp etc print things has nothing to do with the C's printf statement. Perl, Python, Java etc are relevant as their printing is based on the C function. Algol is probably OK as it was the precursor. AndrewWPhillips (talk)

Good idea to show how languages that don't have printf do formatting and, in anticipation of perhaps, moving some of this article to formatting article.

DG12 (talk) 18:19, 17 October 2011 (UTC)

I have removed the extraneous sections. Examples unrelated to printf are unnecessary. Post-C languages that implement printf are also unnecessary thanks to the list at the end. The spiritual predecessors were retained. — Preceding unsigned comment added by Ziphit (talk • contribs) 19:42, 10 November 2016 (UTC)

%Z and %wZ in Win32
In Windows, the *printf functions support %Z and %wZ respectively, corresponding to pointers to the sort-of-documented Windows NT types "ANSI_STRING" and "UNICODE_STRING" respectively. Is it worthwhile mentioning this? —Preceding unsigned comment added by Myria (talk • contribs) 21:53, 15 March 2010 (UTC)

sprintf
should we have another page for sprintf with all its args? Umar1996 (talk) 13:53, 27 September 2010 (UTC)

No, its very closely related. AndrewWPhillips (talk) 04:47, 9 November 2010 (UTC)

snprintf is very old
The article implies that snprintf was only added to C in 1999. On the contrary it is very old. It first appeared in UNIX compilers before the first C standard (1989) but after that most compilers renamed it to _snprintf as it was not in the standard library.

AndrewWPhillips (talk) 04:46, 9 November 2010 (UTC)

Single page for format string

 * Based on Talk:C standard library

The following pages discuss essentially the same topic of format string and its implementations: fwprintf, printf, scanf, vwprintf, printk, swprintf, wprintf. I propose to cleanup these pages by removing the material that fails WP:NOTMANUAL and by merging the remains into Format string.1exec1 (talk) 21:35, 8 October 2011 (UTC)

In my opinion, merging can be done after all the pages almost get ready including code samples,usage etc for the ease of editing these pages.Ingaleashwini (talk) 19:50, 13 October 2011 (UTC)

I believe there is no such thing in wikiland as "after all the pages ... are ready" that is, things are supposed to be always tweaking better and better. DG12 (talk) 00:54, 15 October 2011 (UTC)


 * If your “getting ready” involves adding to the articles, it might be more efficient to merge first and then add, otherwise the merging task could be more complex, and the information added might turn out to be redundant (see WP:Article fork).
 * I think the proposed merges sit in three levels of similarity:
 * general C library functions with printf in their name
 * functions more related to scanf than printf
 * printk, because of its limited application and relationship with syslog
 * I certainly support all the printf-like functions from my first group being merged, whether it be to the existing printf name, or Format string, or maybe something like printf format string, which would distinguish it from other formatting strings such as those used by strftime, Python 3, others at String functions, etc. Perhaps printf functions or printf family to hint that each function shouldn’t necessarily have its own page? Summary of the current printf article as I currently see it:
 * Timeline of print statements and string formatting, before and after C’s printf
 * Closely related functions, in C and other languages
 * Variable argument list as a single parameter
 * Alternatives for string formatting, especially in C++
 * Formatting string
 * Data and format types
 * Modifiers
 * Field width for tables
 * Custom format types
 * Languages with printf formatting strings
 * Vadmium (talk, contribs) 04:28, 15 October 2011 (UTC).
 * I agree that Printf format string would be a better name. 1exec1 (talk) 14:35, 18 October 2011 (UTC)
 * Though I opposed before I agree now and Printf format string would be better name.Ingaleashwini (talk) 19:04, 18 October 2011 (UTC)
 * I've moved the page to printf format string and deleted some information relevant only to the C language. Now the article discusses the printf format string in general, whereas C file input/output gathers, among other things, all information about the C printf family of functions. 1exec1 (talk) 20:37, 20 October 2011 (UTC)
 * Format string (C programming language)? 110.174.44.126 (talk) 14:11, 10 April 2020 (UTC)
 * I have moved it back (to the original printf). printf format string is very awkward and not really the way articles are to be named.  See WP:COMMONNAME, WP:CONCISE.  It's actually surprising that it managed to last this long. -- C. A. Russell ( talk ) 03:04, 2 May 2023 (UTC)

width
Is it correct that a string cannot be truncated (including trailing spaces) using width (or anything else in the format)? For example

sssm="my shortname  " ; sssy="your verylongname    ";

printf ("~123456789ABC~\n~%12s~\n~%12s~", sssm,sssy)

outputs

~123456789ABC~ ~my shortname  ~ ~your verylongname   ~  — Preceding unsigned comment added by DGerman (talk • contribs) 01:12, 15 October 2011 (UTC)


 * Where does it say that? The width option is for setting a lower limit on the width by padding the output, so it is true that it does not do truncation. The precision option can be used truncate strings, although I seem to remember that some C standard (but apparently not POSIX) still requires that they always be null-terminated, even if you know truncation will always happen. Vadmium (talk, contribs) 02:55, 15 October 2011 (UTC).

Merge with Variable interpolation and String interpolation
Moved to Talk:String_interpolation.

Helpful links

 * http://blog.hostilefork.com/where-printf-rubber-meets-road/
 * http://www.ciselant.de/projects/gcc_printf/gcc_printf.html

Programming languages with printf -- literally? what is this list.
many languages on the list do not literally have a "printf" intrinsic, and most any language has some more or less roundabout mechanism to achieve similar results. So what exactly is the point of this list? — Preceding unsigned comment added by 75.186.86.53 (talk) 23:34, 25 March 2015 (UTC)

There were three types of languages in the list:

- printf using the same format string style (e.g. AWK) - a formatting function that had similar capabilities but a different format string style (e.g. AMPL) - a formatting function that is arguably unrelated to the spirit of printf (e.g. Python)

I went through and removed entries that fell under the last two categories Ziphit (talk) 20:49, 8 November 2016 (UTC)


 * THANK YOU!!!! Can we maybe delete all the Fortran and other stuff too? I suppose an article of "how text output was done in various programming languages" might be interesting but it this is not that article.Spitzak (talk) 18:49, 9 November 2016 (UTC)


 * I agree, the article should focus on printf specifically. The superfluous examples were removed.  Ziphit (talk) 19:35, 10 November 2016 (UTC)

Why is SQLite not on this list while T-SQL is? Admittedly SQLite is not really a programming language but is there no term for the SQLite-implementation of SQL? HansVB81 (talk) 17:27, 21 November 2020 (UTC)

Could somebody please explain the rationale for treating 0 values differently?
There's probably a mathematical explanation for why the # format "flag" character doesn't display 0 like other values, but it makes displaying lists of numbers considerably more complicated, if not slower. I think this would be a good page to explain why this decision was made (I can't find it). Tmellman (talk) 11:14, 12 September 2016 (UTC)

"%d" listed at Redirects for discussion
The redirect [//en.wikipedia.org/w/index.php?title=%25d&redirect=no %d] has been listed at redirects for discussion to determine whether its use and function meets the redirect guidelines. Readers of this page are welcome to comment on this redirect at  until a consensus is reached. Nickps (talk) 14:00, 18 March 2024 (UTC)

"Format string" listed at Redirects for discussion
The redirect [//en.wikipedia.org/w/index.php?title=Format_string&redirect=no Format string] has been listed at redirects for discussion to determine whether its use and function meets the redirect guidelines. Readers of this page are welcome to comment on this redirect at  until a consensus is reached. Nickps (talk) 14:00, 18 March 2024 (UTC)
 * I ended up renominating this one, so you should comment again if you already did. Nickps (talk) 01:10, 21 March 2024 (UTC)

"Placeholder (Computing)" listed at Redirects for discussion
The redirect [//en.wikipedia.org/w/index.php?title=Placeholder_(Computing)&redirect=no Placeholder (Computing)] has been listed at redirects for discussion to determine whether its use and function meets the redirect guidelines. Readers of this page are welcome to comment on this redirect at  until a consensus is reached. Nickps (talk) 19:08, 18 March 2024 (UTC)

"Format specifiers" listed at Redirects for discussion
The redirect [//en.wikipedia.org/w/index.php?title=Format_specifiers&redirect=no Format specifiers] has been listed at redirects for discussion to determine whether its use and function meets the redirect guidelines. Readers of this page are welcome to comment on this redirect at  until a consensus is reached.

Tabular output limitation
In response to removal of Tabular output by @Spitzak who says: Removed this, it is hardly a "limitation", the solution is obvious, and there isn't any better method. The described behavior of the function is definitely what I'd call a limitation, what is that obvious solution? and surely there is a better way. Other than that I agree with you ;)

I recently moved this section from Vulnerabilities to a new section Limitations, since that behavior is a limitation and is not a vulnerability.

But, I'm not going to revert the change since WP doesn't need to be a programmer's guide for the standard C library.

Maybe should describe this behavior under Width field. Stevebroshar (talk) 12:40, 20 April 2024 (UTC)
 * The obvious solution is that the programmer has to decide what to do if the value is too big to fit in the allotted width. One approach would be to use something like %7d %7d %7d (with spaces) instead of %8d%8d%8d. However, the how-to section was not appropriate at Wikipedia regardless of the details. Johnuniq (talk) 02:19, 21 April 2024 (UTC)
 * That the programmer has to decide what to do is obvious yes, but not what I'd call a solution. Your proposed solution is not what I'd call obvious. ... I think your proposed solution _could_ solve the problem, but it might not. If a value doesn't fit in width 8, then surely it won't fit in 7. Your solution ensures that there is whitespace between values, but the output won't be tabular. There are two issues: tabular format and space between values. I'd say the problem is challenging and there is no obvious solution. ... But, beyond that I disagree that there is an obvious answer, I have an issue with the word obvious. When someone says something is obvious, it sounds like they are saying they are smarter than anyone who does not see that solution on their own. IMO the world needs more compassion. ... WRT how-to: the first half describes a limitation. The second half is about how to work around the limitation. If we don't want how-to then could have removed the how-to part. Why remove the limitation part? ... IMO this article is sorely missing info on limitations since printf has a list of limitations arm long. Stevebroshar (talk) 10:25, 21 April 2024 (UTC)
 * I consider the solution obvious, especially if you just ask the programmer "what if you wanted a different character than space to separate the columns". And this is not a limitation of printf, it is a limitation of devices that are fixed width and cannot overprint a digit with some other symbol.Spitzak (talk) 10:44, 22 April 2024 (UTC)
 * Your understanding of obvious is different than mine. ... your question to the programmer makes no sense to me since your proposed solution is to use a space. ... the issue is more about formatting than output devices ... overprinting one character on another is a solution? Stevebroshar (talk) 12:50, 25 April 2024 (UTC)