C preprocessors/macros 中的无效字符?
Invalid characters in C preprocessors/macros?
我不能在 宏 中使用哪些不同的无效字符?
似乎 #define TE$T 8
有效,所以 $
有效。
有人有无效字符列表吗? (或者相反,有效列表)。
是您的编译器允许使用 $
作为标识符。它不是标准的,如果您使用 -pedantic
或类似的编译器,您不应该期望其他编译器提供它或您的编译器允许它。
在C11草案的通用扩展附录中:
J.5.2 Specialized identifiers
1 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).
第 6.4.2 节 显示了哪些字符 每个 符合编译器必须
支持:
6.4.2 Identifiers
6.4.2.1 General
Syntax 1 identifier:
identifier-nondigit
identifier identifier-nondigit
identifier digit
identifier-nondigit:
nondigit
universal-character-name
other implementation-defined characters
nondigit: one of
_ 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
digit: one of
0 1 2 3 4 5 6 7 8 9
你应该把自己限制在那些。
宏名称只能由字母数字字符和下划线组成,即 'a-z'、'A-Z'、'0-9' 和 '_',并且第一个字符不能是数字。一些预处理器也允许使用美元符号字符“$”,但您不应该使用它。
也看看这个...What are the valid characters for macro names?
It seems that #define TE$T 8
is working, so $
is valid.
事实并非如此。 $
不是标准 C 中标识符的有效字符。一些编译器,例如 GCC,允许标识符中的 $
作为扩展。 (参见 Dollar Signs)
所以你问错了,宏中的名称没有什么特别的,预处理器所做的只是文本替换。
考虑somefille.c
#include<stdio.h>
#define NAM$ "SomeName"
int main(void)
{
printf("Name - %s\n",NAM$);
return 0;
}
用
编译以上内容
gcc -pedantic somefille.c -o somefille
给你
somefille.c:2:9: warning: '$' in identifier or number [enabled by default]
#define NAM$ "SomeName"
这个[ page ]说。
-pedantic
Issue all the warnings demanded by strict ISO C and ISO C++; reject all programs that use forbidden extensions, and some other
programs that do not follow ISO C and ISO C++. For ISO C, follows the
version of the ISO C standard specified by any -std option used.
根据严格的标准,宏名称中不能有空格,并且必须符合 C 变量遵循的相同命名规则:只能使用字母、数字和下划线 (_) 字符,第一个字符不能是数字。
问题是各种编译器不符合这一点。一个例子是我上面提到的 gcc。
话虽如此,仍然遵守以下规则:
宏名称不能以数字开头,如果违反此规定,您可能会得到如下错误:
error: macro names must be identifiers
宏名称不能包含空格。例如 #define FULL NAME "Your name"
给你:
error: ‘NAME’ undeclared (first use in this function)
我不能在 宏 中使用哪些不同的无效字符?
似乎 #define TE$T 8
有效,所以 $
有效。
有人有无效字符列表吗? (或者相反,有效列表)。
是您的编译器允许使用 $
作为标识符。它不是标准的,如果您使用 -pedantic
或类似的编译器,您不应该期望其他编译器提供它或您的编译器允许它。
在C11草案的通用扩展附录中:
J.5.2 Specialized identifiers
1 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).
第 6.4.2 节 显示了哪些字符 每个 符合编译器必须 支持:
6.4.2 Identifiers
6.4.2.1 General
Syntax 1 identifier:
identifier-nondigit
identifier identifier-nondigit
identifier digit
identifier-nondigit:
nondigit
universal-character-name
other implementation-defined characters
nondigit: one of
_ 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
digit: one of
0 1 2 3 4 5 6 7 8 9
你应该把自己限制在那些。
宏名称只能由字母数字字符和下划线组成,即 'a-z'、'A-Z'、'0-9' 和 '_',并且第一个字符不能是数字。一些预处理器也允许使用美元符号字符“$”,但您不应该使用它。
也看看这个...What are the valid characters for macro names?
It seems that
#define TE$T 8
is working, so$
is valid.
事实并非如此。 $
不是标准 C 中标识符的有效字符。一些编译器,例如 GCC,允许标识符中的 $
作为扩展。 (参见 Dollar Signs)
所以你问错了,宏中的名称没有什么特别的,预处理器所做的只是文本替换。
考虑somefille.c
#include<stdio.h>
#define NAM$ "SomeName"
int main(void)
{
printf("Name - %s\n",NAM$);
return 0;
}
用
编译以上内容gcc -pedantic somefille.c -o somefille
给你
somefille.c:2:9: warning: '$' in identifier or number [enabled by default]
#define NAM$ "SomeName"
这个[ page ]说。
-pedantic
Issue all the warnings demanded by strict ISO C and ISO C++; reject all programs that use forbidden extensions, and some other programs that do not follow ISO C and ISO C++. For ISO C, follows the version of the ISO C standard specified by any -std option used.
根据严格的标准,宏名称中不能有空格,并且必须符合 C 变量遵循的相同命名规则:只能使用字母、数字和下划线 (_) 字符,第一个字符不能是数字。
问题是各种编译器不符合这一点。一个例子是我上面提到的 gcc。
话虽如此,仍然遵守以下规则:
宏名称不能以数字开头,如果违反此规定,您可能会得到如下错误:
error: macro names must be identifiers
宏名称不能包含空格。例如
#define FULL NAME "Your name"
给你:error: ‘NAME’ undeclared (first use in this function)