Talk:Printf format string

From Wikipedia, the free encyclopedia
Jump to: navigation, search
          This article is of interest to the following WikiProjects:
WikiProject Computing (Rated Start-class, Low-importance)
WikiProject icon This article is within the scope of WikiProject Computing, a collaborative effort to improve the coverage of computers, computing, and information technology on Wikipedia. If you would like to participate, please visit the project page, where you can join the discussion and see a list of open tasks.
Start-Class article Start  This article has been rated as Start-Class on the project's quality scale.
 Low  This article has been rated as Low-importance on the project's importance scale.
 
WikiProject C/C++ (Rated Start-class, Mid-importance)
WikiProject icon This article is within the scope of WikiProject C/C++, a collaborative effort to improve the coverage of C/C++ on Wikipedia. If you would like to participate, please visit the project page, where you can join the discussion and see a list of open tasks.
Start-Class article Start  This article has been rated as Start-Class on the quality scale.
 Mid  This article has been rated as Mid-importance on the importance scale.
Taskforce icon
This article falls within the scope of C.
 

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)

ru:printf[edit]

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 (talkcontribs) 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)

%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 (talkcontribs) 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]

Based on Talk:C standard library#Pages for each function and WP:NOTMANUAL

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:
  • 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#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)

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 (talkcontribs) 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.