Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the '@' and '$' for in C/C++? [duplicate]

Tags:

c++

c

So far, in MinGW, the '$' seems to be just a basic character (like 'a'), that can be used in names (variables, functions, etc.) But it doesn't look like one. Is there some hidden feature of using '$' as part of a name that I'm missing?

int $m = 2;
printf("$m = %i", $m);

Console Output:

$m = 2

However the '@' symbol produces an error when used as a character. error: stray '@' in program I assume that mean's that it's used for something? Wikipedia, and MSDN don't mention either character. And Google gives plenty of matches for "symbol" or "C".

like image 416
Wolfgang Skyler Avatar asked Feb 15 '23 10:02

Wolfgang Skyler


1 Answers

Neither $ or @ are part of standard C's character set (C11 5.2.1 Character sets, paragraph 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

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

the space character, and control characters representing horizontal tab, vertical tab, and form feed.

The C++ standard says about the same (2.2 Character sets, paragraph 1):

The basic source character set consists of 96 characters: the space character, the control characters representing horizontal tab, vertical tab, form feed, and new-line, plus the following 91 graphical characters:

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
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
0 1 2 3 4 5 6 7 8 9
_ { } [ ] # ( ) < > % : ; . ? * + - / ^ & | ∼ ! = , \ " ’

So if you can or can't use them (at all or even for a specific purpose) it's up to your implementation.

In your case it sounds like you're probably using GCC, which allows $ in identifiers as an extension, but doesn't allow @ - probably because GCC also compiles Objective-C code, where @ has special meaning.

From the GCC documentation:

In GNU C, you may normally use dollar signs in identifier names. This is because many traditional C implementations allow such identifiers. However, dollar signs in identifiers are not supported on a few target machines, typically because the target assembler does not allow them.

like image 56
Carl Norum Avatar answered Feb 24 '23 11:02

Carl Norum