Explain the difference between standard and user defined libraries? [duplicate] - c

This question already has answers here:
What is the difference between #include <filename> and #include "filename"?
(30 answers)
Difference between angle bracket < > and double quotes " " while including header files in C++? [duplicate]
(2 answers)
Closed 9 years ago.
I would like an explanation of the difference between < header.h > and "header.h" in library #include directives. How exactly (in which locations) does the linker search for the files? In what order does it perform the search?

When we write <stdio.h> , we are referring to a header file which is available in the include directory of the system. When we write #include <stdio.h>, the preprocessor searches for the header file in the system include directory and not in the current directory. When we write #include "stdio.h", the preprocessor starts searching for this header file in the current directory and then in its parent directories. So if we write our own stdio.h, save it in the current directory, and include it in the program using #include "stdio.h" then our header will be included instead of the system header.
In short, if we use angular brackets (<>) then we are indicating that the file can be found in one if the standard directories in the
system. If we use quotation marks (" ") then we are indicating that a non-standard header is being used.

Related

microtime.h: No such file or directory [duplicate]

This question already has answers here:
How to include header files in GCC search path?
(3 answers)
Closed 1 year ago.
I am attempting to compile a program file named MM.c. However, I continue to get the error message "microtime.h: No such file or directory". Even though clearly you can see in the image that the microtime.h file is in the same directory.
You're using <> to delimit the name of the header file. That tells the preprocessor to look only in system include directories or those specified by the -I option.
Application header files should be delimited with double quotes instead.
#include "microtime.h"

can someone explain me why the #include directive in C allows to include files other that ".h" files [duplicate]

This question already has answers here:
What tokens are permitted as arguments to #include?
(2 answers)
Closed 2 years ago.
can someone explain to me why writing something like:
#include "file.txt"
#include "File.js" /* why including files other than files with .h extension is valid */
#include "anotherFile.c"
main()
{
printf("why including files other than header files (.h files) is allowed in C");
}
So can someone provide me a good tutorial online describing this. when i search online about this i find nothing. is this behavior of #include directive described somewhere in the C standard pdf or C standard html if so please guide me to the actual page where this behavior is described in the C standard with a link
The include directive is part of the C Preprocessor. The preprocessor will simply replace an include with the contents of the file referenced.
See 6.10.2 Source file inclusion:
A preprocessing directive of the form
#include "q-char-sequence" new-line causes the replacement of that directive by the entire contents of the source file identified by the
specified sequence between the " delimiters.

Difference between #include <filename> & #include "filename" [duplicate]

This question already has answers here:
What is the difference between #include <filename> and #include "filename"?
(30 answers)
Closed 7 years ago.
Here is a preprocessor directive in C :
#include <filename>
we can write it in this way also:
#include "filename"
Is there any difference between these two?
In general, the <> version should look only in "system directories", while the "" should look in the "local directories" first, and then the system directories.
What that actually means is implementation dependent. In most cases "" will look in the current directory first, but in some implementations it will look in the directory of the source (.c) file first (and some compilers have a switch for that). Also, behavior is different w.r.t. the "set of system directories" to search if "local directory search" fails (the same as for <> or not).

C language, How to include a file from "XXX.h" to <XXX.h>", maybe using Makefile? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the difference between #include <filename> and #include “filename”?
I just got involved into a project.
When I were tracing the project code, I found that
some people can include a header file by this way:
include < XXX.h >
XXX.h is a header file that is not in system libraries and made by our own programmers...
My question is how to use '<' and '>' instead of double " ?
and how to include headers in other directories by this way?
for example:
headers/header_a.h
headers/header_b.h
I can use include < header_a.h > and < header_b.h >...
should I use Makefile to implement this? thanks..
A file in double-quotes is referenced relative to the current directory:
#include "../file.h" // file from parent directory
You'd generally use this for your own headers.
A file in angle-brackets is referenced relative to the paths specified to the compiler:
#include <sys/bits.h> // file under, e.g., /usr/include
#include <thirdpartytools/somelib.h> // file under /path/to/third/party/includes
You'd generally use this for system or perhaps third-party headers, assuming the compiler is invoked with something like
gcc -I/usr/include -I/path/to/third/party/includes ...
The <> usually denotes System headers and the "" usually denote header files in the current directory (usually your header files). Source/Reference here for more info:
Difference between angle bracket < > and double quotes " " while including header files in C++?

What is the difference between <stdio.h> and "stdio.h"? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
what is the difference between #include <filename> and #include “filename”
In both cases there is no error ...Is there any difference between them ?
<stdio.h> searches in standard C library locations, whereas "stdio.h" searches in the current directory as well.
Ideally, you would use <...> for standard C libraries and "..." for libraries that you write and are present in the current directory.
The second version is specified to search first in an implementation defined location, and afterwards if the file is not found, search in the same place as the <...> version, which searches in the paths usually specified by the -I command line option and by built-in include paths (pointing to the location of the standard library and system headers).
Usually, implementations define that location to be relative to the location of the including file.
The <> tell the compiler to look for the file in the libraries' headers and "" tell it to look around among your application's headers.
As for why both of them works for you, maybe your compiler also looks for the filename in the library headers in case it didn't find one among yours.
You use #include when you want to say: "look for a file with this name in the system's include directory". You use #include "doublequoted" when you want to say: "look for a file with this name in my own application's include directory; however, if it can't be found, look in the system's include directory".
#include <something.h> is meant for system headers, while #include "something.h" is for headers of your own program. System headers are searched for in usual system directories (and those included with -I argument), which your headers are searched for in current directory and then the same locations as system headers.
see http://gcc.gnu.org/onlinedocs/gcc-2.95.3/cpp_1.html#SEC6
For the compilers I've used, "..." starts looking for the include file in the same directory as the source file that is being compiled, then the include path. Includes with <...> start in the include path, skipping the current die unless it is in the include path.
Normally standard header files are enclosed by < > and other user specific files are specifed with " .
I case of "..." compiler first search the header file in your local directory where
your .c file presents
while in case of <...> compiler only search in header file folder
The difference is that header files made by the developer are enclosed by "". Header files that are already in the system are enclosed with <>. Even the <> headers need the -I directive if the directories that are placed are not in the search path of the compiler.
Bottom line: Your headers with "", system headers with <>
<stdio.h> refers to a header (not a header file)
"stdio.h" refers to a source file.
Headers need not exist phisically in the implementation; the way they are identified is implementation-defined (usually the headers are files on specific directories)
When the directive uses ", the source file is searched in an implementation-defined manner and, if not found, the directive is reprocessed as if it was written with < and > in the first place.

Resources