Ok, I know that leading underscores in identifiers are reserved for use by the implementation. But I forget (if I ever knew to begin with), is there any rule against trailing underscores? And is there any such rule in C?
Ok, I know that leading underscores in identifiers are reserved for use by the implementation. But I forget (if I ever knew to begin with), is there any rule against trailing underscores? And is there any such rule in C?
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
Well, i've come across a few professionals who use trailing underscores in their code. It's usually used to signify a member variable of a class. Others prefer using 'm_' for that purpose.
Are there rules for it in C? I doubt it.
As long as your code is clear and consistent, then there's no prob, imo. I would probably only use it to signify private/protected member variables in classes.
By trailing underscore I mean:It's usually used to signify a member variable of a class. Others prefer using 'm_' for that purpose.
void dosomething_();
Is there any regulation against that?
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
That's what i was referring to, but i've never seen someone using it for function names, only for variables. I don't think that there are any regulations for these kinds of things, but rather programmer preference. If you have a good enough reason for using it, then by all means, go ahead.
Well, I wouldn't ever use it for either functions or variable names... but since leading underscores are taboo, I thought perhaps trailing ones were too (since they look so weird ). The reason I'm asking is actually because my header inclusion guards are in the form:
#ifndef HEADER_H_
#define HEADER_H_
And I wanted to be sure that the trailing underscore isn't something terrible. Although I've never heard of using a trailing underscore to indicate a member variable... I guess we learn something new every day.
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
As far as I know, trailing underscores are not a standards problem - they're not reserved
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
>Is there any regulation against that?
Aside from looking weird, no.
My best code is written with the delete key.
Here is a good overview:
I agree it is not good practice to start an identifier with an underscore (as mentioned above). But it is legal to do so.
WHAT IS AN IDENTIFIER?
Before you can do anything in any language, you must know how to name an identifier. An identifier is used for any variable, function, data definition, etc. In the C programming language, an identifier is a combination of alphanumeric characters, the first being a letter of the alphabet or an underline, and the remaining being any letter of the alphabet, any numeric digit, or the underline.
Two rules must be kept in mind when naming identifiers.
The case of alphabetic characters is significant. Using INDEX for a variable name is not the same as using index and neither of them is the same as using InDeX for a variable name. All three refer to different variables.
According to the ANSI-C standard, at least 31 significant characters can be used and will be considered significant by a conforming ANSI-C compiler. If more than 31 are used, all characters beyond the 31st may be ignored by any given compiler.
WHAT ABOUT THE UNDERLINE?
The underline can be used as part of a variable name, and adds greatly to the readability of the resulting code. It is used by some, but not all, experienced C programmers. A few underlines are used for illustration in this tutorial. Since most compiler writers use the underline as the first character for variable names internal to the system, you should refrain from using the underline to begin an identifier to avoid the possibility of a name clash. To get specific, identifiers with two leading underscores are reserved for the compiler as well as identifiers beginning with a single underscore and using an upper case alphabetic character for the second. If you make it a point of style to never use an identifier with a leading underline, you will not have a naming clash with the system.
Just to point out the current texts on C, most point out that it is not recommended to start an identifier with underscore, but do have examples mention that it is valid to start an identifier with an underscore.
mr. C
Mr. C: Author and Instructor
Your post pertains to leading underscores, but this thread is about the use of trailing underscores.
Code:char _identifier; // leading char identifier_; // trailing
>>identifiers beginning with a single underscore and using an upper case alphabetic character
Interesting to know. So now I know why some compiler identifiers start with single and some with double
>>but this thread is about the use of trailing underscores.
Well, I suppose the quote was relevant in that it excludes any mention of trailing underscores being illegal.
Thanks for the replies everyone
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
>identifiers with two leading underscores are reserved for the compiler as well as identifiers
>beginning with a single underscore and using an upper case alphabetic character for the second.
Not to forget that any identifier with a leading underscore in either the ordinary or tag name spaces at file scope is reserved.
My best code is written with the delete key.
>>in either the ordinary or tag name spaces at file scope
Is "name spaces" distinct from "namespace's"? And what are ordinary and tag namespaces? And by file scope, do you mean declared globally?
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
>Is "name spaces" distinct from "namespace's"?
Yes. And no. And yes. But also no. Kind of. The concept is the same, but a name space is what lets you do this:
While a namespace is what lets you do this:Code:struct name { int stuff; }; int main() { name name; }
So it's kind of the same, but not. Yet it is.Code:namespace my { struct name { int stuff; }; } namespace your { struct name { int stuff; }; } int main() { my::name me; your::name you; }
>And what are ordinary and tag namespaces?
>And by file scope, do you mean declared globally?Code:int ordinary; struct tag { int stuff; };
Yes, if you want to be uncouth about it.
My best code is written with the delete key.
>>but a name space is what lets you do this:
Whoa, you can do that??...
It took me a long time to realize the possibility that you weren't mocking me. Do you always speak in parables? So the "ordinary name space" (not namespace) is the category of identifier which designates an individual variable, while the "tag name space" is the category of identifier which designates a type or struct?Code:int ordinary; struct tag { int stuff; };
So returning to:
>>but a name space is what lets you do this:
You're saying that because the 'name' designating the struct type is in the tag name space, it doesn't conflict with the 'name' designating the name object called 'name'? I'd always assumed that it would be illegal, since you can't do it with built-in datatypes like int.
And coming around in a full circle, about how name space is sort of like namespace but not, based on the assumption that all my interpretation above is correct... it's because both allow you to use the same name for different things as long as they are in different name spaces/namespaces, but namespaces allow you to reuse names within the same name space and name spaces allow you to reuse names within the same namespace?
Please correct me if I'm wrong
**P.S. And are there even any other name spaces than ordinary and tag?
Last edited by Hunter2; 09-12-2004 at 01:40 PM.
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
>Whoa, you can do that??...
If not then everyone was right when they said I was a looney.
>It took me a long time to realize the possibility that you weren't mocking me.
>Do you always speak in parables?
Only when there's a possiblity that I might be mocking you.
>So the "ordinary name space" (not namespace) is the category of identifier which designates an individual variable
Well, the ordinary name space is the "other" location for everything that doesn't have its own specialized name space, such as struct/union/enum/class tags, members and labels.
>while the "tag name space" is the category of identifier which designates a type or struct?
That sounds about right.
>I'd always assumed that it would be illegal, since you can't do it with built-in datatypes like int.
But the built-in types are not identifiers, they're keywords.
>but namespaces allow you to reuse names within the same name space and name spaces allow you to reuse names within the same namespace?
Now you're starting to word your explanations like I do, and that's frightening.
>**P.S. And are there even any other name spaces than ordinary and tag?
Labels, tags (only one name space even though there could have been four), members, and ordinary. Even more confusingly, a namespace is a name space, but a name space is not necessarily a namespace.
My best code is written with the delete key.