问题
I've always wondered if there are any naming convetions as when to use ALLCAPS for a type and when to append _t
(and when to not use anything?). I know back in the days K&R published all kinds of documents about how to use C, but I couldn't find anything about this.
Among the C standard library types, _t
seem prettys dominant
time_t
clock_t
uint32_t
size_t
sig_atomic_t
...
, as opposed to FILE
, va_list
or struct tm
. Are there actually rules to this or is it completely arbitrary? Microsoft always uses typenames in ALLCAPS in their Windows API, which at least seems more consistent than the C library, frankly...
回答1:
Actually according to the POSIX standard, all type names ending in _t are reserved, as defined here:
Names that end with ‘_t’ are reserved for additional type names.
回答2:
I would strive to minimize your use of typedef
. Use the struct
keyword for structures and only use typedef when you're either (a) defining an opaque type that might be implemented as anything, or (b) defining an alias for an arithmetic type you might want to change in the future or in different configurations (for example if you want to be able to choose float
or double
).
In any case, don't use ALL CAPS because it's hideously ugly, and don't use _t
because it's reserved by POSIX. That probably doesn't matter if you prefix your type names appropriately, but then the _t
on the end is just unnecessary ugliness and gratuitous nonportability. Just prefixing them like this should be fine: foo_scalar
(where foo
is the name of your library/module).
回答3:
It's entirely arbitrary -- different library writers/standards use different conventions (or no conventions at all). Just pick one for your code and be consistent.
回答4:
There aren't any since the types you mention may have been accumulated over the years from across standards (such as POSIX) and different dominant implementations. A case in point are xstr
and assert
both of which are macros in lowercase. However make sure you have read C-FAQ 12.9 and you should be good to go.
You may want to look up the section on Reserved Identifiers in the standard too. Here's what my copy of N1570 says:
7.1.3 Reserved identifiers
1 Each header declares or defines all identifiers listed in its associated subclause, and optionally declares or defines identifiers listed in its associated future library directions subclause and identifiers which are always reserved either for any use or for use as file scope identifiers. — All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use. — All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces.
— Each macro name in any of the following subclauses (including the future library directions) is reserved for use as specified if any of its associated headers is included; unless explicitly stated otherwise (see 7.1.4).
— All identifiers with external linkage in any of the following subclauses (including the future library directions) and errno are always reserved for use as identifiers with external linkage.184)
— Each identifier with file scope listed in any of the following subclauses (including the future library directions) is reserved for use as a macro name and as an identifier with file scope in the same name space if any of its associated headers is included.
2 No other identifiers are reserved. If the program declares or defines an identifier in a context in which it is reserved (other than as allowed by 7.1.4), or defines a reserved identifier as a macro name, the behavior is undefined.
3 If the program removes (with #undef) any macro definition of an identifier in the first group listed above, the behavior is undefined.
回答5:
The most common convention is that you use all caps (only) for macros (regardless of whether it happens to be a type or not).
回答6:
ALL CAPS are most commonly used for constants and/or macros. I haven't encountered it for types anywhere else but in the Windows API and wouldn't recommend it.
uint8_t etc is a quite common way of naming types, as we can see from the standard. Another common way is like This
with only the first letter capital.
回答7:
ALLCAPS are generally (not universally) used for macros, and in my experience are rarely used for type definitions. The most common conventions I've seen for type names are either LeadingUpperMixedCase or _t suffix (which others have pointed out may cause conflicts with POSIX)
来源:https://stackoverflow.com/questions/10356626/c-type-naming-conventions-t-or-allcaps