I was wondering: is string defined as a keyword or typedef name in any plain C (not C++) implementation?
Meaning: either the implementation has a string
keyword, or it has a built-in header with a line similar to:
typedef char* string;
I was wondering: is string defined as a keyword or typedef name in any plain C (not C++) implementation?
Meaning: either the implementation has a string
keyword, or it has a built-in header with a line similar to:
typedef char* string;
No C standard has string
as a keyword. The idea that some folk have of writing
typedef char* string;
is to be reprehended, as it obfuscates.
Use a char*
, const char*
, char[]
or const char[]
as appropriate, to model a NUL
-termimated string of characters. There are other ways of modelling text, but this one is the one used by the C standard library.
Some implementations may provide a string
but they would be doing so against the express wishes of the ISO standard, and you could argue that they are therefore not a C implementation. That's because, in C11 7.31 Future library directions
, it explicitly states:
Function names that begin with
str
,mem
, orwcs
and a lowercase letter may be added to the declarations in the<string.h>
header.
Earlier, in 7.1.3 Reserved Identifiers
, we see (my emphasis):
All identifiers with external linkage in any of the following sub-clauses (including the future library directions) and
errno
are always reserved for use as identifiers with external linkage
So implementations should not be doing this. Of course, you're free to create something called string
in your own code, you just have to be aware of the issues. If you value portability, you should not be defining anything that meets this criteria. Otherwise, a future C standard may render your code non-compilable.
No, C implementations do not provide a string
keyword and they can't, really. As C99 says (4. Conformance):
- [...] A conforming implementation may have extensions (including additional library functions), provided they do not alter the behavior of any strictly conforming program.3)
3) This implies that a conforming implementation reserves no identifiers other than those explicitly reserved in this International Standard.
The keywords defined by C99 are exactly:
auto enum restrict unsigned
break extern return void
case float short volatile
char for signed while
const goto sizeof _Bool
continue if static _Complex
default inline struct _Imaginary
do int switch
double long typedef
else register union
string
is not on that list.
Later on the standard defines which identifiers are reserved for use by the implementation:
- 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.
If an implementation wants to provide an additional keyword, it would have to be something like __string
or _String
.
Under 7.26 Future library directions it says:
7.26.11 String handling
<string.h>
- Function names that begin with
str
,mem
, orwcs
and a lowercase letter may be added to the declarations in the<string.h>
header.
So the "best" an implementation can do is to define string
as a function, and only if <string.h>
is included.