2

These days I starts learning C language.

And today by coincidence I find my C compiler can recognize non-alphanumeric variable names. Which is amazing. Below is an example:

zen1@localhost:~/git_test|⇒  cat z.c
#include <stdio.h>

int main () {
    char *中文变量 = "谁是你爸爸";
    printf("%s\n", 中文变量);
    return 0;
}
zen1@localhost:~/git_test|⇒  make z
cc     z.c   -o z
zen1@localhost:~/git_test|⇒  ./z
谁是你爸爸

I searched online, which seems at first C standard doesn't support this kind of variable name.
But after a bit search work, I didn't find which version of C standard starts to support it.
Thus I asked this question.

YSC
  • 38,212
  • 9
  • 96
  • 149
Zen
  • 4,381
  • 5
  • 29
  • 56
  • 2
    C or C++, please pick one – David Heffernan Dec 30 '15 at 09:19
  • C and C++ share the same file postfix? I'm learning C now, so I think I'm asking for the C standard. – Zen Dec 30 '15 at 09:21
  • The mentioned answer says C99 and C++98. (link removed? http://stackoverflow.com/questions/5676978/unicode-identifiers-and-source-code-in-c11) – Jean-Baptiste Yunès Dec 30 '15 at 09:22
  • I would add that using all English names in variable names is a good practice. It means that almost anyone can read your code, rather than those that know Chinese, Swedish, Polish or whatever native languages there are. And it really doesn't look right if you mix the C english-based keywords with other languages anyway. – Mats Petersson Dec 30 '15 at 09:24
  • @Zen So why is it tagged C++? These are different languages. – David Heffernan Dec 30 '15 at 09:24
  • @DavidHeffernan, I'm not familiar about C++, but I heard about that C++ is based on C, which is compatible with C. So C standard is always suitable on C++, meanwhile we can't say C++ standard is not always suitable on C? – Zen Dec 30 '15 at 09:24
  • @Mats Petersson, I totally agree with your point. I'm just curious about this feature. – Zen Dec 30 '15 at 09:26
  • @Zen C and C++ are evolving separately. C has features that C++ doesn't have. C++ of course has plenty more features that C doesn't have. Unless you already know the answer to your question, you have no way of knowing whether there's a relevant difference between C and C++ here. (And yes, there are some differences between the two languages related to UCNs, but they're minor.) –  Dec 30 '15 at 09:26
  • @Zen No, that's not correct. They are different languages, albeit related. This should be tagged C only. Please correct. – David Heffernan Dec 30 '15 at 09:26
  • You're wrong @Zen. In fact C and C++ are two different languages although they look very similar for the basic constructions. But the fact that in general a C++ compiler also compile C (and usually the converse) is a good point that may explain why encodings may be supported for both on your compiler. – Jean-Baptiste Yunès Dec 30 '15 at 09:27
  • The question is about the C language, the duplicate question answers the same question for the C++ language. – chqrlie Dec 30 '15 at 09:51
  • 1
    Regarding the C language, non ASCII letters have been supported in identifiers since C99. *Annex D - Universal character names for identifiers* normalizes which characters are allowed. The downside of using these is it makes the code much harder to read for non native speakers (and writers). – chqrlie Dec 30 '15 at 10:04

0 Answers0