#include <cstdio> and #include <stdio.h> in the same file!?

J

james.d.mclaughlin

I was having a look through the header files for a SAT-solver that I neededto work out how to use, and which for some reason seemed to be using a lotof the C versions of the headers (<stdlib.h> instead of <cstdlib> for instance) alongside C++ headers such as <algorithm>.

Which is how I encountered this:

#include <cstdio>
#include <string.h>
#include <stdio.h>

Does anyone know of any reason why someone would want to #include both stdio.h and cstdio in the same file? More importantly, are there any potential pitfalls to doing this? If there are, can I just fix them by commenting outthe line with stdio.h?

A slightly less important question as well - does anyone know of any reasonnot to change <string.h> to <cstring> in the above? I can easily go through the file putting std:: before function names and the like.

Many thanks,

James McLaughlin.
 
V

Victor Bazarov

I was having a look through the header files for a SAT-solver that I needed to work out how to use, and which for some reason seemed to be using a lot of the C versions of the headers (<stdlib.h> instead of <cstdlib> for instance) alongside C++ headers such as <algorithm>.

Which is how I encountered this:

#include <cstdio>
#include <string.h>
#include <stdio.h>

Does anyone know of any reason why someone would want to #include both stdio.h and cstdio in the same file? More importantly, are there any potential pitfalls to doing this? If there are, can I just fix them by commenting out the line with stdio.h?

A slightly less important question as well - does anyone know of any reason not to change <string.h> to <cstring> in the above? I can easily go through the file putting std:: before function names and the like.

Is there a way for you to wrap your lines? For some reason my Tb 17
can't handle your article when quoting...

Anyway...

To answer your question, some C++ library implementations can have the
standard C headers declaring their functions in the global namespace and
the <c..> headers putting the same functions in the 'std' namespace. If
you want to use either 'printf' or 'std::printf' in your code, you might
want to include both headers to afford yourself that liberty. Often,
however, including the .h version would actually give you both std:: and
:: version of the function, so there would be no need to include both
headers.

As to pitfalls, there could be a [buggy] version of the library where
inclusion of both files could give you ambiguity or even a conflict, but
then you should avoid using that kind of library. Of course, when you
have no choice, you'd work around the issue by only including one or the
other version of the C header.

V
 
J

james.d.mclaughlin

Is there a way for you to wrap your lines? For some reason my Tb 17
can't handle your article when quoting...

Sorry! I could have sworn Google Groups used to wrap them automatically...

Thanks for the help - but can't really see g++ versions of C library headers
being buggy in the way you describe.

James.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top