C 标识符C Identifiers

“Identifiers”或“symbols”是您为程序中的变量、类型、函数和标签提供的名称。"Identifiers" or "symbols" are the names you supply for variables, types, functions, and labels in your program. 标识符名称在拼写和大小写上必须与任何关键字都不同。Identifier names must differ in spelling and case from any keywords. 不能将关键词(C 或 Microsoft)用作标识符;将它们保留以用于特殊用途。You cannot use keywords (either C or Microsoft) as identifiers; they are reserved for special use. 通过在变量、类型或函数的声明中指定标识符来创建标识符。You create an identifier by specifying it in the declaration of a variable, type, or function. 在此示例中,result 是整型变量的标识符,mainprintf 是函数的标识符名称。In this example, result is an identifier for an integer variable, and main and printf are identifier names for functions.

#include <stdio.h>  

int main()  
{  
    int result;  

    if ( result != 0 )  
        printf_s( "Bad file handle\n" );  
}  

声明后,您可在以后的程序语句中使用标识符来引用关联值。Once declared, you can use the identifier in later program statements to refer to the associated value.

特殊种类的标识符(称为语句标签)可在 goto 语句中使用。A special kind of identifier, called a statement label, can be used in goto statements. 声明和类型中介绍了声明,goto 和标记语句中介绍了语句标签。)(Declarations are described in Declarations and Types Statement labels are described in The goto and Labeled Statements.)

语法Syntax

identifier:identifier:
nondigitnondigit

identifier nondigitidentifier nondigit

identifier digitidentifier digit

nondigit:以下项之一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

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 ZA 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:以下项之一digit: one of
0 1 2 3 4 5 6 7 8 90 1 2 3 4 5 6 7 8 9

标识符名称的第一个字符必须是 nondigit(即,第一个字符必须是下划线、大写字母或小写字母)。The first character of an identifier name must be a nondigit (that is, the first character must be an underscore or an uppercase or lowercase letter). ANSI 允许外部标识符名称包含 6 个有效字符,内部(一个函数中)标识符名称包含 31 个有效字符。ANSI allows six significant characters in an external identifier's name and 31 for names of internal (within a function) identifiers. 外部标识符(在全局范围内或使用存储类 extern 进行声明)可能要受其他命名限制的约束,因为这些标识符必须由其他软件(如链接器)处理。External identifiers (ones declared at global scope or declared with storage class extern) may be subject to additional naming restrictions because these identifiers have to be processed by other software such as linkers.

Microsoft 专用Microsoft Specific

尽管 ANSI 允许外部标识符名称包含 6 个有效字符,内部标识符(在一个函数内)名称包含 31 个有效字符,但 Microsoft C 编译器允许内部或外部标识符名称包含 247 个字符。Although ANSI allows 6 significant characters in external identifier names and 31 for names of internal (within a function) identifiers, the Microsoft C compiler allows 247 characters in an internal or external identifier name. 如果您不担心 ANSI 兼容性,则可使用 /H(限制外部名称的长度)选项将此默认值修改为更小或更大的数字。If you aren't concerned with ANSI compatibility, you can modify this default to a smaller or larger number using the /H (restrict length of external names) option.

结束 Microsoft 专用END Microsoft Specific

C 编译器会将大写和小写字母视为不同的字符。The C compiler considers uppercase and lowercase letters to be distinct characters. 利用此功能(称为“区分大小写”),您可以创建拼写相同但一个或多个字母的大小写不同的不同标识符。This feature, called "case sensitivity," enables you to create distinct identifiers that have the same spelling but different cases for one or more of the letters. 例如,下列每个标识符都是唯一的:For example, each of the following identifiers is unique:

add  
ADD  
Add  
aDD  

Microsoft 专用Microsoft Specific

请勿为标识符选择以两条下划线开头或者以一条下划线后跟一个大写字母开头的名称。Do not select names for identifiers that begin with two underscores or with an underscore followed by an uppercase letter. ANSI C 标准允许保留以这些字符组合开头的标识符名称以供编译器使用。The ANSI C standard allows identifier names that begin with these character combinations to be reserved for compiler use. 不应将一条下划线和一个小写字母作为前两个字母来命名具有五级范围的标识符。Identifiers with file-level scope should also not be named with an underscore and a lowercase letter as the first two letters. 以这些字符开头的标识符名称也将保留。Identifier names that begin with these characters are also reserved. 按照约定,Microsoft 使用下划线和大写字母作为宏名称的开头,并使用双下划线作为 Microsoft 特定关键字名称的开头。By convention, Microsoft uses an underscore and an uppercase letter to begin macro names and double underscores for Microsoft-specific keyword names. 若要避免任何命名冲突,请始终选择不是以一条或两条下划线开头的标识符名称,或选择以一条下划线后跟一个大写字母开头的名称。To avoid any naming conflicts, always select identifier names that do not begin with one or two underscores, or names that begin with an underscore followed by an uppercase letter.

结束 Microsoft 专用END Microsoft Specific

下面是符合 ANSI 或 Microsoft 命名限制的有效标识符的示例:The following are examples of valid identifiers that conform to either ANSI or Microsoft naming restrictions:

j  
count  
temp1  
top_of_page  
skip12  
LastNum  

Microsoft 专用Microsoft Specific

尽管默认情况下源文件中的标识符区分大小写,但对象文件中的符号不区分大小写。Although identifiers in source files are case sensitive by default, symbols in object files are not. Microsoft C 将编译单元中的标识符视为区分大小写。Microsoft C treats identifiers within a compilation unit as case sensitive.

Microsoft 链接器区分大小写。The Microsoft linker is case sensitive. 您必须根据情况一致地指定所有标识符。You must specify all identifiers consistently according to case.

“源字符集”是可能出现在源文件中的合法字符集。The "source character set" is the set of legal characters that can appear in source files. 对于 Microsoft C,源集是标准 ASCII 字符集。For Microsoft C, the source set is the standard ASCII character set. 源字符集和执行字符集包含用作转义序列的 ASCII 字符。The source character set and execution character set include the ASCII characters used as escape sequences. 有关执行字符集的信息,请参阅字符常量See Character Constants for information about the execution character set.

结束 Microsoft 专用END Microsoft Specific

标识符具有“范围”(在程序中发现标识符的区域)和“链接”(确定其他范围中的同一名称是否引用同一标识符)。An identifier has "scope," which is the region of the program in which it is known, and "linkage," which determines whether the same name in another scope refers to the same identifier. 生存期、范围、可见性和链接中介绍了这些主题。These topics are explained in Lifetime, Scope, Visibility, and Linkage.

另请参阅See Also

C 的元素Elements of C