views:

579

answers:

6

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?

+2  A: 

The sad truth is - there is no real convention.

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

Eli Bendersky
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
A: 

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

Soufiane Hassou
Could you explain more?
AraK
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
How `C` has a name `strpbrk` and `C++` has a name `sync_with_stdio`, and not `swstdio` :)
Johannes Schaub - litb
+11  A: 

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
"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
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
+4  A: 

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
+5  A: 

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
+1 for the link. More names are reserved than you realize, for example types ending in `_t`.
kaizer.se
@kaizer: afaik reserving `_t` is a POSIX restriction
Christoph
Yeah. C only reserves those that start with `uint` or `int` and end with `_t`.
Johannes Schaub - litb
+1  A: 

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