-1

Reading various C/C++ codes, I can say that I am confused between these two ways of including a header file inside the code.

#include <stdio.h>

and

#include "stdio.h"

Some text editors has auto-completion feature. I have been recommended the latter most of the time. But in various examples of programs, using angular braces is also recommended.

Which one should one choose while writing a C/C++ program? What are the differences of the two? Are they language specific?

Himanshu Mishra
  • 8,510
  • 12
  • 37
  • 74
  • See http://stackoverflow.com/questions/4118376/what-are-the-rules-on-include-xxx-h-vs-include-xxx-h/4118390#4118390 – paxdiablo Jul 04 '15 at 05:00

1 Answers1

0

No, they are not language specific. Both C and C++ makes use of them.

https://gcc.gnu.org/onlinedocs/gcc-3.1/cpp/Include-Syntax.html

Both user and system header files are included using the preprocessing directive ‘#include’. It has two variants:

#include <file>

This variant is used for system header files. It searches for a file named file in a standard list of system directories. You can prepend directories to this list with the -I option (see Invocation).

#include "file"

This variant is used for header files of your own program. It searches for a file named file first in the directory containing the current file, then in the quote directories and then the same directories used for <file>. You can prepend directories to the list of quote directories with the -i quote option. The argument of #include, whether delimited with quote marks or angle brackets, behaves like a string constant in that comments are not recognized, and macro names are not expanded. Thus, #include <x/*y> specifies inclusion of a system header file named x/*y.

However, if backslashes occur within file, they are considered ordinary text characters, not escape characters. None of the character escape sequences appropriate to string constants in C are processed. Thus, #include "x\n\\y" specifies a filename containing three backslashes. (Some systems interpret \ as a pathname separator. All of these also interpret / the same way. It is most portable to use only /.)

It is an error if there is anything (other than comments) on the line after the file name.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Himanshu Mishra
  • 8,510
  • 12
  • 37
  • 74
  • Several of the details here are specific to some implementations (e.g. under unix) but are not required by the standards. Implementations on other systems do such things differently. – Peter Jul 04 '15 at 05:03
  • The standard actually says nothing about `-I` and current directories and so on, headers are not even required to exist as 'real' files :-) The whole thing is very implementation specific. – paxdiablo Jul 04 '15 at 05:03
  • https://gcc.gnu.org/onlinedocs/cpp/Include-Syntax.html – Himanshu Mishra Jul 04 '15 at 05:07