Search code examples
clibrariesncursesstdio

Does including ncurses.h in C programming also include stdio.h?


I was studying up on ncurses.h because I am trying to learn a bit of design aspects for c, and stumbled across a statement saying that stdio.h is inside of ncurses

  #include <ncurses.h>        /*ncurses.h includes stdio.h*/

Is this true? If so, can someone explain to me why, and if that means I don't have to ever include it again?

Also, does this cause any issues because I am not exactly defining it in a regular sense


Solution

  • Yes, including <ncurses.h> will almost certainly include <stdio.h>. But I advise not taking advantage of this.

    The documentation for ncurses does say:

    NOTES
    The header file <curses.h> automatically includes the header files <stdio.h> and <unctrl.h>.

    (ncurses.h is (usually?) a symbolic link to curses.h.)

    Thomas Dickey, the primary maintainer of ncurses, tells us that this goes back to 1994, so you're extremely unlikely to encounter an ncurses implementation that doesn't do this.

    The X/Open Curses standard, which ncurses supports, says (emphasis added):

    The inclusion of <curses.h> may make visible all symbols from the headers <stdio.h>, <term.h>, <termios.h>, and <wchar.h>.

    Furthermore, some functions defined in <ncurses.h> and/or <curses.h> take arguments of type FILE, which is defined in <stdio.h>. (It's conceivable that something could use the FILE type without including <stdio.h>, but that would be contrived and silly, and the possibility is not worth worrying about.)

    So #include <ncurses.h> is practically guaranteed to include <stdio.h>.

    Having said all that, I advise against taking advantage of this guarantee. If your C source file depends on things declared in <stdio.h>, as a matter of style you should have an explicit #include <stdio.h>, even though it's strictly redundant. More generally, a C source file should have a #include directive for any header that it depends on. This is a matter of style and maintainability, not an absolute requirement. See, for example, this question. The C standard guarantees that including <stdio.h> more than once will not cause problems.