Jump to content
  • Advertisement
Sign in to follow this  

C Linkage in C++ and the Standard Namespace ...

This topic is 4157 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I recently discovered C linkage and I managed to understand all the business regarding name mangling etc. but I still have one confusion: I've read that in headers like <cstdlib> which ensure proper C linkage, functions like printf have been defined in the standard namespace. 1) Why have these functions been defined in std? 2) If they have been placed in std, why can I still use both printf and std::printf? ( Which is better ... what is the difference? ) 3) Some standard C functions that are typically found in <stdlib.h> are not in the standard namespace at all ... what gives?

Share this post

Link to post
Share on other sites
Well having a look in my cstdlib header... what happens is that it includes stdlib.h... and it undefines the macros (so that real functions are used) and the functions are additionally added to std namespace... it's like doing:

void myfunc();
namespace std
using ::myfunc;

That's to say the function is defined in global namespace... and it's added to std namespace using a using directive qualified from global namespace. That is to say that you may use them qualified or unqualified.

As to which is better... they are equivalent. The purpose of them being added to std:: namespace is for consistency with the rest of C++ in general... but they are also left in global namespace for backward compatibility.

I'd say that you should be using std:: qualifier as this is more consistent. As for why some of stdlib.h isn't taken into global namespace, I'm not sure... It's possible that their functionality is included in other headers? I'm not certain.
Anyway, I hope this helps with your query,


EDIT: I just thought, maybe you mean NULL is not defined in std namespace? This is because NULL is a #define, and hence does not respect namespaces (it's simply 0)... so std::NULL would be preprecessed to std::0.

[Edited by - mrcheesewheel on July 27, 2007 1:02:57 PM]

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!