43

Recently one of my friend 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.

But I don't know if @ and $ has any special meaning. If it does, could you please give example where it can be applied?

7
  • 30
    When you write your email address and how much salary you require?
    – this
    Jun 9, 2014 at 5:52
  • 1
    I'm pretty confident the answer is that they do not have any special meaning.
    – quant
    Jun 9, 2014 at 5:55
  • 3
    No, neither is part of the basic source character set, nor the basic execution character set.
    – chris
    Jun 9, 2014 at 5:55
  • 2
    @Arman For some definition of "special meaning". They're illegal outside of comments, string literals and character literals. Jun 9, 2014 at 8:56
  • 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 7, 2017 at 15:16

3 Answers 3

40

@ 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.

15
  • 3
    Of course if an implementation decides not to support that, then your code suddenly doesn't work.
    – chris
    Jun 9, 2014 at 5:56
  • 1
    Aren't the only valid characters for names a-zA-z_0-9 ?
    – this
    Jun 9, 2014 at 5:57
  • 1
    Corrected on $; forgot that was an extension.
    – user149341
    Jun 9, 2014 at 5:58
  • 5
    Note that neither @ nor $ are valid for operator overloading either although you might be able to do evil things with preprocessor macros somehow Jun 9, 2014 at 6:03
  • 1
    void $$$$ (void) { printf ("Hai"); } main() { $$$$(); } Error: suffix or operands invalid for `call'. How ? Jun 9, 2014 at 6:05
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

2
  • 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, 2017 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, 2017 at 3: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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.