Identifiers

Identifiers provide names for the following language elements:

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--+
                 '-_------'
 

Related information

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, under compilation with the c99 invocation command, the -qlanglvl=extc99 or -qlanglvl=stdc99 options or related pragmas (for C), or the-qlanglvl=ucs (for C and C++) option.

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 Identifiers that begin with a single underscore are reserved as identifiers with file scope in both the ordinary and tag namespaces.

C++ 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 C, the predefined identifier of foo is C::foo. If foo is defined within the body of main, the predefined identifier of foo is main::C::foo.

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>.

End of C++ only

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.

Related information

IBM extension

Assembly labels

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++ only You 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 diagramAssembly 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).

End of C++ only

The following are restrictions on the use of assembly labels:

Related information

End of IBM extension