Varun Chhangani
Varun Chhangani

Reputation: 1206

Can an underscore be used anywhere in an identifier in C?

I have a simple question: can I use an underscore anywhere in an identifier?

If yes please give an example, and if not please explain why.

Upvotes: 4

Views: 6231

Answers (4)

Elias Van Ootegem
Elias Van Ootegem

Reputation: 76408

Yes, underscores can be used as identifiers. Here's an example: The IOCCC winner of 1988

#define _ -F<00||--F-OO--;
int F=00,OO=00;main(){F_OO();printf("%1.3f\n",4.*-F/OO/OO);}F_OO()
{
            _-_-_-_
       _-_-_-_-_-_-_-_-_
    _-_-_-_-_-_-_-_-_-_-_-_
  _-_-_-_-_-_-_-_-_-_-_-_-_-_
 _-_-_-_-_-_-_-_-_-_-_-_-_-_-_
 _-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
 _-_-_-_-_-_-_-_-_-_-_-_-_-_-_
 _-_-_-_-_-_-_-_-_-_-_-_-_-_-_
  _-_-_-_-_-_-_-_-_-_-_-_-_-_
    _-_-_-_-_-_-_-_-_-_-_-_
        _-_-_-_-_-_-_-_
            _-_-_-_
}

As to why an underscore is allowed anywhere in an identifier, the answer is: the standard describes the syntax of an identifier to be comprised of at least one nondigit, followed by zero or more digit or nondigit chars
It also defines nondigits as being either one of the following chars:

_ 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

and digits are:

0 1 2 3 4 5 6 7 8 9

So following this rule, _____ is as valid an identifier as my_identifier or _000000AAFF___
But be aware that there is such a thing as reserved identifiers, and these, too, are described in the standard.
To answer the why question once again: because the standard said so ;-P

Upvotes: 5

Jens Gustedt
Jens Gustedt

Reputation: 78973

Yes, underscores may appear everywhere in an identifier, _a, _0_ or even _ are all valid identifiers. But there are a rules which of these you may use in your code and which are reserved for the "implementation" (= compiler and OS).

  • identifiers starting with an _ and a then a capital letter are reserved everywhere, you are not supposed to use them yourself. They can be used freely by the compiler or may be used by future language extensions, e.g this is why the official Boolean type in C is named _Bool.

  • identifiers starting with an _ and a then a lower case letter are reserved at file scope, so your are not allowed to name a function or global variable _toto or something alike. These could conflict with names of library functions, e.g.

  • identifiers with more than one _ in a row are reserved by C++ for name mangling. If you ever think of writing code in C that is callable from C++ you shouldn't use these.

  • identifiers ending with _t are reserved by POSIX for types. If you want to avoid future portability issues on new systems, avoid these, too.

There are a lot of other rules for reserved names, I only listed those that I remembered concerning _.

Upvotes: 10

Jayanth Ramachandran
Jayanth Ramachandran

Reputation: 8826

Rules for writing identifier

  1. An identifier can be composed of letters (both uppercase and lowercase letters), digits and underscore '_' only.

  2. The first letter of identifier should be either a letter or an underscore. But, it is discouraged to start an identifier name with an underscore though it is legal. It is because, identifier that starts with underscore can conflict with system names. In such cases, compiler will complain about it. Some system names that start with underscore are _fileno, _iob, _wfopen etc.

  3. There is no rule for the length of an identifier. However, the first 31 characters of an identifier are discriminated by the compiler. So, the first 31 letters of two identifiers in a program should be different.

Ex:

    int identifier;
    int _identifier;
    int new_identifier;

Upvotes: 5

Henkersmann
Henkersmann

Reputation: 1220

An Underscore is seen a normal letter/Character

MSDN Desciption of identifiers

Besides that there are often some Coding guidelines that restrict you in the use of the underscore, e.g. in a constructor the parameters are named _name where the name of the class variable is name.

Upvotes: 0

Related Questions