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.
Related
what is the substitute for something like this,
dir(int)
in C?
when I include something like,
#include <string.h>
then I would like to see what is being included.
C does not have any introspective feature analogous to Python's dir(). It also does not have an object model at all like Python's, which would be necessary to make that sensible. Especially, it does not have anything along the lines of packages- or modules-as-objects, which is the only way that an analogue of dir() could be useful for inspecting header contents.
The C programmer generally approaches the issue from exactly the opposite direction: (1) what functions do I need? (2) what headers provide their declarations, and what additional libraries, if any, need to be linked to get their implementations?
If you are using GCC to compile your code you can use:
gcc -E NAMEOFFILE.c
Which will give you the file after its run through the preprocessor.
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.
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).
I am new to C language, and was wondering if there is a universal header that can be included at top of main function . In Java it is very easy just do ctrl+shift+o in eclipse and it imports packages for you. But in C, I have to google every time and add it. Sometimes, I don't even know what library to include.
Thank you very much .
There is no universal header -- since parsing every header takes time, and there are thousands (if not millions) of headers available, there is no way to include them all into every compilation unit. You wouldn't want to, since 99.9% of them wouldn't be used and would only needlessly bloat the end executable with static allocations.
Every standardized function will tell you the headers you need to include at the top of its manpage; for example, from malloc(3):
NAME
calloc, malloc, free, realloc - Allocate and free dynamic
memory
SYNOPSIS
#include <stdlib.h>
void *calloc(size_t nmemb, size_t size);
void *malloc(size_t size);
void free(void *ptr);
void *realloc(void *ptr, size_t size);
Thus you need to #include <stdlib.h> in your project, and there's the prototypes for you to see.
If you want a quick way to see the manpages, you can configure your IDE to show them to you quickly. The default keybinding for K in vim is to load the manpage for the function under the cursor -- but, since it uses the default manpage search order, it can sometimes find the wrong page. (On printf, for example, it loads printf(1) rather than printf(3). Annoying. The MANSECT environment variable described in man(1) can be used to change this behavior to show you 3 before 1, if you wish.)
There isn't a single header that is all-encompassing.
All else apart, any such header for MS Windows would be wrong for Unix, and any such header for Unix would be wrong for Windows. Even on Unix, should the universal header include all the X11 headers? What about the OpenSSL headers? What about the POSIX threads headers? Other POSIX headers?
You need to learn where to go to find the information for any given function that you need to use. On Unix, the classic resource was the 'man page' (meaning 'manual page', typically formatted with the '-man' troff/nroff macro package). These days, I tend to use the web: for example, I find POSIX man pages at The Open Group.
(Note that a header is separate from a library; there may be many headers used by the functions in a single library. See the Standard C Library as an illustration.)
There is no universal header, but you can certainly make one for your application, and include it in all your files. If you are not sure what file to include for a function that you need to call, you can use man command on UNIX. For example,
man 3 printf
shows this:
SYNOPSIS
#include <stdio.h>
int printf(const char * restrict format, ...);
If you stay with it for enough time, you usually remember the "mapping" of functions to headers relatively quickly.
No.
I suppose you could create one, but that would slow your compiles down quite a bit. Some IDEs might help you find the include files.
You can get Eclipse to automatically insert various header files you use often. But: if you want to learn C I would suggest that you do that job of finding which ones to include.
It's as with all learning. Learn by doing — learn by repetition.
The standard C library is not that huge — and the header files are intuitively named and have a manageable list of standard functions.
Have a list like this available and read it. Read it when you'll need an include etc.
When you feel you really know when and what to include. Automate it.
You are including a header file because you are using an interface defined in the header file; not because there is a standard set to include. So, how do you determine what interface you need? There are two basic options:
Find either the header files or descriptions of the header files and look through them to learn of the interfaces, or
Use 'apropos search_term' or 'man interface_function'.
As a beginner, option #1 would be a good start — look in /usr/include or find a book named 'The C Standard Library' (or something similar).
A good reference manual (such as this one) will have an appendix listing all of the library functions and their associated header file. You can also check the on-line C language standard, Chapter 7 for a similar listing.
when I read ucos source files, I find this code in ucos_ii.c
#include "os_core.c"
#include "os_mbox.c"
#include "os_mem.c"
#include "os_q.c"
#include "os_sem.c"
#include "os_task.c"
#include "os_time.c"
what's the advandage of including .c files?
By doing this, they may be allowing the compiler to do more inlining and/or space optimization. uCos is an embedded operating system, so anything that saves space or time is a good thing. (Within reason, of course)
It can simplify the building process by requiring a simpler makefile. In this case, 7 less files need to be added to the makefile. However, as projects become large, it quickly becomes unwieldy.
Another downside is any variable which would normally have internal linkage is now available to the other c files.
I hope someone can correct me if I'm wrong, since my episodes of coding in C are far and few in between, but AFAIK, adding a .c file like that lets you treat all the functions and whatnot that are defined in that file as if they were coded directly in the file they are included in. That should let you build up a more complex file from simpler, more re-usable parts.
i think this is used to import the system library function,and when you need to use a method that from system library than it works