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

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

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.

In general, even if one header includes another as part of its implementation, you should always trust the C standard and the documentation of whatever API you're using. If it says that a certain header file is the one that contains a certain item, or it says that in order to access a certain item, include this header, you should follow that.
It's not harmful to include the same file multiple times if it has include guards (i.e. #pragma once or #ifndef _HEADER_NAME ... #endif)
You're technically relying on an implementation detail if you omit an include that the C standard (or API documentation) says is the correct file to include for a certain declaration or definition. That means that the next version of your compiler or library could very well remove that convenient nested include that makes your program a couple of lines shorter.

It does indeed include stdio.h, and yes this does mean that you don't need to include stdio.h again.
This works because the compiler will insert the contents of the #include'd header files at the point in your code that the #include directive was placed. This means that not only was ncurses.h included in your code at the location you specified, but stdio.h was already included in that file at the appropriate location prior to it being placed into your code.
Subsequent includes of stdio.h will have no effect on your program due to the use of include guards.
If you're using anything from stdio.h in your own code I would consider it good practice to add the #include <stdio.h> directive to your own source files. My reasoning is that it communicates directly to the next developer reading your code that stdio functionality is used here without relying on implicit information.

ncurses' curses.h includes <stdio.h> because a few of the (X/Open Curses) standard functions which curses.h declares use FILE. The C standard refers to FILE as an "object type", which some might read literally as requiring a typedef. However there have been implementations where FILE is a symbol defined with #define, e.g.,
#define FILE struct _iobuf
(quoting from more than one BSD: 4.3BSD, SunOS 4, Ultrix). Without delving into its history, I'd assume the change to make it a typedef came from the AT&T code since it shows up in the SVr4 implementations.
For that reason (as well as it being the simplest way to guarantee that FILE is declared properly), curses.h will include stdio.h.
Now... there are a few other header files which you might encounter which require another header file to be included, to make them useful. One longstanding example is for the stat function, i.e.,
#include <sys/types.h>
#include <sys/stat.h>
but in the relevant (system-specific) documentation, both headers are shown in the manual page summary. The POSIX documentation for stat does not point that out, but uses it in the examples. Some newer implementations include (or provide in some roundabout way) the definitions from <sys/types.h> needed in <sys/stat.h>, but in writing portable code, that has not been something to rely upon.
curses was never documented in that way, e.g., you're unlikely to find manual pages showing
#include <stdio.h>
#include <curses.h>
because the original 4BSD curses included <stdio.h>. Interestingly enough, 4.2BSD curses header did not use any definition from stdio.h. It also included sgtty.h (analogous to termios.h), but used those definitions in the header file. Perhaps the original developer thought that including stdio.h was a good idea. In any case, in later versions of curses, it did use FILE, and since the precedent of including stdio.h. from curses.h was well established, no one considered splitting things up as done with <sys/types.h> and <sys/stat.h>.
ncurses' documentation has mentioned the header files which curses.h includes, in a NOTES section:
The header file <curses.h> automatically includes the header files
<stdio.h> and <unctrl.h>.
That note dates back to ncurses 1.8.7 in 1994, making it rather unlikely that you will encounter a version of ncurses for which the statement is untrue.
X/Open Curses has something similar to say:
The inclusion of <curses.h> may make visible all symbols from the headers <stdio.h>, <term.h>, <termios.h> and <wchar.h>.
HPUX curses, for instance, includes <term.h> from <curses.h> to declare setupterm in curses.h, but ncurses (and Solaris curses) do not. AIX curses includes <term.h> and <termios.h>. Again, ncurses (and Solaris curses) do not. X/Open Curses says "may make visible" because including a header file does not necessarily make all symbols in it visible (there are ifdef's to consider).
However, these functions in any X/Open Curses (i.e., anything more current than 1990) use FILE, making <stdio.h> needed for <curses.h>:
getwin (SVr4)
newterm (SVr2)
putwin (SVr4)
Now... including <stdio.h> before <curses.h> probably will not make any difference (aside from adding clutter to your program). It's possible (but unlikely) that there could be some symbol redefined in <curses.h> which alters the behavior of the standard features in <stdio.h>. Since any implementation of stdio.h which you are likely to encounter has include-guards (a feature from the 1990s—not seen in SunOS 4's 1994 headers) including <stdio.h> after <curses.h> would also be unlikely to change the behavior of your program (again, clutter being the only result).
Include-guards have been the preferred style for system headers since the 1990s. I have a script (called Include) which I use for test-compiling headers which I wrote in 1998, to check that a given header includes the necessary headers (such as in this case) to provide all of the necessary typedefs and symbols used in the header file.

Related

Why is stddef.h not in /usr/include?

I have compiled the gnu standard library and installed it in $GLIBC_INST.
Now, I try to compile a very simple programm (using only one #include : #include <stdio.h>):
gcc --nostdinc -I$GLIBC_INST/include foo.c
The compilation (preprocessor?) tells me, that it doesn't find stddef.h.
And indeed, there is none in $GLIBC_INST/include (nor is there one in /usr/include). However, I found a stddef.h in /usr/lib/gcc/x86_64-unknown-linux-gnu/5.3.0/include.
Why is that file not under /usr/include? I thought it belonged to the standard c library and should be installed in $GLIBC_INST/include.
How can I compile my foo.c with the newly installed standard library when it doesn't seem to come with a stddef.h?
Edit: Clarification
I feel that the title of this question is not optimal. As has been pointed out by some answers, there is not a requirement for stddef.h to be in /usr/include (or $GLIBC_INST/include, for that matter). I do understand that.
But I am wondering how I can proceed when I want to use $GLIBC_INST. It seems obvious to me (although I might be wrong here) that I need to invoke gcc with --nostdinc in order to not use the system installed header files.
This entails that I use -I$GLIB_INST/include. This is clear to me.
Yet, what remains unclear to me is: when I also add -I/usr/lib/gcc/x86..../include, how can I be sure that I do have in fact the newest header files for the freshly compiled glibc?
That's because files under /usr/include are common headers that provided by the C library, for example, glibc, while the files at /usr/lib/gcc are specific for that particular compiler. It is common that each compiler has their own different implementation of stddef.h, but they will use the same stdio.h when links to the installed C library.
When you say #include <stddef.h> it does not require that /usr/include/stddef.h exists as a file on disk at all. All that is required of an implementation is that #include <stddef.h> works, and that it gives you the features that header is meant to give you.
In your case, the implementation put some of its files in another search path. That's pretty typical.
Why is that file not under /usr/include?
Because there's absolutely no requirement for standard headers to be located at /usr/include/.
The implementation could place them anywhere. The only guarantee is
that when you do #include <stddef.h>, the compiler/preprocessor correctly locates and includes it. Since you disable that with -nostdinc option of gcc, you are on your own (to correctly give the location of that header).

Should I, or should I not include the same headers in different c files, which in turn are headers used in a main file?

I'm building a main.c file to make use of functions out of a few different .h files. A few of those .h files (or rather, their .c source files) use the same includes (the standard but also some others like )
My question is: Is it okay if I just include those once for all header files in my main.c, or should I let every .h file include them individually and not include them in my main.c (considering I only use functions from those header files)?
Or should I do both?
How I do it now is:
dist.c:
#include "dist.h"
#include <stdio.h>
#include <unistd.h>
#include "rpiGpio.h"
#include <pthread.h>
#include <wiringPi.h>
#include <softPwm.h>
Then for another:
cmps.c:
#include "cmps.h"
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include "rpiGpio.h"
Then in my main.c:
#include <stdio.h>
#include <stdlib.h>
#include "dist.h"
#include "cmps.h"
Thanks in advance!
You should include standard headers above your own headers, and you should include all the dependencies for a file in that file. If you change the includes in one of your file, it shouldn't affect any other files. Each file should maintain its own list of header dependencies.
If, in your example, dist.h includes <stdio.h>, you should not rely on this outside dist.h. If you change dist.h so that it no longer depends on <stdio.h> and remove that #include, then your program breaks.
I think the answer is "it depends". So long as you are consistent, I think it's OK. Some advantages and disadvantages of different approaches:
Including a system header twice will not cause adverse effects (due to header guards); including your own headers twice should also not cause problems provided you use header guards too. However, arguably it may slow compilation.
In some circumstances, particularly on older Unices, the order of inclusion of headers matters (sadly). Sometimes the order of #defines (e.g. #define _GNU_SOURCE) does matter with respect to #include. I've also had this happen with Linux include files for various internal bits of networking (I now forget what). For this reason, it's a good idea to always include your system includes (and the #defines they examine) in a consistent manner.
One way to do that is to put all your system includes in a single include file of your own, and include them from each .c file; this produces much the same result as you would get with autoconf and its generated config.h. However, it may unnecessarily include files slowing compilation down.
Some say put includes of system headers above your own includes. Whilst this is often seen as good practice, it doesn't work great if your own .h files reference types defined in system includes, e.g. stdint.h defines int32_t. If you include that in your .h file alone, you are back to a potential problem with include order and whether or not you've got your #define _GNU_SOURCE in the right place.
Therefore my personal coding style is to have a config.h equivalent, which is included by all .h files and (for good measure) all .c files as the first include, which contains all the relevant system includes. It's not ideal for compile time, but that's what precompiled headers are for. I normally arrange system includes in alphabetical order, unless there is a reason not to.
The alternative is to (carefully) do it by file, as #meagar suggests.

How to know, what is inside a header file?

I wonder what is inside stdio.h and conio.h etc.
I want to know how printf and scanf are are defined.
Is there a way I can open stdio.h and see what is written inside?
Depending on your implementation, you should be able to open any .h file in your favorite editor and read it directly; they're (usually) just plain text files.
However, stdio.h will only give you the declarations for printf and scanf; it won't contain the source code for them. Most compilers don't ship the source code for standard library functions; instead, they ship precompiled libraries which are linked with your code when you build the executable.
If you're willing to spend some money, P.J Plauger's The Standard C Library is a good resource that shows an implementation of the standard library functions.
When the preprocessor includes a header file into a source file, that inclusion is very much literal. That means that the header files are normal text files with source in them, and must be readable by the compiler (and therefore by you). You just have to find where they are, and you can open them like any other text file.
However, you won't find out how functions are defined, just how they are declared. And some structures are supposed to be "black boxes", whose data members should be considered private. Usually the source for the standard C library is available or downloadable, so try and find that too. It all depends on what compiler you're using.
You might also want to check out a reference site such as this one. There you can find pretty detailed information about e.g. printf.
Those headers generally chain include more machine/OS specific headers.
If you are on Linux/OS X then you can get some more info with
man stdio
Also check out http://www.cplusplus.com/reference/cstdio/ https://en.wikipedia.org/wiki/Conio.h
Most compilers allow you to read the results after the preprocessor (the compilation step that processes the #include directives) has been run. With gcc for instance, use the -E command-line option.
You can always rely on the Internet's supply of Unix-style manual pages, by searching for "man something" you can look for the relevant manual section for something.
For instance, there are pages for both printf() and scanf().
You can easily see there that the declarations aren't very special, and quite obvious from the usage. It's just int printf(const char *format, ...); for instance.
the content of some headers is defined by the C-Standard.
other headers are defined by the library that provides it.
Some headers are defined from the system for that you are writing the code (may fall into the second case since the OS provides the libs)
depending on that you may look into c language reference or you may look into the libraries manual or in the OS's API reference.
But one thin is for sure. if you can include a header (and the compiler does not complain that he could not find it) than you also can look into it. just look into the standard include directories of the compiler or the additional include directories that are specified in project file ore Makefile to find the files on your file system.
But usually the better way is to look in the Documentation because the header itself may be difficult to read because of many #ifdefs and further includes
The most fundamental way to find out what's inside those headers is to read them. Of course, you must locate them first. To this end you can use this short shell code:
gcc -E -M - << EOF
#include <stdio.h>
EOF
This will provide you with a complete list of all the headers directly or indirectly included by #include <stdio.h>. Of course, if you are only interested in the 'stdio.h' header itself, you can just do
locate stdio.h
but this will usually list quite a few false positives.

Type definitions in POSIX/SUS headers

This question concerns the various types that need to be defined in the required headers of the POSIX/SUS standard.
Some types needs to be defined in many header files, and I'm wondering what's the correct and compliant way of achieving this.
For instance, look at the <time.h> header specification:
Inclusion of the header may make visible all symbols from the
<signal.h> header.
This is straightforward, <signal.h> is included from <time.h>.
Now what about this:
The clock_t, size_t, time_t, clockid_t, and
timer_t types shall be defined as described in
<sys/types.h>.
As I understand it, it means we can't simply include <sys/types.h> from <time.h>, as this would expose more symbols than required.
So can anyone confirm this?
Would it break the standard compliance to include <sys/types.h>?
If so, I think the best solution is to create a specific header for each type, so a particular type can be made visible from anywhere, without worrying about other symbols.
Is there any other good solution?
And last thing, what about the types from the C standard?
Lots of types in the POSIX/SUS specification are integral types, and may need to have fixed width.
So would it be ok for the standard to include <stdint.h> from a specific header, or would it break the compliance?
You are right that exposing unwanted types and declarations from additional headers would be non-conforming.
One trivial (but, in terms of time the preprocessor spends opening files, expensive) solution is to have a separate header for each type, and whenever you need, for example, time_t, do:
#include <types/time_t.h>
Of course types/time_t.h would have the appropriate multiple-inclusion guards.
There are many other ways of achieving the same. The approach glibc and gcc use is to have special "needed" macros you can define before including a header that asks it to do nothing but provide one or more types. This solution is also very expensive, probably moreso than the above, because it breaks compilers' heuristics for multiple-inclusion guards. The compiler can't elide repeated inclusions; it has to parse the file each time it's included.
The way we do it in musl is to have a single file, bits/alltypes.h, which includes the definitions of all types which are needed by multiple headers and macros to control which are exposed. It's generated by a simple sed script that hides all the macro logic:
http://git.musl-libc.org/cgit/musl/tree/arch/i386/bits/alltypes.h.in?id=9448b0513e2eec020fbca9c10412b83df5027a16
http://git.musl-libc.org/cgit/musl/tree/include/alltypes.h.in?id=9448b0513e2eec020fbca9c10412b83df5027a16
http://git.musl-libc.org/cgit/musl/tree/tools/mkalltypes.sed?id=9448b0513e2eec020fbca9c10412b83df5027a16
Upon reading this question i opened the couldn't resist checking this up in THE largest UNIX compliant (strictly though, only "UNIX-like") open-source project of all time - Linux Kernel (which is again almost always worried about standard compliance nowadays).
A sample implementation :
The time.h header will define a few internal flags and then include the types.h which defines ALL the types albeit within #ifdefs that check whether some internal flag is defined.
So this boils down to the following :
Define modular headers like time.h
#define the relevant internal flags to establish the context.
Include the "internal" headers that provide dependencies.
Implement the "internal" headers such that they selectively expose functionality based upon the context in which they are #include-ed.
This way modular headers can be provided for inclusion without worrying about accidentally exposing more symbols than required.

what are the commonly used libc header files and their functionalities

I have searched a lot and found a beautiful reference of https://en.wikipedia.org/wiki/C_standard_library different header files, but it doesn't say anything about the common functions they define. Is there any brief reference to commonly used C functions?
For example:
#include <getopt.h>
#include <event.h>
#include <libpq-fe.h>
#include <config.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <pwd.h>
#include <grp.h>
..are headers which I see commonly on major c programs.. Can anyone explain what they do?
Nb : if i see a method int XXX(char *YYY) , how can i find in which header this method is defined ..
There is no in-detail explanations of how everything work in standard C library I am aware of. I can not tell if there is any, but note that the implementation is platform-dependent. Thus, you will need many explanations.
It would be better for you to take a look at the documentation and the source of the libc you are using. For instance, for glibc it can be found here: docs, source.
As of the headers you have mentioned, it really is not standard in C (except stdio.h), though commonly used in linux. For instance, getopt.h lets you use functions for command line options traversal. It is quite easy to google what each header is related to. There is no header-meaning relation in one place for every header you will see.
Finding out which header contains a function is usually done by googling. Yes, again. But there are at least two other ways to find it out. First, if you use an IDE, it can let you 'go to declaration' of a function, which will effectively find the header file. Second, you can grep through all /usr/include/ files (or wherever your header files are stored) and find where the function is declared.
Also note that where a function is declared in a header does not tell you where the function is implemented. For instance, most (if not all) functions from standard library are implemented in glibc (or ms c runtime).
See here for what's standard C:
http://port70.net/~nsz/c/c11/n1570.html#7
And here for what's standard POSIX:
http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/contents.html
As for how to find where a given function is declared, lookup the function here:
http://pubs.opengroup.org/onlinepubs/9699919799/functions/contents.html
and the correct header will be shown in the synopsis text. Your own system's manual (e.g. man pages on *nix) will probably also contain this information, but around 5% of the time they're wrong, so it's best to look to the standards for the authoritative answer.
Actually if you look at your beautiful reference, you can click most of the include files and find there the list functions they define.

Resources