13

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.

AkThao
  • 582
  • 4
  • 7
  • 23
  • 6
    `#import` is not standard C, so it's somewhat hard to answer this question. – Oliver Charlesworth Sep 01 '16 at 20:20
  • 2
    `#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 Sep 01 '16 at 20:27
  • 1
    @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 Sep 01 '16 at 20:30
  • @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 Sep 01 '16 at 21:29

2 Answers2

10

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.

rici
  • 234,347
  • 28
  • 237
  • 341
  • So `#import` is still able to be used because it's part of other derivates of C (like Objective-C), but it's just not native in C itself? – AkThao Sep 02 '16 at 10:43
  • @kashveyron: if you are using a compiler whose preprocessor also supports objective-C.But it won't work with Microsoft compilers, f9e example. – rici Sep 02 '16 at 13:42
  • Oh that makes more sense, so it's basically whatever the compiler already has and knows. – AkThao Sep 02 '16 at 15:03
  • @rici I didn't really understand what you meant by this: "But `#pragma once` is a much better solution, because the includer shouldn't need to know whether duplicate inclusion is acceptable". Would care to comment on this? – Alexander Apr 26 '20 at 20:44
  • @alexander: not all header files need include guards. Indeed, some headers such as `assert.h` are designed to be included more than once. Whether or not a header file should be guarded is something known to whoever wrote that file, and that's where the guard belongs. It's an implementation detail, and the file which includes the header should not have to use two different syntaxes depending on the file being included. I hope that makes it clearer. – rici Apr 27 '20 at 02:14
4

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.

jamesdlin
  • 81,374
  • 13
  • 159
  • 204