43

Recently, one of my friends encountered this question in an interview. The interviewer asked him if the special characters like $, @, |, ^, ~ have any usage in C or C++ and where.

I know that |, ^ and ~ are used as bitwise OR, XOR and complement respectively.

Do @ and $ have any special meaning? If they do, what would be an example where it could be applied?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Forever Learner
  • 1,465
  • 4
  • 15
  • 30
  • 3
    No, neither is part of the basic source character set, nor the basic execution character set. – chris Jun 09 '14 at 05:55
  • 2
    @Arman For some definition of "special meaning". They're illegal outside of comments, string literals and character literals. – James Kanze Jun 09 '14 at 08:56
  • 1
    @Arman My point was more or less that "special meaning" doesn't really mean anything. The standard places a number of requirements on the meanings of a number of characters. (FWIW: an implementation isn't required to accept $ or @ even in a comment or a string literal. I dare any implementation not to, however.) – James Kanze Jun 09 '14 at 12:19
  • 2
    Note that Microsoft uses @ in library function names, followed by a number representing the number of bytes used for input parameters for certain 32 bit calling conventions, but these "mangled" names are only visible from assembly code, not from C or C++ code. – rcgldr Feb 07 '17 at 15:16
  • A canonical question is *[Dollar sign in variable name](https://stackoverflow.com/questions/7926394/)* (2011). – Peter Mortensen Jun 16 '23 at 13:24

3 Answers3

42

@ is generally invalid in C; it is not used for anything. It is used for various purposes by Objective-C, but that's a whole other kettle of fish.

$ is invalid as well, but many implementations allow it to appear in identifiers, just like a letter. (In these implementations, for instance, you could name a variable or function $$$ if you liked.) Even there, though, it doesn't have any special meaning.

  • 3
    Of course if an implementation decides not to support that, then your code suddenly doesn't work. – chris Jun 09 '14 at 05:56
  • 1
    Aren't the only valid characters for names a-zA-z_0-9 ? – this Jun 09 '14 at 05:57
  • 1
    Corrected on `$`; forgot that was an extension. –  Jun 09 '14 at 05:58
  • @self., Or a *universal-character-name* (`\u` and `\U`), or other implementation-defined characters (`$` for example). – chris Jun 09 '14 at 05:59
  • C++11 allows a-z A-Z 0-9 and underscore and *"other implementation-defined characters"*. So it is up to an implementation whether $ or another identifier is allowed. MSVC allows $. I'm not sure about other compilers. – Matt Coubrough Jun 09 '14 at 05:59
  • @MattCoubrough, I remember GCC supporting it when I tried making a small, hackish, PHP-C++ thingy – chris Jun 09 '14 at 06:00
  • @MattCoubrough: GCC and clang allow `$` as well. Between that and MSVC, that's the majority of the "big" compilers. –  Jun 09 '14 at 06:01
  • @MattCoubrough et al [GCC allows it too](https://gcc.gnu.org/onlinedocs/gcc/Dollar-Signs.html) – Tony Delroy Jun 09 '14 at 06:02
  • 5
    Note that neither @ nor $ are valid for operator overloading either although you might be able to do evil things with preprocessor macros somehow – Matt Coubrough Jun 09 '14 at 06:03
  • 1
    void $$$$ (void) { printf ("Hai"); } main() { $$$$(); } Error: suffix or operands invalid for `call'. How ? – mahendiran.b Jun 09 '14 at 06:05
  • @self it's allowed as extensions in gcc and MSVC http://stackoverflow.com/questions/7926394/in-variable-name – phuclv Jun 09 '14 at 06:07
  • @mahendiran.b - It depends on the implementation. The code worked for me on gcc by adding int before main.#include #include using namespace std; void $$$$ (void) { printf ("Hai"); } int main() { $$$$(); return 0; } – Forever Learner Jun 09 '14 at 06:08
  • @MattCoubrough, http://coliru.stacked-crooked.com/a/3346f35f86802704. Actually, that reminds me a lot of [this](http://i.imgur.com/r75gOVP.png), where I wrote [this](http://coliru.stacked-crooked.com/a/75344cd23f7ad758). Now to go retrieve my nice Clang command line again because Clang didn't seem to like the dollar signs. – chris Jun 09 '14 at 06:25
  • I also imagine it might be possible to do raw string literals with just `@(literal)` to make it look a bit more like C#. – chris Jun 09 '14 at 06:30
  • 1
    MSVC, in particular, uses `$` and `@` in the names of certain hidden entities, like vtables. For any class `C`, if it has a virtual function it'll have vtable `C::$vftable@`, and/or if it has a virtual base class it'll have vtable `C::$vbtable@`. – Justin Time - Reinstate Monica Feb 06 '17 at 22:29
  • This is an incomplete answer. It ought to be more comprehensive. At least it should be qualified, e.g., *"within the standard"* or similar. – Peter Mortensen Jun 16 '23 at 13:11
  • From *[Dollar sign in variable name](https://stackoverflow.com/questions/7926394/dollar-sign-in-variable-name/7926469#7926469)* (my emphasis): *"The only legal characters* ***according to the standard*** *are alphanumerics and the underscore. The standard does require that just about anything Unicode considers alphabetic is acceptable (but only as single code-point characters). In practice, implementations offer extensions (i.e. some do accept a $) and restrictions (most don't accept all of the required Unicode characters). "* – Peter Mortensen Jun 16 '23 at 13:17
19

To complete the accepted answer, the @ can be used to specify the absolute address of a variable on embedded systems.

unsigned char buf[128]@0x2000;

Note this is a non-standard compiler extension.

Check out a good explanation here

Community
  • 1
  • 1
Bilow
  • 2,194
  • 1
  • 19
  • 34
  • Why? Since C doesn't do compile-time bounds checking anyway (even when it can), what's the advantage over unsigned char *buf = 0x2000;? – Muzer Mar 23 '17 at 17:19
  • @Muzer it makes a difference at least for `sizeof`. And then depending on your compiler and the flags used, you can have compile-time or runtime bounds checking. – To마SE Jun 15 '17 at 03:43
12

To complete the other answers. The C99-Standard in 5.2.1.3:

Both the basic source and basic execution character sets shall have the following members:

the 26 uppercase letters of the Latin alphabet

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

the 26 lowercase letters of the Latin alphabet

a b c d e f g h i j k l m n o p q r s t u v w x y z

the 10 decimal digits

0 1 2 3 4 5 6 7 8 9

the following 29 graphic characters

! " # % & ' ( ) * + , - . / : ; < = > ? [ \ ] ^ _ { | } ~

All other characters maybe not even exist. (And should not be used)

But there is also this point in the Common extensions: Annex J, J.5.2:

Characters other than the underscore _, letters, and digits, that are not part of the basic source character set (such as the dollar sign $, or characters in national character sets) may appear in an identifier (6.4.2).

Which is basically what duskwuff already wrote.