Talk:printf
This is the talk page for discussing improvements to the Printf article. This is not a forum for general discussion of the article's subject. |
Article policies
|
Find sources: Google (books · news · scholar · free images · WP refs) · FENS · JSTOR · TWL |
![]() | This article is rated C-class on Wikipedia's content assessment scale. It is of interest to the following WikiProjects: | ||||||||||||||||||||||||||
|
Old VfD discussion
[edit]Unattributed edits
[edit]The edits by User:68.100.130.21 on this page were by me. --Graue 21:31, 11 October 2005 (UTC)
v variants
[edit]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
[edit]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
[edit]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)
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)
[edit]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
[edit]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
[edit]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
[edit]Lua has a %q
specifier in its string.format()
function (which is basically just like PHP's sprintf()
) 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
printf
builtin supports it. —82.36.30.173 (talk) 00:47, 10 August 2012 (UTC)
rewrites
[edit]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)
printf
in Java
[edit]The reason that I said that Java's implementation of printf
gives it the functionality of both printf
and fprintf it that it is possible to open a PrintStream object to write to a file and call that object's printf
method to write to that file (similar in practice to fprintf
, but more syntactically similar to printf
). Because System.err
and (obviously) System.out
are both PrintStream
objects, they both support printf
. --Btx40 (talk) 21:07, 28 April 2008 (UTC)
Should it not be mentioned that Java 1.5 and later also support a form of sprintf
in the form of String.format
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
[edit]The section on precision uses "*" in the beginning of the sentence, shouldn't it be ".*" instead? The flag ".*" is used in the example printf
statement in the second sentence. 64.94.55.231 (talk) 18:02, 16 June 2008 (UTC)
Call for examples
[edit]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?
[edit]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
[edit]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
[edit]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
[edit]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
[edit]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
[edit]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#Redundant content forks).
- I think the proposed merges sit in three levels of similarity:
- 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#Format, 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
[edit]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
[edit]Moved to Talk:String_interpolation#Merge_with_Variable_interpolation.
Helpful links
[edit]- 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.
[edit]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?
[edit]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
[edit]
The redirect %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 Wikipedia:Redirects for discussion/Log/2024 March 17 § %d until a consensus is reached. Nickps (talk) 14:00, 18 March 2024 (UTC)
"Format string" listed at Redirects for discussion
[edit]
The redirect 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 Wikipedia:Redirects for discussion/Log/2024 March 18 § Format string 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
[edit]
The redirect 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 Wikipedia:Redirects for discussion/Log/2024 March 18 § Placeholder (Computing) until a consensus is reached. Nickps (talk) 19:08, 18 March 2024 (UTC)
"Format specifiers" listed at Redirects for discussion
[edit]
The redirect 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 Wikipedia:Redirects for discussion/Log/2024 March 18 § Format specifiers until a consensus is reached.
Tabular output limitation
[edit]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)
- 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)
- 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)
Start a discussion about improving the Printf page
Talk pages are where people discuss how to make content on Wikipedia the best that it can be. You can use this page to start a discussion with others about how to improve the "Printf" page.