2

Can we define the variable in C++/C using special characters such as;

double ε,µ,β,ϰ;

If yes, how can this be achieved?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 1
    Full answer here: https://en.cppreference.com/w/cpp/language/identifiers – Richard Critten Oct 01 '18 at 07:34
  • You can use macros to define them as something else. For instance, `#define β beta`, then use `β` as the name of an identifier. I wouldn't recommend it. – Blaze Oct 01 '18 at 07:35
  • 3
    There is no such thing as C/C++. These are two different languages with different rules. Pleaae decide which language you want to use and update your tags accordingly. – Gerhardh Oct 01 '18 at 07:40
  • try to use compilation option `-fextended-identifiers`. For more info check answers here: [https://stackoverflow.com/questions/32799078/why-doesnt-these-unicode-variable-names-work-with-fextended-identifiers](https://stackoverflow.com/questions/32799078/why-doesnt-these-unicode-variable-names-work-with-fextended-identifiers) [https://stackoverflow.com/questions/12692067/and-other-unicode-characters-in-identifiers-not-allowed-by-g](https://stackoverflow.com/questions/12692067/and-other-unicode-characters-in-identifiers-not-allowed-by-g) – stackoverflower Oct 01 '18 at 07:42
  • 1
    @stackoverflower: Wrong compiler - this is tagged visual C++ – MSalters Oct 01 '18 at 08:33
  • 1
    Sort of a duplicate - https://stackoverflow.com/questions/32799078/why-doesnt-these-unicode-variable-names-work-with-fextended-identifiers. Doesn't mention Greek explicitly, but the top answer does reflect P.W.'s answer below. – MSalters Oct 01 '18 at 08:37
  • @Gerhardh: While that's true, C and C++ both are ISO languages and follow basic rules from another ISO standard, ISO/IEC TR 10176 "Guidelines for the preparation of programming language standards." which in turn refers to ISO 10646 aka Unicode. – MSalters Oct 01 '18 at 08:45
  • @MSalters this does not mean that they have exactly the same rules where those unicode characters may be used. In ISO/IEC 9899:2011 I cannot find anything about an identifier not starting with a `universal-character-name` in a certain range. – Gerhardh Oct 01 '18 at 08:57
  • @Gerhardh: It's true they don't have the exact same rules, but given that both C and C++ base their rules on the same ISO standard, this is one of those rare cases where it does make sense to ask about C and C++ at the same time. Remember, this is no coincidence. WG14 and WG21 coordinate in these matters. – MSalters Oct 01 '18 at 09:06

3 Answers3

7

As per the working draft of CPP standard (N4713),

5.10 Identifiers [lex.name]
...
An identifier is an arbitrarily long sequence of letters and digits. Each universal-character-name in an identifier shall designate a character whose encoding in ISO 10646 falls into one of the ranges specified in Table 2. The initial element shall not be a universal-character-name designating a character whose encoding falls into one of the ranges specified in Table 3.

And when we look at table 3:

Table 3 — Ranges of characters disallowed initially (combining characters)

0300-036F 1DC0-1DFF 20D0-20FF FE20-FE2F

The symbols you have mentioned are the Greek Alphabet which ranges from U+0370 to U+03FF and the extended Greek set ranges from U+1F0x to U+1FFx as per wikipedia. Both these ranges are allowed as the initial element of an identifier.

Note that not all compilers provide support for this.

GCC 8.2 with -std=c++17 option fails to compile.
However, Clang 7.0 with -std=c++17 option compiles.

Live Demo for both GCC and Clang

P.W
  • 26,289
  • 6
  • 39
  • 76
  • Any character not in those ranges is allowed? `U+0370` to `U+03FF` is not in the range you quoted (`0300-036F` ends just before). – Fire Lancer Oct 01 '18 at 09:00
  • 1
    It makes sense that `0300-036F` is excluded. Those are combining characters/diacriticals, such as the ` in à. Per Unicode standards, combining marks should **follow** the character they modify, which logically excludes them from the begin of any other unicode string, not just C++ identifiers. – MSalters Oct 01 '18 at 11:37
1

Yes you can use special characters, but not all of them. You can find the allowed one in the link below.

You can find a detailed explanation on how to built identifier (with the list of unicode authorized characters) on the page Identifiers - cppreference.com.

An identifier is, quoting,

an arbitrarily long sequence of digits, underscores, lowercase and uppercase Latin letters, and most Unicode characters (see below for details). A valid identifier must begin with a non-digit character (Latin letter, underscore, or Unicode non-digit character). Identifiers are case-sensitive (lowercase and uppercase letters are distinct), and every character is significant.

Furthermore, Unicode characters need to be escaped.

YSC
  • 38,212
  • 9
  • 96
  • 149
Lham
  • 19
  • 5
1

Since the question is tagged Visual Studio: Just write the code as you'd expect it.

double β = 0.1;

When you save the file, Visual Studio will warn you that it needs to save the file as Unicode. Accept it, and it works. AFAICT, this also works in C mode, even though most other C99 extensions are unsupported in Visual Studio.

However, as of g++ 8.2, g++ still does not support non-ASCII characters used directly in identifiers, so the code is then effectively not portable.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
MSalters
  • 173,980
  • 10
  • 155
  • 350