Is someone able to explain why _variable$2
is a valid C programming identifier?
Based on the ISO documentation as well as a number of online supporting (credible) sources, I thought that only letters, digits and the _
(underscore) characters were allowed.
However variable names such as _variable$2
are completely valid and run as normal when compiled and tested. And if this is the case, what other special characters can and cannot be used similarly? Is this limited to simply characters or could even emoji's be substituted into valid identifier names within the C programming language? Any help would be appreciated :)
Is someone able to explain why: '_variable$2' is a valid C programming identifier?
It isn't, in the sense that a strictly-conforming C program cannot use identifiers that contain the '$' character.
I thought that only letters, digits and the '_' (underscore) characters were allowed.
Only the underscore, the decimal digits, the unaccented upper- and lowercase letters, and universal character names are required to be allowed in C identifiers (the universal character names are new in C11). However, the standard explicitly permits implementations to define other characters that they accept as well.
However variable names such as '_variable$2' are completely valid and run as normal when compiled and tested.
That one implementation accepts such identifiers does not make them "completely valid". It just makes them valid in that implementation.
And if this is the case, what other special characters can and cannot be used similarly? Is this limited to simply characters or could even emoji's be substituted into valid identifier names within the C programming language?
The standard specifies that the list of additional characters accepted in identifiers is implementation defined. This has a specific meaning in the standard: conforming implementations must document their choices for all implementation defined characteristics. Therefore, if you're willing to rely on the specific characteristics of some chosen implementation, then you should find a list or description of that implementation's allowed extra characters in its documentation.
On the other hand, if you want your program to work unchanged with multiple different C implementations, then you should stick to only letters, digits, and the underscore, and maybe universal character names in identifiers.
And don't be too quick to overlook those universal character names: to the extent that emoji (and many other characters) are encoded by Unicode, you can use UCNs to include them in your identifiers, at least in a logical sense, provided that you are content to rely on C11.