Identifiers

Identifiers provide names for the following language elements:
  • Functions
  • Objects
  • Labels
  • Function parameters
  • Macros and macro parameters
  • Type definitions
  • Enumerated types and enumerators
  • Structure and union names
  • C++ only Classes and class members
  • C++ only Templates
  • C++ only Template parameters
  • C++ only Namespaces
An identifier consists of an arbitrary number of letters, digits, or the underscore character in the form:
Read syntax diagramSkip visual syntax diagram
               .------------.   
               V            |   
>>-+-letter-+----+-letter-+-+----------------------------------><
   '-_------'    +-digit--+     
                 '-_------'     

Characters in identifiers

The first character in an identifier must be a letter or the _ (underscore) character; however, beginning identifiers with an underscore is considered poor programming style.

The compiler distinguishes between uppercase and lowercase letters in identifiers. For example, PROFIT and profit represent different identifiers. If you specify a lowercase a as part of an identifier name, you cannot substitute an uppercase A in its place; you must use the lowercase letter.

The universal character names for letters and digits outside of the basic source character set are allowed in C++ and at the C99 language level. C++ only In C++, you must compile with the -qlanglvl=ucs option for universal character name support.

IBM extension The dollar sign can appear in identifier names when compiled using the -qdollar compiler option or at one of the extended language levels that encompasses this option.

Reserved identifiers

Identifiers with two initial underscores or an initial underscore followed by an uppercase letter are reserved globally for use by the compiler.

C only Identifiers that begin with a single underscore are reserved as identifiers with file scope in both the ordinary and tag namespaces.

C++ only Identifiers that begin with a single underscore are reserved in the global namespace.

Although the names of system calls and library functions are not reserved words if you do not include the appropriate headers, avoid using them as identifiers. Duplication of a predefined name can lead to confusion for the maintainers of your code and can cause errors at link time or run time. If you include a library in a program, be aware of the function names in that library to avoid name duplications. You should always include the appropriate headers when using standard library functions.

The __func__ predefined identifier

The C99 predefined identifier __func__ makes a function name available for use within the function. Immediately following the opening brace of each function definition, __func__ is implicitly declared by the compiler. The resulting behavior is as if the following declaration had been made:
static const char __func__[] = "function-name";
where function-name is the name of the lexically-enclosing function. The function name is not mangled.

C++ only The function name is qualified with the enclosing class name or function name. For example, if foo is a member function of class X, the predefined identifier of foo is X::foo. If foo is defined within the body of main, the predefined identifier of foo is main::X::foo.

C++ only The names of template functions or member functions reflect the instantiated type. For example, the predefined identifier for the template function foo instantiated with int, template<classT> void foo() is foo<int>.

For debugging purposes, you can explicitly use the __func__ identifier to return the name of the function in which it appears. For example:

#include <stdio.h>

void myfunc(void)    {  
         printf("%s\n",__func__);  
         printf("size of __func__ = %d\n", sizeof(__func__));  
}  

int main() {  
     myfunc();  
} 

The output of the program is:

myfunc  
size of __func__ = 7 

When the assert macro is used inside a function definition, the macro adds the name of the enclosing function on the standard error stream.

Assembly labels (IBM extension)

The compiler binds each non-static external variable and function name in the source code to a name that it generates in the object file and any assembly code that is emitted. For compatibility with GCC, the compiler implements an extension to standard C and C++ that allows you to specify the name to be used in the object file and assembly code, by applying an assembly label to the declaration of a global variable or function prototype. You can also define names that do not start with an underscore even on systems where an underscore is normally prepended to the name of a function or variable.

C++ onlyYou can use assembly labels with member functions, and functions and variables that are declared in namespaces other than the global namespace.

Read syntax diagramSkip visual syntax diagram
Assembly label syntax

>>-declarator--+-asm-----+--(--"--string_literal--"--)---------->
               +-__asm__-+                               
               '-__asm---'                               

>--+-------------+---------------------------------------------><
   '-initializer-'   

The string_literal is a valid assembly name that is to be bound to the given object or function. For a label applied to a function declaration, the name must specify an existing function that is defined in any compilation unit; if no definition is available, a link-time error will occur. For a label applied to a variable declaration, no other definition is required.

The following are examples of assembly label specifications:
void func3() __asm__("foo3");    
int i __asm("abc");             
char c asm("abcs") = 'a';					

C++ only To distinguish between overloaded functions, XL C++ mangles function names in the object file. Therefore, if you use an assembly label to map a function name, you must use the mangled name of the target function. Furthermore, you must ensure that an assembly label name that you specify for a variable does not conflict with any mangled name. Alternatively, you can prevent name mangling on a target function by declaring it as having C linkage; for more information, see Name mangling (C++ only).

The following restrictions apply to the use of assembly labels:
  • Assembly labels cannot be specified on local or static variables.
  • The same assembly label name cannot be applied to multiple identifiers (in C++, this is the name after mangling) in the same compilation unit.
  • The assembly label name cannot be the same as any other global identifier name (in C++, the name after mangling) in the same compilation unit, unless the label name and identifier name are used for the same variable or function declaration.
  • The assembly label cannot be specified on typedef declarations.
  • An assembly label cannot be the same as a name specified on a different variable or function by a previous #pragma map directive. Similarly, the map name specified by a #pragma map directive cannot be the same as a name specified by a previous assembly label on a different variable or function.
  • You cannot apply an assembly label to an identifier that has been mapped to a different name by a #pragma map directive on a previous declaration of that variable or function. Similarly, you cannot specify a #pragma map directive on an identifier that has previously been remapped by an assembly label.
  • C onlyIf you apply different labels to multiple declarations of the same variable or function, the first specification is honored, and all subsequent assembly labels are ignored with a warning.
  • C++ onlyYou cannot apply an assembly label to any of the following:
    • member variable declarations
    • friend declarations
    • template function and member declarations, or any declarations contained within a template
    • virtual member functions
    • constructors and destructors