Link to home
Start Free TrialLog in
Avatar of sorty
sorty

asked on

C run-time library functions

Most of the c run-time library functions such as _mkdir begin with an underscore.
However, why does it still work if I use mkdir without the underscore?
And why does rename not have an underscore.
ASKER CERTIFIED SOLUTION
Avatar of PMazur
PMazur

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nietod
nietod

>> These functions are defined with underscore to
>> indicate that they aren't standard ANSI functions.
What this means is that the function is not part of standard C++, but was provided by yoru compiler for your convenience.  However, you need to understand that other compilers won't necessarily supply these functions.  so if you evenr need to make your code work on a different compiler, it might not compile withut rewriting these portions.

For this reason you want to avoid using these functions as much as possible.

>> why does rename not have an underscore.
This is a standard C++ function.  As a standard function it doesn not have an underscore.  It will be available on all C++ compilers.
Avatar of sorty

ASKER

thanks, but I'm still a bit confused. Are the following statements correct?
In the MSDN library:
1. rename is specified without the underscore. This means that there are no macros for it and it is a standard c++ function.
2. _mkdir is specified with the underscore. This means that there is no standard c++ function.

So what is the difference in using _mkdir and mkdir. I tried both and they both work, but mkdir is not documented in the library.
1.  that is correct.  it is standard C/C++.
You can look at the VC docs for the function.  Under compatibility you see it lists ANSI.  that means it is a standard ANSI C/C++ function.  (a bit outdated way to express that it is standard.)  Note that the other fucntion listed, _wrename() is not listed as ANSI.  it is not standard C/C++.  (also indicated by the fact it begins with a "_".)

2).  That is correct.

>> So what is the difference in using _mkdir and mkdir
Theoretically providing a macro called mkdir() is an "incorrect"t hing to do.  The standard allows the implimenter (compiler/library writters) to provide non-standard identifieers (functions, macros, etc) as long as then begin with an  "_".  This is to warn the user that it is non-standard and to prevent name collsisions with names used by the standard and by the user.   However the problem is that this is a somewhat new procedure (and already an outdated one with the addition of namespaces).  Many compilers provided non-standard functions that did not begin with underscores for the last 20 or 30 years.  So there are lots of programs that are written expecting these names.  i.e. they expect mkdir() not _mkdir().  So some compilers might provide macros to let these old programs continue to compiler.  This just makes a mess even messier!
Avatar of sorty

ASKER

oh, I see.
As I'm writing code that will eventually need to be portable across many platforms, I should stick to functions that do not have an _ in them.
So in the case of _mkdir (which isn't standard) if I use mkdir, I'm actually using a macro that really points to _mkdir.
Therefore, I'm not safe using mkdir even though it works.
Surely a function like _mkdir must have a standard. What should I use instead?
>>  I should stick to functions that do not have an _ in
>> them.
Yes, if possible.

>> Therefore, I'm not safe using mkdir even though it works.
Yes it is not safe.  It really us the unportable _mkdir.

>> Surely a function like _mkdir must have a standard.
>> What should I use instead?
Nope.  This is because C++ is OS indpeendant and many OSs don't support directories.  Well these days most do, but historically it was not true.  The original Mac OS (MFS) did not support directories (that was in 1985).  Older OSs, like CP/M did not support them.

The usual solution is to write your own function that makes the directory using a specified string parameter.  Inside the funciton you use conditional compilation to insure that a call is made to the right function for the OS you are using.  i.e something like

void MakeDirectory(const char *name)
{
#ifdef WINDOWS
   __mkdir(name);
#endif
#ifdef SOMEOTHEROS
  NewDir(name);
#endif
}