26

What does f stand for in the name of C standard library functions? I have noticed that a lot of functions have an f in their name, and this does not really make sense to me.

For example: fgets, fopen, printf, scanf, sqrtf and so on.

Jongware
  • 22,200
  • 8
  • 54
  • 100
Erik W
  • 2,590
  • 4
  • 20
  • 33
  • 2
    the leading `f` and the trailing `f` both have a meaning, but are unrelated. – Iharob Al Asimi Jan 14 '15 at 18:04
  • 10
    First two: "file". Second two: "format" or "formatted". `fscanf()` and `fprintf()`: both. – Paul Roub Jan 14 '15 at 18:05
  • 2
    if it's a prefix, then it's generally "file". e.g. `(f)ile (open)`. If the F is postfix, then usually it's 'format'. `(print) (f)ormat`. `(f)ile (print) (f)ormat` – Marc B Jan 14 '15 at 18:05
  • 3
    Another use of 'f' is for `float` as in `ceilf()`, `round()`, ... In `math.h` functions there are also suffixes to indicate the return type. – meaning-matters Jan 14 '15 at 18:06
  • 1
    Shorthands like these, and the similar `s` in `sprintf`, `sscanf`, and the `str` prefixes were preferred over full names because initially [only 6 were significant](http://stackoverflow.com/questions/18290165/whats-the-exact-role-of-significant-characters-in-c-variables). – Jongware Jan 14 '15 at 18:15

5 Answers5

61

Your question in general is too general but I can explain a few examples.

  • fgets, fopen, fclose, … — The ”f“ stands for “file”. These functions accept or return a FILE * pointer as opposed to a file number as the POSIX functions do.
  • printf, scanf, … — The ”f“ stands for “formatted”. These functions accept a format string.
  • fprintf, fscanf — This is a combination of the above two.
  • sinf, cosf, … — The “f” stands for float (to distinguish from the double alternatives). Note that this fits quite nicely with suffixing floating point literals with an f as in 1.5f.
  • Finally, as Deduplicator points out, there are some names such as free, floor or setbuf (“set buffer”) where the “f” simply appears as a natural language character.

The tradition of pre- or suffixing names with single letters that indicate the type of the arguments is a necessity in C that has become obsolete in C++ thanks to overloading. Actually, overloading in C++ works by the compiler automatically adding those suffixes again under the hood to the generated symbols by a process called name mangling.

5gon12eder
  • 24,280
  • 5
  • 45
  • 92
8

The leading f refers to the type that function operates on:

  • fgets: use gets on a FILE * handle instead of just stdin
  • fopen: open a file, and return it as a FILE * (instead of a file descriptor which the original open does)

The trailing f means that it uses a formatting string:

  • printf: print out according to the format specifier
  • scanf: read in according to the format

And combined, you get things like:

  • fprintf: print out to a particular FILE * according to the format specifier

When you consider things like the math.h functions, then the trailing f designates that the particular function operates on operands of type float like so:

  • powf: take the exponent of floats
  • powl: take the exponent of long doubles
randomusername
  • 7,927
  • 23
  • 50
6

A leading f stands for file, a trailing one stands for formatted; for example, sscanf is used to scan strings for values in a pattern as specified by a format, whilst fprintf prints formatted text to a file.

EDIT: Oh, and then there's math functions from math.h that will have type suffixes like atanf for calculating the arcustangens for float values.

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
4

I am under the impression that for fgets and fopen the f stands for file.

For printf and scanf I believe that the f stands for formatted. This is at least partially supported by the Wikipedia article on scanf.

kjschiroo
  • 500
  • 5
  • 10
1

In functions from the stdio library, the preceeding f in things like fread, fprintf, fwrite, etc. stands for 'file'. The f at the end of functions like printf and scanf stands for 'format'. Thus printf is FORMATTED printing, meaning that you can use things liek %d and %s in it.

In math functions a suffix of f usually means float.

Gophyr
  • 406
  • 2
  • 11