C identifier with dollar sign

About some C compilers allowing identifiers with a $ sign in them.

From my old blog.


As mentioned in C standards (see here, for a draft version of C99, under section 6.4.2), a valid C identifier

… is a sequence of non-digit characters (including the underscore _ ,the lowercase and uppercase Latin letters, and other characters) and digits …

That means a $ sign cannot appear anywhere in an identifier.

Yet some compilers allow variable names having $ via extensions.

(So yeah, this is not standard C and therefore is not portable.)

Consider the following program:

#include<stdio.h>

int fn$a$()
{
    printf("\nExecuted!");
}

int main()
{
    int $id=3;
    printf("\n%d", $id); 
    fn$a$();
    return 0;
}

It has two identifiers with '$' in them:

But will still compile without errors in both gcc (6.3.0) and clang (4.0.1) using default flag values.

And print

3
Executed!

upon execution.

Interesting isn't it? :-)

See more