What does the underscore mean/do? 
Author Message
 What does the underscore mean/do?

I have been programming with C and C++ for quite a few years now, and I
see underscores used in functions and structs like

typedef sturct _SPRITES {
  char blah;
  // etc....

Quote:
}

or _getch()  or  _kbhit

however, when you use them, you call them normally

SPRITES a_sprite;
while(kbhit = 0)

stuff like that....

what is with that?  and what about double underscores?

This is just to satisfy my own curiosity =)
-Tom



Sun, 25 Nov 2001 03:00:00 GMT  
 What does the underscore mean/do?

Quote:


> > I have been programming with C and C++ for quite a few years now, and I
> > see underscores used in functions and structs like
> > ...
> > what is with that?  and what about double underscores?

> The underscore in the function or structure name is used to usually
> denote some sort of system/library level construct. I assume it is
> used to reduce the chance of name a clash with normal programmer
> written functions.

> Jussi Jumppanen
> Author of: Zeus for Windows, Win32 (Brief, WordStar, Emacs) Text Editor
> "The C/C++, Java, HTML, Pascal, Cobol, fortran programmers text editor"
> Home Page: http://ourworld.compuserve.com/homepages/jussi/

alright thanks, I was just really curious as to why I see that in code
sometimes


Sun, 25 Nov 2001 03:00:00 GMT  
 What does the underscore mean/do?
I know it is kind of weird to expand on my own note, but just incase anyone
was wondering the same question.  I figured out that the leading underscore
signifies non-ANSI C compliance, in other words, compiler specific functions,
apparently _kbhit and _getch are among that group.  I had a feeling that is
what it was.

Jussi was right in the fact that it was a naming convention, it is for
compiler specific functions/structures.

-Tom



Sun, 25 Nov 2001 03:00:00 GMT  
 What does the underscore mean/do?

Quote:

> I have been programming with C and C++ for quite a few years now, and I
> see underscores used in functions and structs like

> <snip>
> what is with that?  and what about double underscores?

My understanding is that the underscore is just another character (along
with letters and digits) that's available to use in C symbols. Any special
significance in particular constructions is (or used to be) a matter of
convention/standard rather than of intrinsic function.

Morris Dovey
West Des Moines, Iowa USA



Sun, 25 Nov 2001 03:00:00 GMT  
 What does the underscore mean/do?


Quote:
>I know it is kind of weird to expand on my own note, but just incase anyone
>was wondering the same question.  I figured out that the leading underscore
>signifies non-ANSI C compliance, in other words, compiler specific functions,
>apparently _kbhit and _getch are among that group.  I had a feeling that is
>what it was.

That is not correct. The C language specifes that various groups of
identifiers are "reserved" for use by the implementation under various
circumstances. The simple principle is that the implementation (compiler)
uses identifiers from these groups for any extra bits and pieces it needs
and the program does not and this avoids clashes. There are two important
groups to consider here

1. Identifiers beginning with an underscore followed by either another
   underscore or an uppercase letter. These are reserved under all
   circumstances.

2. Identifiers beginning with an underscore but which are not in group 1
   (e.g. _, _xyz _012 etc.). These are essentially reserved at file scope.

You're probably still wondering why your statement is not correct. In
fact C does not require non-standard system functions to start with an
underscore as long as they aren't defined in one of the standard C headers.
For example POSIX defines functions like read(), fork(), pipe() etc.
in the header <unistd.h> which is not a standard C header. Also some
standard identifiers start with an underscore e.g. __DATE__ and _IOFBF.

It is true that some compilers name functions with underscores e.g. _kbhit
and _getch above. However this is quite unnecessary as far as the
definition of the C language is concerned, although it is legitimate to
do so.

Quote:
>Jussi was right in the fact that it was a naming convention, it is for
>compiler specific functions/structures.

Mostly it is for internal workings e.g. the members of a FILE datastructure
which the program itself should never see.

--
-----------------------------------------


-----------------------------------------



Sun, 25 Nov 2001 03:00:00 GMT  
 What does the underscore mean/do?

Quote:

> I have been programming with C and C++ for quite a few years now, and I
> see underscores used in functions and structs like
> ...
> what is with that?  and what about double underscores?

The underscore in the function or structure name is used to usually
denote some sort of system/library level construct. I assume it is
used to reduce the chance of name a clash with normal programmer
written functions.

Jussi Jumppanen
Author of: Zeus for Windows, Win32 (Brief, WordStar, Emacs) Text Editor
"The C/C++, Java, HTML, Pascal, Cobol, Fortran programmers text editor"
Home Page: http://ourworld.compuserve.com/homepages/jussi/



Mon, 26 Nov 2001 03:00:00 GMT  
 What does the underscore mean/do?
On Thu, 10 Jun 1999 08:04:28 +1000, Jussi Jumppanen

Quote:


>> I have been programming with C and C++ for quite a few years now, and I
>> see underscores used in functions and structs like
>> ...
>> what is with that?  and what about double underscores?

>The underscore in the function or structure name is used to usually
>denote some sort of system/library level construct. I assume it is
>used to reduce the chance of name a clash with normal programmer
>written functions.

In fact, such names are reserved for use by the implementation in
various contexts -- you're not allowed to use them when writing
standard C.  A name beginning with an underscore followed by
either another underscore or a capital letter is always reserved.
A name beginning with an underscore followed by a lower case
letter or a digit is reserved at file scope.

The implementation is not allowed to declare things with
nonreserved names in standard headers.  An implementation that
declares getch in <stdio.h> is not conforming; one that declares
_getch in <stdio.h> may be conforming (i.e., this will not make
it nonconforming).
--
Michael M Rubenstein



Mon, 26 Nov 2001 03:00:00 GMT  
 What does the underscore mean/do?
Lawrence,

It may be that I do not understand what you are saying exactly but I got out
my
"Standard C" book written by P.J. Plauger (ANSI C auditor I think) & Jim
Brodie (Chair of the ANSI C committee).
There is a section, in the appendix ( about page 169 I think), that talks
about reserved names.  Any name starting with
an underscore is reserved.  ANSI C compilers prepend an underscore to
function names when compiling.  My
understanding is that for a C compiler to be considered ANSI compatible all
functions that are not ANSI standard must
have the underscore as the first character.  Which meens that the function
in the library would actually have a double
underscore.

The bottom line is ... if you are writing functions don't use an
underscore(s) as the first letters of the function name.
Doing so would run the risk (however small) of clashing with a library
function.

For anyone interested ... the book I mentioned is out of print but you may
still be able to get it through amazon.com.

David Norton

Quote:



>>I know it is kind of weird to expand on my own note, but just incase
anyone
>>was wondering the same question.  I figured out that the leading
underscore
>>signifies non-ANSI C compliance, in other words, compiler specific
functions,
>>apparently _kbhit and _getch are among that group.  I had a feeling that
is
>>what it was.

>That is not correct. The C language specifes that various groups of
>identifiers are "reserved" for use by the implementation under various
>circumstances. The simple principle is that the implementation (compiler)
>uses identifiers from these groups for any extra bits and pieces it needs
>and the program does not and this avoids clashes. There are two important
>groups to consider here

>1. Identifiers beginning with an underscore followed by either another
>   underscore or an uppercase letter. These are reserved under all
>   circumstances.

>2. Identifiers beginning with an underscore but which are not in group 1
>   (e.g. _, _xyz _012 etc.). These are essentially reserved at file scope.

>You're probably still wondering why your statement is not correct. In
>fact C does not require non-standard system functions to start with an
>underscore as long as they aren't defined in one of the standard C headers.
>For example POSIX defines functions like read(), fork(), pipe() etc.
>in the header <unistd.h> which is not a standard C header. Also some
>standard identifiers start with an underscore e.g. __DATE__ and _IOFBF.

>It is true that some compilers name functions with underscores e.g. _kbhit
>and _getch above. However this is quite unnecessary as far as the
>definition of the C language is concerned, although it is legitimate to
>do so.

>>Jussi was right in the fact that it was a naming convention, it is for
>>compiler specific functions/structures.

>Mostly it is for internal workings e.g. the members of a FILE datastructure
>which the program itself should never see.

>--
>-----------------------------------------


>-----------------------------------------



Fri, 30 Nov 2001 03:00:00 GMT  
 What does the underscore mean/do?


Quote:
>Lawrence,

>It may be that I do not understand what you are saying exactly but I got out
>my
>"Standard C" book written by P.J. Plauger (ANSI C auditor I think) & Jim
>Brodie (Chair of the ANSI C committee).
>There is a section, in the appendix ( about page 169 I think), that talks
>about reserved names.  Any name starting with
>an underscore is reserved.

That is true. However not all are reserved in all circumstances.

Quote:
> ANSI C compilers prepend an underscore to
>function names when compiling.

No, there is no requirement for them to do this and many don't. All the
standard does is define what programs are valid and how valid programs
should behave (sometimes precisely, sometimes giving the implementation
some choice). The standard does not concern itself with the mechanisms
of translation used by a particular implementation, either the
compiled code does the right thing or it doesn't.

Quote:
> My
>understanding is that for a C compiler to be considered ANSI compatible all
>functions that are not ANSI standard must
>have the underscore as the first character.

No. The standard does require that standard headers such as <stdio.h>
only define identifiers specified by the standard and in addition may use
identifiers from the reserved namespace. When a program includes a
non-standard system header (e.g. <unistd.h>, <windows.h>, <dos.h> etc.)
then the behaviour of the program is undefined as far as the standard
is concerned - it says nothing about what may or may not be in such
headers.

One thing the implementation must do however is correctly translate
strictly conforming programs. So a program such as this:

int close(void);

int main(void)
{
     return close();

Quote:
}

int close(void)
{
    return 0;

Quote:
}

must work. If there is a function called close() in the system library it
must not interfere with the correct operation of this program. On the
other hand changing the program to

#include <unistd.h>    /* The POSIX header for close() */

int close(void);

int main(void)
{
     return close();

Quote:
}

int close(void)
{
    return 0;

Quote:
}

could cause the program to fail since as far as C is concerned including
<unistd.h> causes undefined behaviour (even though the search strategy
for searching for headers is implkementation-defined). So something
like

#include <unistd.h>    /* The POSIX header for close() */

int main(void)
{
     close(0);
     return 0;

Quote:
}

can legitimately call a system library function that is not a standard C
function. In fact even

int main(void)
{
     close(0);
     return 0;

Quote:
}

can do so since calling a function which is undefined (close() is undefined
as far as standard C is concerned) has undefined behaviour. When
undefined behaviour anything whatsoever that the compiler does is
correct as far as the standard is concerned.

Quote:
>Which meens that the function
>in the library would actually have a double
>underscore.

The close() function in the system library I have here is, according
to the de{*filter*}, called close i.e. no underscores. This is perfectly
compatible with ANSI C as long as the library function is ignored if
the program defines an object or function with external linkage called
close. That is the case here (it is sometimes called weak linking).

It is certainly true that on systems that don't support weak linking
there may be a problem if the system libaries use non-reserved
identifiers. One solution there is to not put such functions in the main
library but only link them when specifically requested to.

Quote:
>The bottom line is ... if you are writing functions don't use an
>underscore(s) as the first letters of the function name.
>Doing so would run the risk (however small) of clashing with a library
>function.

The bottom line is that the implementation can do anything it likes
as long as it doesn't interfere with the correct operation of a strictly
conforming program. Strictly conforming programs cannot define reserved
identifiers in user code which leaves them free for the implementation
to use. However strictly conforming programs can't include non-standard
headers (<unistd.h> etc.) either or call non-standard functions that
the program code itself doesn't define. It is these last points that
give the flexibility I'm talking about.

--
-----------------------------------------


-----------------------------------------



Sat, 01 Dec 2001 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. System.Threading.Timer , am I doing this correctly ?

2. Threading problem. I'm doing something wrong

3. How Should I go about doing this?

4. static member inheritance not doing what I want...

5. What is the common methjod of doing this in C#

6. Microsoft shouldn't have done this

7. Doing relative paths in ATL Server?!?

8. Doing .NET with C++

9. Wot am I doing rong??

10. Difference between two different ways of doing things

11. Can this be done?

12. Creating a Virtual directory through a program as done by VS.NET IDE

 

 
Powered by phpBB® Forum Software