pmor
pmor

Reputation: 6277

Are user-defined identifiers beginning with a single underscore non-problematic?

Is this identifier non-problematic:

_var

C11, 7.1.3 Reserved identifiers, 1

All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces.

Does it follow from this that user-defined identifiers beginning with a single underscore are non-problematic?

Upvotes: 1

Views: 159

Answers (4)

Eric Postpischil
Eric Postpischil

Reputation: 222272

Does it follow from this that user-defined identifiers beginning with a single underscore are non-problematic?

No, that list item merely tells you certain things are problematic. It makes no statement that other things are non-problematic.

The same paragraph tells you that all identifiers listed in the header subclauses are reserved if header that declares them is included, possibly for any use, so they are problematic. There are additional issues listed in that paragraph.

C 2018 6.4.2.1 5 and 6 tell you that identifiers longer than the minimums listed in 5.2.4.1 (63 characters for internal identifiers, 31 for external) may be a problem; the behavior is not defined if two identifiers differ only beyond the limit of significant characters the implementation imposes.

C 2018 6.4.2 also allows identifiers with implementation-defined characters, so such identifiers may work in some implementations and not others.

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 310910

No, there is a problem.

You forgot to include one more quote

All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.

So you may not declare an identifier beginning with one underscore followed by an uppercase letter.

In general it is a bad style of programming using identifiers starting with underscore because the reader of the code can think that this identifier is reserved by the implementation.

Upvotes: 2

Abhijeet Murmu
Abhijeet Murmu

Reputation: 1

Inside the main function or user defined function, you can write like that

You won't get any compile error.

int main()
{
 int _var;
 float _var1;
}

Upvotes: 0

tstanisl
tstanisl

Reputation: 14107

Yes. As long as:

  • are at block scope (includes enum/struct/union tags)
  • OR are struct/union members
  • OR are function parameters
  • _ is followed by neither capital nor another underscore

E.g.

struct X { int _a; };
int main() { int _a; }
void foo(int _a);

Upvotes: 2

Related Questions