This is well-explained in the Gnu CPP (C preprocessor) manual, although the behaviour is the same in clang (and possibly other C compilers, but not MSVC):
The problem. Summary: You don't usually want to include the same header twice into a single translation unit, because that can lead to duplicate declarations, which is an error. However, since included files may themselves want to include other files, it is hard to avoid.
Some non-standard solutions (including #import
). Summary: #import
in the including file and #pragma once
in the included file both prevent duplicate inclusion. But #pragma once
is a much better solution, because the includer shouldn't need to know whether duplicate inclusion is acceptable.
The linked document calls #import
a "deprecated extension", which is a slightly odd way of describing a feature which was never part of any standard C version. But it's not totally meaningless: many preprocessor implementations do allow #import
(which is a feature of Objective-C), so it is a common extension. Calling it deprecated is a way of saying that the extension will never be part of any C standard, regardless of how widespread implementations are.
If you want to use an extension, use #pragma once
; that also might not be present in a future standard, but changing it for a given header file will only require a change in one place instead of in every file which includes the header. C++ and even C are likely at some point to develop some kind of module feature which will allow inclusion guards to finally be replaced.
#import
is not standard C, so it's somewhat hard to answer this question. – Oliver Charlesworth#import
is not deprecated in standard C, because it has never been part of any version of standard C, nor of K&R C. – John Bollinger