3

Is it somehow possible to program in C++ using characters from the Nordic alphabet in class- and variable names? (specifically: æ, ø and å).

Example:

auto føø = 2;

I am using GCC > 6, which do not seem to support it. Is there another compiler supporting these characters?

(FYI: I have duckduckgoed this, but I came up empty.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
unique_ptr
  • 233
  • 3
  • 9

1 Answers1

1

According to this:

Rules for Naming A Variable

  1. A variable name can't be a C++ keyword. For example, int can't be a variable name as it is a C++ keyword.
  2. A variable name must start with an alphabet (A-Z and a-z) or underscore ( _ ) sign. For example, var, X, _name, etc. are valid variable names, but 1a, $age, etc. are invalid variable names.
  3. Variable names can have alphabet (A-Z and a-z), underscore ( _ ), digits (0-9), but they can't have other symbols, such as %, &, @ , etc. For example, a_01 and findSum are valid variable names, but name& and calc% are not allowed in C++.

So to answer your question:

Is it somehow possible to program in C++ using characters from the Nordic alphabet in class- and variable names? (specifically: æ, ø and å).

It's not portable, because the standard doesn't allow it; of course it's up to the individual compiler to allow it anyway. What often does work is using a macro instead, like this:

#define føø my_foo

And then later do

auto føø = 2;
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Blaze
  • 16,736
  • 2
  • 25
  • 44
  • 1
    Does that source apply specifically to C++ 11? At least the current draft has different rules (for example, [*all* characters are significant](http://eel.is/c++draft/lex.name#1.sentence-5)). – Max Langhof Oct 16 '18 at 08:28
  • 1
    It doesn't specify a version. I removed that significant characters part, because honestly, it surprised me, too. After searching around a bit, it seems like the real limitation actually is "at least 32 characters", but most compilers required the whole name to be significant, anyway. Thanks to you I learned why lots of old C programs had such horrible and mangled variable names - the limit was 6 significant characters back then. – Blaze Oct 16 '18 at 08:45
  • The C++-standard explicitly allows certain ranges of unicode to be part of identifiers - as long as the other rules (like not starting with and underscore or number) are not broken. Specifically the range 00F8-00FF - which is a range starting with 'ø'. – ABaumstumpf Jul 14 '22 at 07:57