I've read up a bit on preprocessor directives and I've seen #import being used a few times in C programs. I'm not sure what the difference is between them, some sites have said that #include is only used for header files and #import is used more in Java and is deprecated in C.
If that's the case, why do some programs still use #import and how exactly is it different from #include? Also, I've used #import in a few of my C programs and it seems to work fine and do the same thing as #include.
The difference between two things is the way in which they are unlike each other.
The most common use for among is when something is in or with a group of a few, several, or many things. The most common use of between is when something is in the middle of two things or two groups of things. It is sometimes used in the phrase in between.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With