Solved

What is the significance of "underscore prefix" in C

Posted on 1998-12-17
2
822 Views
Last Modified: 2008-03-17
In many header files I am getting some of the functions and variables that are begining with '_'(an underscore)character, what it signifies?
For eg. In <conio.h> there are two functions getche() and _getche(), Please can U distinguish what are they. And to be more specific If I am using threads I can use either _beginthread() or just beginthread() but for my surprise both are working fine eventhough in <process.h> there is only one declaration i.e _beginthread().
Hoe it happens?
0
Comment
Question by:shivaki
2 Comments
 

Author Comment

by:shivaki
ID: 1255252
Edited text of question
0
 
LVL 8

Accepted Solution

by:
Answers2000 earned 0 total points
ID: 1255253
The ANSI/ISO standard for C specifies a set of library functions.  These don't have underscore.

Most compilers (including VC) offer Compiler specific extensions (VC) to the library

In old versions of Microsoft/Visual C the MS extensions didn't have a leading underscore.  It is now (I believe) an ANSI requirement that the names of non-ANSI library functions don't begin with a letter.  In any case old versions of MS Compiler didn't have leading underscore, new versions do.

When MS changed this, they didn't want to break tonnes of existing code written to their compiler.  Therefore they support use of non-underscored names (unless I think you enable strict ANSI compliance) for the compiler specific extensions.  Some medium-old versions of MS compiler require you to link oldnames.lib to enable the non-underscored names.

In summary I recommend you use the underscored names for all new code.  This is in better compliance with ANSI/ISO C standard, and makes it obvious to yourself (or other readers of your code) that you are using an MS extension, which could be important later if you port your code to another compiler/platform.

Finally the magic about the declarations is done using the .LIB files rather than the preprocessor.  Each .LIB is the pre-built library code (already contained) plus enough info for the compiler/linker to find each function by name.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question