0

An article here explains the differences which many of us have known for years. My assumptions have been:

  • sprintf
    • First character "s" is for first argument of type string.
    • Last character "f" is for format?
  • fprintf
    • First character "f" is for first argument of type string.
    • Last character "f" is for format?
  • printf
    • starts with "" for no first argument.
    • Last character "f" is for format?

I would think the first letter is for the first argument, but vsprintf and printf deny this claim. The last letter seems to mean it takes a format. Not to mention all the other uses of U's, N's, C's, V's and F's.

It would be amazing if someone could give explanation and source to the rational behind all these conventions in stdio.

BONUS - Get additional respect for name dropping.

Community
  • 1
  • 1
Eric Bischoff
  • 233
  • 1
  • 6
  • 2
    `fprintf()` writes to a filestream. The leading `f`stand for *file*. The final `f` really stands for *formatted*. – pid Oct 18 '16 at 21:15
  • @pid Thank you for your comment. Those were similar assumptions I've had. I could be wrong, but I think most people agree on what they stand for. I'm really looking for the rational, such as "is this part of a bigger naming convention?" and the source material, such as "Who wrote it?". – Eric Bischoff Oct 18 '16 at 21:35

2 Answers2

3

It's not so much the first character that you need to look at, but rather the prefix.

The "original" function, printf is for formatted printing. One or more prefixes can then be added to the printf name:

  • f: print to a FILE
  • s: print to a string
  • v: print with va_list
  • n: print a given number of characters
  • w: print wide characters:

So that gives us:

  • printf: formatted printing to stdout
  • fprintf: formatted printing to a FILE
  • sprintf: formatted printing to a string
  • snprintf: formatted printing to a string with a given number of characters
  • vprintf: formatted printing with va_list to stdout
  • vfprintf: formatted printing with va_list to a FILE
  • vsprintf: formatted printing with va_list to a string
  • vsnprintf: formatted printing with va_list to a string with a given number of characters
  • wprintf: formatted printing of wide characters to stdout
  • fwprintf: formatted printing of wide characters to a FILE
  • swprintf: formatted printing of wide characters to a string
  • vwprintf: formatted printing of wide characters with va_list to stdout
  • vfwprintf: formatted printing of wide characters with va_list to a FILE
  • vswprintf: formatted printing of wide characters with va_list to a string
dbush
  • 162,826
  • 18
  • 167
  • 209
1

Prefixes:

  1. N - provides a safety net for limiting the number of characters it can safely write/read from.

  2. V - you're using variable arguments list. This is useful if you want to write functions that emulate printf.

  3. S - you're reading/writing from/to a string.

  4. F - you're reading/writing from/to file.

  5. P - you're reading from the a process's output

  6. None - printf and scanf don't have prefixes as they're just reading/writing from standard input/output - which are nothing but files in the UNIX world anyway.

Suffixes:

  1. F - You're reading and writing in terms of formatted output (You're saying that you'll use a format string followed by variable arguments)

  2. C - You're dealing with characters one at a time.

  3. S - You're dealing with strings (the quantity is almost always till you reach the next whitespace character).

  4. W - You're dealing with words (don't confuse this with english words, word just means the sizeof(int))

zapstar
  • 458
  • 2
  • 8
  • 20