C system header file descriptions - c

This may be a ridiculous question, but it seems like a reasonable one to me... I often open up source files and see various system header files included and wonder what each of them are. I know what the likes of stdio.h and sys\types.h are for, but there are others that I'm not too sure about. Is there someplace that gives a list of standard header files and a description of their purpose (and details given per OS, language standard)? Maybe this list would also include definitions included in said header files, macros, etc. Something organised by topic would be helpful, too.

For language-defined headers, the library section (section 7) of the C standard (PDF) is definitive. There's a subsection for each header, though <limits.h> and <float.h> are described in 5.2.4.2.
POSIX is here; access is free, but you have to sign up for an account. (I'm actually not 100% sure of the relationship among POSIX, SUS, and IEEE Std 1003.1.)
EDIT :
Mac OSX man pages are available here.
But consider that you might be approaching this from the wrong direction. When writing code, a better approach is typically to (a) decide what you want to do, (b) find a function that will do it, and (c) read the function's documentation to determine which header you need to #include. A given header doesn't necessarily have a coherent meaning.

http://en.wikipedia.org/wiki/C_standard_library

Hopefully this is along th lines of what you were looking for:
http://en.wikipedia.org/wiki/C_standard_library

Here's one for C:
http://www.acm.uiuc.edu/webmonkeys/book/c_guide/

One way is to look inside the header file. Many files have good comments. Under unix systems you could use the manpage: man stdint.h p.a.

Related

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.

Universal header to include in C language

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.

What is the difference between the various unistd.h under /usr/include in Linux?

Under the /usr/include directory in Linux i entered the command:
find -type f -name unistd.h which gave the following output:
./unistd.h
./linux/unistd.h
./asm-generic/unistd.h
./bits/unistd.h
./asm/unistd.h
./sys/unistd.h
my question is, what is the purpose of each unistd.h, since there is only one definiton of that file in the single unix specification ?
Thanks in advance.
linux/unistd.h actually points to asm/unistd.h, which in turn points to either asm/unistd_32.h or asm/unistd_64.h, which is where system call numbers are defined and presented to user space depending on the system's architecture. These come from the kernel.
bits/unistd.h is a collection of macros that augment unistd.h (mostly stuff to help prevent buffer overflows), which is conditionally included via:
/* Define some macros helping to catch buffer overflows. */
#if __USE_FORTIFY_LEVEL > 0 && defined __extern_always_inline
# include <bits/unistd.h>
#endif
In essence, the only POSIX required header is in fact, just unistd.h, the rest are either extensions, or definitions from the kernel. So, just including unistd.h is all you have to worry about doing, everything you need will be pulled in depending on your architecture and whatever build options you've selected.
It's a common technique in C and C++ - you have a single file with the "standard" name in the "standard" place, in this case ./unistd.h, and then have that file include one or more implementation specific files, depending on preprocessor macros. If you look at almost any "standard" C or C++ header files, you will see it including other files not mentioned in any standard.
Basically think of /usr/include/unistd.h as a smart symbolic link. It will point to a correct implementation depending on what your operating conditions are.
That said, it makes difficult sometimes to figure out what that correct implementation is.

What is sys/user.h used for?

I was inspecting the code of a linux application and i saw the #include in one of the code files. I tried looking it up on opengroup.org but i couldn't find it there, this is what the sys directory looks like: http://www.opengroup.org/onlinepubs/000095399/basedefs/sys/ . I guess it's not standard header file, but i checked it in my /usr/include/sys and it was there.
What does it do and what it is used for ? If you can provide me with some manual for it, i would be grateful. Thanks.
Used in conjunction with ptrace(2) (see PTRACE_PEEKUSER): http://linux.die.net/man/2/ptrace
The comment at the top of the header pretty much says it all:
#ifndef _SYS_USER_H
#define _SYS_USER_H 1
/* The whole purpose of this file is for GDB and GDB only. Don't read
too much into it. Don't use it for anything other than GDB unless
you know what you are doing. */
GNU specific extensions are usually pretty easy to identify (e.g. _GNU_SOURCE). However, debugging and instrumentation has to work even if those extensions aren't turned on. For instance, people want to use GDB on code that does not #define _GNU_SOURCE.
In that case, stuff that is not defined in ISO C (and not required by POSIX) is usually clearly labeled as such.
You'll also find all kinds of strange looking symbols in programs that include the Valgrind headers.

Reading Recommendations To Learn Basics Of C

I'm now really diving into my OS project, called ForestOS, but now I'm needing to dive more into some simple and basic things of C. As now I'm having many problems with the correct variable to use and functions.
I want resources that only talk about variables, functions and how to develop without headers(stdio.h, math.h and all the others).
Best starting place is probably the book The C Programming Language.
The book was central to the development and popularization of the C programming language and is still widely read and used today.
A guide to OS development suggests CProgramming.com as the best place to start. There's tutorials, links to further resources, and everything for free.
Building an OS is non-trivial, I suggest if you are "having many problems with the correct variable to use and functions" then you may be attempting to walk before you can run!
Quote:
how to develop without headers(stdio.h, math.h and all the others).
I assume that you actually mean that you want to code without using the standard library rather than "without headers". Header files are intrinsic to modularisation in C; if you did not use headers, your code would have to be one monolithic module. Don't confuse headers with libraries.
However, even then there is no need not to use the standard library when writing 'bare-metal' code. You simply need a library that does not have OS dependencies, and you write the low level glue code to make things like stdio and memory allocation work on your system. Such a library is Newlib for example. It will make your life a whole lot easier if you have standard library support.
You only need headers to provide declarations of functions and external variables.
It is possible to eliminate the header files and provide your declarations within the translation unit (a.k.a. source file). Although possible, this is not recommended.
Here is an example of a legal C program without header files:
/* Forward declaration of main(). */
int main(void);
/* Definition for main() function. */
int
main(void)
{
return 13; /* 42 is such an overrated number. */
}
Some reasons for using header files are: code / typing reduction and single point of maintenance. If two modules need the same structure declaration, placing it in a header file will reduce typing (you only have to #include it in both files instead of copying it into both files). Also, if you need to change any declaration, if it is copied, you'll have to hunt down all copies and change every instance vs. making one change in a header file.
As far as standard header files, such as math.h and stdio.h, if you don't need them, don't include them. An OS should not require stdio.h, but may use math.h. Most standard header files do not contribute to the code size; only to the compile time.
I highly suggest you focus on the correctness of your OS and don't worry about trivialities such as header files. After your OS is working correctly and robust, go ahead and trim the fat.
Go through Kernighan C or the book named "Let Us C".It would help you learn much better as a beginner

Resources