Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can C++ variables in cpp file defined as Special Symbols β

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

double ε,µ,β,ϰ;

If yes, how can this be achieved?

like image 985
Science Guy Avatar asked Dec 31 '25 03:12

Science Guy


2 Answers

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

like image 105
P.W Avatar answered Jan 01 '26 17:01

P.W


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.

like image 25
MSalters Avatar answered Jan 01 '26 17:01

MSalters