2

I'm curious if there's any way to use @, $, or ? in a C function or variable name. I know that linkers allow them (because of C++ name mangling).

Is there any kind of escape code that could allow this (I don't care how ugly it looks)? Or, in standard C, is this completely impossible?

MiJyn
  • 5,327
  • 4
  • 37
  • 64

2 Answers2

6

It is not possible in purely standard C.

But if using GCC (or probably Clang/LLVM) you can have $ in identifiers, and you can set the linker name using asm labels

You could perhaps also play GNU ld tricks with ld scripts.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
5

Standard C does not allow any of these (and it can't really allow ? since it's an operator and thus a separate token). GCC (and possibly compatible compilers) allow $ but not the others. However, you could use the GNU C (GCC) extension for making a declaration for an external-linkage name that references a different underlying symbol name; this may achieve what you want, e.g. if you're trying to reference C++ symbols. I believe the syntax is something like adding __asm__("symbol_name") to the end of the function declaration. There are some examples in the glibc headers on most Linux systems.

Alternatively, if you have dlsym, you could use it to look up the names at runtime.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711