Difference between revisions of "Printf family"

From Maths
Jump to: navigation, search
(Created page with "{{DISPLAYTITLE:{{cs|printf}} family}} : Not to be confused with ''printf (program)'' __TOC__ ==Description== The {{cs|printf()}} family of functions are used for ''formatt...")
 
(No difference)

Latest revision as of 09:46, 1 October 2017

Not to be confused with printf (program)

Description

The printf() family of functions are used for formatting strings, in fact printf is short for "print formatted", the family has 2 distinct branches:

  • First, defined in stdio.h[1]
    • int printf(const char* format, ...)
    • int fprintf(FILE* file, const char* format, ...)
    • int snprintf(char* target, size_t capacity const char* format, ...)
    • int sprintf(char* target, const char* format, ...)[Note 1] Warning:do not use
  • Second, defined in stdarg.h - these are identical except they use C varadics, via va_list
    • int vprintf(const char* format, va_list ap)
    • int vfprintf(FILE* file, const char* format, va_list ap)
    • int vsnprintf(char* target, size_t capacity const char* format, va_list ap)
    • int vsprintf(char* target, const char* format, va_list ap)[Note 2] Warning:do not use

Return values

Implementation notes


TODO: Explain that printf can't be a macro using fprintf as the standard specifies "functions" (so taking address must work)




TODO: Explain though, how any implementation written by a sane person, would make printf use vfprintf(stdout,...) "" for the string ones


Introductory examples

Formats

Examples

Notes

  1. Do not use this, same reason as gets(char* target) - the size of the buffer isn't given, so it may write beyond it!
  2. Do not use this, same reason as gets(char* target) - the size of the buffer isn't given, so it may write beyond it!

References

  1. POSIX - find older editions at some point!