the role of underscores in C 
Author Message
 the role of underscores in C

Hello,

I noticed, that in some header files an underscore '_' or two
underscores '__' stand in front of an identifier. What does it mean ?

For example I write down a header 'test.c' in which a line like
 extern int _test_function(int arg);
appears. In my application I include this header and try to use the
above function with
 result = test_function(23);

Is this correct and does this function call use the above declared
function ? If this is true, why does anybody use the underscore,
because it would be the same without it, or not ?

--
       Thank you for your attention,
                                     Michael Risse



Sun, 13 Aug 2000 03:00:00 GMT  
 the role of underscores in C



Quote:
>Hello,

>I noticed, that in some header files an underscore '_' or two
>underscores '__' stand in front of an identifier. What does it mean ?

It means undefined behavior is triggered, unless the files you were looking
at were part of the C implementation (system headers).

Identifiers with leading underscores---more precisely, identifiers that begin
with two underscores, or a single underscore and a capital letter---are
reserved for the use of the implementation.

Any program which uses such identifiers, risks interfering with the C
implementation in unpredictable ways.

Quote:
>For example I write down a header 'test.c' in which a line like
> extern int _test_function(int arg);
>appears. In my application I include this header and try to use the
>above function with
> result = test_function(23);

That is obviously wrong. The identifiers don't match. But since undefined
behavior occurs, it might work by dumb luck!


Sun, 13 Aug 2000 03:00:00 GMT  
 the role of underscores in C



Quote:
>Hello,

>I noticed, that in some header files an underscore '_' or two
>underscores '__' stand in front of an identifier. What does it mean ?

>For example I write down a header 'test.c' in which a line like
> extern int _test_function(int arg);
>appears. In my application I include this header and try to use the
>above function with
> result = test_function(23);

>Is this correct and does this function call use the above declared
>function ? If this is true, why does anybody use the underscore,
>because it would be the same without it, or not ?

>--
>       Thank you for your attention,
>                                 Michael Risse

First, your header should have the .h extension not .c

the decl for test_function(25) should be
extern test_function(int arg);

the _ can be included as part of the name, but should remain with the name, it
has no special meaning to C beyond that of any other "normal" character.

To the linker, OTOH, underscores can mean different things. Often underscores
are included before symbol names since certain binary formats demand
this...unless you are calling functions from assembly files, you shouldn't have
to worry about this. Just think of the underscore as an ordinary character like
'a', 'b', or 'c'.....

Oftern you will see functions or #define's use the underscore to indicate
ceratin "human readable" meanings. This is oart of a coding style only..

for example, I use underscores for #define's that prevent reinclusion of
headers...example:

the file foo.h

#ifndef __FOO_H__
#define __FOO_H__

/* stuff here */

#endif /* __FOO_H__ */
--
Trevor R.H. Clarke
Computer Science House
Rochester Institute of Technology



Sun, 13 Aug 2000 03:00:00 GMT  
 the role of underscores in C


|> the _ can be included as part of the name, but should remain with
|> the name, it has no special meaning to C beyond that of any other
|> "normal" character.

In certain circumstances, such as how the original poster was using
the underscore, it is significant.  As others have already no doubt
pointed out, identifiers that begin with either two underscores or
an underscore followed by an uppercase letter are reserved for use
by the implementation.  Furthermore, any identifier beginning with
an underscore (whether it falls into either of the categories above
or not) is always reserved for use as an identifier with file scope.

|> for example, I use underscores for #define's that prevent reinclusion
|> of headers...example:
|>
|> the file foo.h
|>
|> #ifndef __FOO_H__
|> #define __FOO_H__
|>
|> /* stuff here */
|>
|> #endif /* __FOO_H__ */

And thus you've invoked undefined behavior; the identifier __FOO_H__
does not "belong" to you.  It's better to simply use FOO_H or any
other descriptive identifier that doesn't infringe on the namespace
of the implementation.  Many C programmers get the idea for using
double-underscore prefixes for such "idempotency #defines" from
looking at system headers.  There's an important difference, though;
the identifiers are allowable in system headers because they are
part of the implementation.  The same can't be said of your headers.

Regards,

--
Chris Engebretson - Raytheon STX Corporation | Ph#: (605)594-6829
USGS EROS Data Center, Sioux Falls, SD 57198 | Fax: (605)594-6940

Opinions are not those of  Raytheon Systems Company  or the USGS.



Sun, 13 Aug 2000 03:00:00 GMT  
 the role of underscores in C

Quote:

> I noticed, that in some header files an underscore '_' or two
> underscores '__' stand in front of an identifier. What does it mean ?

Underscores are allowed in the names, including at the start.
However, there are restrictions on who should use them.

Quote:
> For example I write down a header 'test.c' in which a line like
>  extern int _test_function(int arg);
> appears. In my application I include this header and try to use the
> above function with
>  result = test_function(23);

No.  The underscore is part of the name, so this doesn't work.

Quote:
> Is this correct and does this function call use the above declared
> function ? If this is true, why does anybody use the underscore,
> because it would be the same without it, or not ?

The names beginning __ are set aside by the language for use by
implementors.  User programs aren't supposed to use them.  This
ensures that a compiler can have internal procedures in the standard
library without worrying about users creating procedures of the same
name.  _ is also reserved.  IIRC it's not reserved for anyone in
particular, but it tends to get used by third party libraries - again,
in order to keep the user namespace as clean as possible.

Unless you happen to be implementing a C implementation or library,
don't worry about them.

--

            http://www.tardis.ed.ac.uk/~broonie/
            http://www.ed.ac.uk/~filmsoc/



Sun, 13 Aug 2000 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Newbie: separate big .cs file into small .cs files

2. discussion of the role of MFC/ATL

3. role of VC

4. C coder needed for Role Playing EWtoo style talker

5. Object Role Modeling

6. Elite Software Developer Role

7. Role of semicolon in C

8. DAO and SQL Server application role

9. Role of ADVAPI32.DLL

10. Role of windows in Windows

11. Newbie: Role of Runtime library

12. MFC's role in .NET?

 

 
Powered by phpBB® Forum Software