2

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

double ε,µ,β,ϰ; 

If yes, how can this be achieved?

9
  • 1
    Full answer here: en.cppreference.com/w/cpp/language/identifiers Commented Oct 1, 2018 at 7: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. Commented Oct 1, 2018 at 7: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. Commented Oct 1, 2018 at 7:40
  • 1
    @stackoverflower: Wrong compiler - this is tagged visual C++ Commented Oct 1, 2018 at 8:33
  • 1
    Sort of a duplicate - stackoverflow.com/questions/32799078/…. Doesn't mention Greek explicitly, but the top answer does reflect P.W.'s answer below. Commented Oct 1, 2018 at 8:37

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

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).
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.
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.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.