1

I just came across some C code where the programmer used $ at the start of one of their variable names. Like this:

int main() {
    double $var;
    return 0;
}

I've never encountered this until now. It functions no different than a typical variable so I assume there is a convention for using such a variable name like how constant variables are named with all capital letters. However, I can't find any answer. The programmer was using it as an abbreviation for "amount".

Sintrias
  • 456
  • 1
  • 9
  • 19
  • 2
    "I just came across some C code" I doubt it. Please show a [mre] which contains that and can be compiled. If it cannot be compiled by a C compiler then it is not C code. – Yunnosch Oct 02 '21 at 06:53
  • @Yunnosch https://gcc.godbolt.org/z/EMGdYoEe4 – Aykhan Hagverdili Oct 02 '21 at 06:58
  • 2
    @Yunnosch it is as minimal as it can be. It compiles on both gcc and clang – bolov Oct 02 '21 at 06:58
  • [C11 6.4.2](https://port70.net/~nsz/c/c11/n1570.html#6.4.2) allows the usage of `$` a part of an identifier (*"other implementation-defined characters"*). If you have a program using it, it's using an implementation-defined option that may not work in some other compiler (or after an uprade or with different options). – pmg Oct 02 '21 at 06:58
  • 5
    Since this is an implementation-defined character, programs using variables like this are not fully portable, so it should be avoided. – Barmar Oct 02 '21 at 07:01
  • Well, I am baffled. – Yunnosch Oct 02 '21 at 07:16
  • Thanks everyone. It looks like I couldn't find anything on the question because I was specifically searching with "$" instead of "dollar sign". @Yunnosch I updated the question to include the main function for a full implementation. – Sintrias Oct 02 '21 at 08:02
  • Thanks for the edit. But in this case it was me doing the learning of something new. (Though from a non-extended syntax view point generous people might still see some reason in my first comment. But I really did not know at all that it is possible and do not retreat to that "high ground". ;-) ) – Yunnosch Oct 02 '21 at 08:05
  • @pmg it seems that C99 allows it too, `gcc -std=c99 -Wpedantic -Wall test.c ` compiles without warnings while `-std=c89` gives `warning: '$' in identifier or number ` – David Ranieri Oct 02 '21 at 08:22
  • @DavidRanieri: the behaviour of a specific compiler is not a guarantee about the Standard (*close, but no cigar*). [C99 6.4.2](https://port70.net/~nsz/c/c99/n1256.html#6.4.2) has the same description as C11 (cf [C89 A.1.1.3](https://port70.net/~nsz/c/c89/c89-draft.html#A.1.1.3)). – pmg Oct 02 '21 at 08:24
  • @pmg I thought that the purpose of `Wpedantic` was to throw a warning when using extensions unless the keyword `__extension__` was used, what is happening here? – David Ranieri Oct 02 '21 at 08:30
  • 2
    `gcc` mostly (I can't quantify it any better) strictly follows the Standard when invoked with `-std=` and `-Wpedantic`... I am just saying there *may be* situations where there's a discrepancy (not to speak of interpretation of the Standard itself -- which tries very damn hard to be objective). – pmg Oct 02 '21 at 08:35
  • @pmg: Actually, in this case, the behavior of a specific compiler is a guarantee about the standard. C 2018 4 7 says “A *conforming program* is one that is acceptable to a conforming implementation.” If no existing compiler accepted `$` in identifiers, then a program using `$var` would not be a conforming program. So demonstrating that a specific compiler does accept it guarantees that, according to the C standard, a program that uses `$var` and is otherwise conforming is a conforming program. – Eric Postpischil Oct 02 '21 at 11:36
  • gcc describes the extension here https://gcc.gnu.org/onlinedocs/cpp/Tokenization.html – stark Oct 02 '21 at 12:05
  • So not all C compilers will successfully compile this code? That seems like something that needs to be addressed because it creates ambiguity in the language. – Sintrias Oct 02 '21 at 21:00

0 Answers0