Preprocessor Includes, when to use <> or "" [duplicate] - c

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the difference between #include <filename> and #include “filename”?
I've hit a bit of a problem in my C learning, I had a quick search of the questions on this site and couldn't find an answer to this question. It's probably a bit stupid, but here goes.
I've been following some c tutorials and throughout the book all includes have been done like this:
#include <stdio.h>
#include <string.h> etc. etc.
All of a sudden however, they've dropped this bomb shell:
#include <stdio.h>
#include "structSize.h"
With absolutely no explanation as to why "..." was used, I'm completely dumbfounded. Could anyone perhaps provide an explanation as to what is the difference between <...> and "..." and when to use each one.
Thanks for the help.
Regards,
Mike

Typically, you use #include "..." for files in your project, and #include <...> for "system" include files.
The difference is in how and where the preprocessor searches for the file based on the name to include. "" syntax will typically search the current file's directory first. The actual search mechanism is compiler specific, however, so you would need to look at your C compiler's documentation for details on what actual paths are used for each option.
For details, see Include Syntax from GCC for one implementation's examples.

With "" the file will be searched in the directory where the file which includes something is located, if the include isn't found, the compiler will look in the standart include directory (it's compiler dependent in which folder this is).
With <> the compiler will look directly in the include directory without looking in any other directory.

Related

#include in header files in embedded [duplicate]

This question already has answers here:
Should I use #include in headers?
(9 answers)
Closed 6 years ago.
I am reading through an embedded C standard book and I noticed the following:
No header file shall contain an #include statement
What should I do with function declarations that have non-standard types?
example: void function(some_funky_type x);
Throw that book away; it is absolute garbage. In fact, you should burn it, to make sure no other poor soul ever picks it up.
Header files should absolutely include all of the header files necessary for them to be self-sufficient. There is nothing worse than trying to carefully massage the order of your #include statements to be sure that the types needed by one are already defined before it is included.
This is a stupid and counterproductive rule for precisely the reason you identified. The alternative is for every .c file to include all of the .h files that a subsequently included header will require. You can imagine that if you introduce a new dependency in a commonly included header that you will now have to update every C file that includes that header.

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.

Path in includes of C library [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the difference between #include <filename> and #include “filename”?
I am creating a shared C library. Is there any difference when including
#include <mylib/someheader.h>
versus
#include "mylib/someheader.h"
from *.c or *.h files of this library?
The first version is used for system headers, the second for external headers.
Most compilers will find the right header, whatever the notation, though.
Depends on compiler. Some of them can differ between "system" include path and "just" include path. <> denotes system include path

What modern C compiler can I use to build this 1992 MS-DOS program?

I was given the source code to modify an MS-DOS program built back in 1992. I have the EXE file and it runs fine, but I need to modify the source code. The source code needs the below headers to compile.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <dos.h>
#include <dir.h>
#include <alloc.h>
#include <ctype.h>
#include <string.h>
#include <mem.h>
#include <values.h>
Does anyone know what was used and are there any modern compilers that can handle this? I tried with Visual Studio 2010 and GCC "out of the box", but it fails because some headers are missing (dir.h, alloc.h, mem.h, values.h)
It might be more interesting to ask what what function declarations, type declarations, global variable declarations and macros it needs to have. The particular arrangement of those things into headers isn't very interesting as long as they are all there.
So comment out the offending #includes and let the compiler complain about the bits it is missing. Then you know what you're looking for.
You could try the Open Watcom compiler, which is one of the few relatively up-to-date compilers that builds 16-bit DOS executables. Other than finding an old MS or Borland compiler (or whatever was originally used), that's probably the easiest route.
If you want to rebuild for a different platform instead of rebuilding for DOS again, you'll likely have to make a lot of changes to the program itself. That may be worthwhile, but may be a lot of work and have a lot of surprise headaches.
There's Turbo C++ 1.01, not so modern, though, that appears to have all these header files as well. I still occasionally use it.
You might try using DJGPP. According to the documentation, it may have the headers you need.
a) Remove all the header files
b) Try a compile
c) Look up which header file the undefined function/type is int
d) Add the header file
e) repeat

What is a good reference documenting patterns of use of ".h" files in C? [duplicate]

This question already has answers here:
Should I use #include in headers?
(9 answers)
Closed 7 years ago.
"C Interfaces and Implementations" shows some interesting usage patterns for data structures, but I am sure there are others out there.
http://www.amazon.com/Interfaces-Implementations-Techniques-Addison-Wesley-Professional/dp/0201498413
Look at the Goddard Space Flight Center (NASA) C coding standard (at this URL). It has some good and interesting guidelines.
One specific guideline, which I've adopted for my own code, is that headers should be self-contained. That is, you should be able to write:
#include "header.h"
and the code should compile correctly, with any other necessary headers included, regardless of what has gone before. The simple way to ensure this is to include the header in the implementation source -- as the first header. If that compiles, the header is self-contained. If it doesn't compile, fix things so that it does. Of course, this also requires you to ensure that headers are idempotent - work the same regardless of how often they are included. There's a standard idiom for that, too:
#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED
...operational body of header.h...
#endif /* HEADER_H_INCLUDED */
It is imperative, of course, to have the #define at the top of the file, not at the bottom. Otherwise, if a header included by this also includes header.h, then you end up with an infinite loop - not healthy. Even if you decide to go with a strategy of:
#ifndef HEADER_H_INCLUDED
#include "header.h"
#endif /* HEADER_H_INCLUDED */
in the code that include the header - a practice which is not recommended - it is important to include the guards in the header itself too.
Update 2011-05-01
The GSFC URL above no longer works. You can find more information in the answers for the question Should I use #include in headers, which also contains a cross-reference to this question.
Update 2012-03-24
The referenced NASA C coding standard can be accessed and downloaded via the Internet archive:
http://web.archive.org/web/20090412090730/http://software.gsfc.nasa.gov/assetsbytype.cfm?TypeAsset=Standard
Makeheaders is an interesting approach: use a tool to generate the headers. Makeheaders is used in D. R. Hipp's cvstrac and fossil.
You might want to take a look at Large-Scale C++ Software Design by John Lakos.

Resources