10
votes

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.

2
#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
@JohnBollinger: Yes. Unfortunately, the cpp manual (in the gcc distribution) calls it "deprecated" because it was part of Objective-C and thus many cpp implementations do in fact implement it. But you can deprecate an extension, which is a way of saying "this extension will never be part of the standard." That's common in ECMAscript and not unknown in the C family.rici
@JohnBollinger: The C language was defined by primarily by precedent prior to C89, and the language which became popular throughout the 1990s was a combined superset of C89 and useful precedents which filled in the gaps thereof. For the Standard to recognize the existence of precedents but consider them "deprecated" would be better than its usual treatment of behaviors that used to be common to the majority of implementations but weren't common to all.supercat

2 Answers

9
votes

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):

  1. 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.

  2. 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.

3
votes

As mentioned in comments, #import is not standard and can mean different things for different compilers.

With Microsoft's compiler, for example, #import can automatically generate and include a header file at compilation time.