Solved

What is the significance of "underscore prefix" in C

Posted on 1998-12-17
2
819 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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 conditional statements in the C programming language.

803 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