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

Discussion in 'C++' started by james.d.mclaughlin@gmail.com, Jan 22, 2013.

  1. Guest

    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.
    , Jan 22, 2013
    #1
    1. Advertising

  2. On 1/22/2013 11:40 AM, wrote:
    > 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
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jan 22, 2013
    #2
    1. Advertising

  3. Guest

    > 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.
    , Jan 23, 2013
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    15,655
    Ali Cehreli
    Aug 10, 2004
  2. Mark
    Replies:
    20
    Views:
    1,625
    Dave O'Hearn
    Dec 27, 2004
  3. Replies:
    18
    Views:
    1,884
    crazzybugger
    Oct 22, 2006
  4. John Douglass

    Crashing with cstdio file class

    John Douglass, Oct 2, 2005, in forum: C++
    Replies:
    5
    Views:
    313
    Jack Klein
    Oct 3, 2005
  5. david wolf
    Replies:
    2
    Views:
    1,065
    Gianni Mariani
    Dec 6, 2006
Loading...

Share This Page