33

I wonder if the naming convention used in C/C++ standard libraries has a name, or at least a cheat-sheet where I can lookup the rules. E.g.

push_back    -- underscore used
setstate     -- but not used here!
string::npos -- when to use abbreviations?
fprintf
...

Does the naming convention used in the C/C++ standard libraries have a specific name?

Khaled Alshaya
  • 94,250
  • 39
  • 176
  • 234

6 Answers6

33

C/C++ uses the famous make-stuff-up-as-we-go-along naming convention. In general, the only consistent things you can say about the C/C++ standard library naming convention is that it doesn't use camel case (except for C++ STL template typenames), it uses lower-case class and function names, and (sometimes) uses underscores to separate words. But the usage of underscores is more of a C++ thing; C in particular tends to abbreviate words, e.g. strlen, memcpy. This is probably a result of the tendency to use very terse names for things in the early days of UNIX.

Charles Salvia
  • 52,325
  • 13
  • 128
  • 140
  • 24
    "tendency to use very terse names for things" which is either due to 80-char or smaller terminals; resource limits on compilers; an innate desire to Huffman encode the universe; or all of the above. – Steve Jessop Nov 14 '09 at 15:32
  • 24
    Pre-ansi compilers only considered the first 6 characters of a symbol to be salient in order to be compatible with Fortran, IIRC, so the symbols `strlen`, `strlenxyz`, and `strlenohmygodwhathaveidone` all referred to the same thing. So, for compatibility with pre-ANSI compilers, the ANSI C standard made sure the first 6 characters of all standard library symbols were unique among each other. – Adam Rosenfield Dec 03 '09 at 01:13
9

The C standard library has well defined rules you must follow to avoid name conficts. I don't know anything about C++ though.

If you think this is a mess you should check out the PHP library...

ntd
  • 7,372
  • 1
  • 27
  • 44
6

I think there are several groups invented at different times by different people and using somewhat different conventions: C libraries, streams, strings, STL (containers + algorithms + iterators). I have a feeling that the latter might be seen as the convention, which sets the example for things like boost and C++0x naming.

UncleBens
  • 40,819
  • 6
  • 57
  • 90
4

The sad truth is - there is no real convention.

See the argument order of stdio functions as a painful reminder.

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
  • 3
    There is a good reason for those, actually. With the traditional C function call ABI, calling multiple functions with the same last argument (i.e. `for (i = 0; i < n; i++) { fputs(s[i], stdout); fputc('\n', stdout; }` is cheaper than if it were the first argument non-changing. However, for variadic functions like `printf`, the fixed arguments must go at the start of the argument list. – ephemient Nov 14 '09 at 20:58
0

I don't have the answer, but at least, I don't think that C and C++ use the same naming convention.

Soufiane Hassou
  • 17,257
  • 2
  • 39
  • 75
  • Things like fprintf, strlen, itoa (shudder) come from C libraries. Those have the convention of lower-case, no underscores and heavy abbreviating. The C libraries are the ones beginning with "c" in C++: cstdio, cstring, cmath etc. – UncleBens Nov 14 '09 at 14:12
  • How `C` has a name `strpbrk` and `C++` has a name `sync_with_stdio`, and not `swstdio` :) – Johannes Schaub - litb Nov 14 '09 at 16:10
0

I don't think there is a name for either set of naming conventions.

The naming conventions in the C and C++ standards are different, though faintly related.

In particular, in the C library, no function or macro name contains an underscore (AFAICR), unlike the C++ library.

Most of the C library naming convention was controlled by precedent; it standardized existing practice (as of about 1984), with remarkably few inventions (locale handling via <locale.h> being the main one).

The C++ library was based on precedent too - but a different set of precedents, and I think it is arguable that the STL was not in as widespread use prior to its adoption by the standard as the functions in the C library were.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278