Leading "_" In Function Names

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
chris

Leading "_" In Function Names

Post by chris »

I'm just wondering why some functions in certain apps/os's have a leading _ (underscore) in there names.

i.e. int _out_char(unsigned char c)
Tim

Re:Leading "_" In Function Names

Post by Tim »

Because the author of the code likes underscores.

Technically, the C standard dictates that the only identifiers that can begin with an underscore are vendor-specific extensions to the C libraries. For example, the Microsoft C runtime library defines a few Posix functions (open, read, close etc.). Because the declarations for these functions are contained in the standard C header files, they must start with an _. The reason for this is that any conforming C program is free to use any non-standard name for itself as long as it doesn't start with an _. So if the library starts definining non-standard functions that don't have an _ at the start, there's a chance that they could clash with a perfectly legal identifier in somebody's program.

As the author of the C runtime, you should follow this for maximum compatibility with standard C programs. If you put any non-standard things in the headers, put an _ at the start of the name.

As the author of C programs, you should follow this too. Don't put an _ at the start of any of your identifiers. C++ is even more strict: it prohibits you from using any identifier with __ in it.
chris

Re:Leading "_" In Function Names

Post by chris »

Thanks!
Post Reply