C/C++中的'@'和'$'代表什么?

4
到目前为止,在MinGW中,"$"似乎只是一个基本字符(如'a'),可以用于名称(变量、函数等)。但它看起来并不像一个。我是否错过了使用"$"作为名称一部分的隐藏功能?
int $m = 2;
printf("$m = %i", $m);

控制台输出:

$m = 2

然而,当'@'符号用作字符时会产生错误。错误信息为:error: stray '@' in program我猜这意味着它被用于某些特殊的用途?维基百科MSDN都没有提到这两个字符。同时,谷歌搜索可以找到大量与“symbol”或“C”有关的匹配结果。

1
https://dev59.com/IGsz5IYBdhLWcg3wZm7Q - im so confused
@ 符号通常用于一些元编程目的。 - chris
1个回答

8

在标准C语言的字符集中,$@都不是合法的字符(参见C11标准的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

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

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

C++标准对于字符集的规定与此相似(2.2 字符集,第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
_ { } [ ] # ( ) < > % : ; . ? * + - / ^ & | ∼ ! = , \ " ’
所以,您是否可以或不能使用它们(全面或仅限于特定用途)是取决于您的实现方式的。
在您的情况下,听起来您可能正在使用GCC,该编译器允许使用$作为标识符的一个扩展,但不允许使用@,这可能是因为GCC还会编译Objective-C代码,其中@具有特殊含义。
根据GCC文档的说明:
在GNU C中,您通常可以在标识符名称中使用$符号。这是因为许多传统的C实现允许此类标识符。但是,在一些目标机器上不支持在标识符中使用$符号,这通常是因为目标汇编程序不允许这样做。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接