72

In Objective-C I often use __typeof__(obj) when dealing with blocks etc. Why not __typeof(obj) or typeof(obj).

When to use which?

Cœur
  • 37,241
  • 25
  • 195
  • 267
hfossli
  • 22,616
  • 10
  • 116
  • 130
  • 3
    I know someone else asked a similar question, but it unfortunately didn't cover all three. http://stackoverflow.com/questions/7618873/is-there-any-difference-between-typeof-and-typeof and was for c++ – hfossli Feb 14 '13 at 14:49
  • 1
    Hmm. I've always used constructs like `[obj isKindOf:otherObj]`. – ott-- Feb 14 '13 at 14:56
  • 1
    Sorry, yes. isKindOfClass is something else. – ott-- Feb 14 '13 at 15:36
  • 4
    `-isKindOfClass:` is a runtime check on the class of an object. The `typeof()` family is a compile-time check on the type (not just class) of any valid C expression. – Jonathan Grynspan Feb 14 '13 at 15:47
  • Can we delete these off topic comments? I delete mine. – hfossli Aug 15 '16 at 09:58
  • 1
    https://clang.llvm.org/docs/UsersManual.html#c-language-features explains `typeof`, `__typeof` and `__typeof__` – DawnSong Jun 06 '19 at 08:25

3 Answers3

65

__typeof__() and __typeof() are compiler-specific extensions to the C language, because standard C does not include such an operator. Standard C requires compilers to prefix language extensions with a double-underscore (which is also why you should never do so for your own functions, variables, etc.)

typeof() is exactly the same, but throws the underscores out the window with the understanding that every modern compiler supports it. (Actually, now that I think about it, Visual C++ might not. It does support decltype() though, which generally provides the same behaviour as typeof().)

All three mean the same thing, but none are standard C so a conforming compiler may choose to make any mean something different.

Jonathan Grynspan
  • 43,286
  • 8
  • 74
  • 104
  • 4
    The sentence "If you are writing a header file that must work when included in ISO C programs, write `__typeof__` instead of `typeof`" on the following page seems to imply some sort of "standard" here though: http://gcc.gnu.org/onlinedocs/gcc/Typeof.html – trojanfoe Feb 14 '13 at 15:37
  • 5
    Yes, in that non-standard extensions to the language should be prefixed with `__` to mark them as non-standard. `typeof()` is technically *bad* C, not just *nonstandard* C. – Jonathan Grynspan Feb 14 '13 at 15:39
  • 2
    OK nice one. I've never had any cause to use this feature in the 20-odd years of C programming I've done. I'm not about to start now ;-) – trojanfoe Feb 14 '13 at 15:42
  • 5
    So what's the difference between `__typeof()` and `__typeof__()`? – hfossli Feb 15 '13 at 08:39
  • 1
    As far as I'm aware, they do the same thing. – Jonathan Grynspan Feb 15 '13 at 20:19
33

As others have mentioned, typeof() is an extension of C that has various support in respective compilers.
If you happen to be writing Objective-C for iOS or Mac apps, chances are good that you will be compiling your app with the Clang compiler.

Clang does support the use of typeof(), but technically it's for when your C Language Dialect is set to be a gnu* type. However __typeof__() is supported in both c* and gnu* language dialects - as detailed in the Clang documentation.

Now if you're writing your code with Xcode, the default setting for the C language dialect appears to be GNU99 and the option of allowing 'asm' 'inline' 'typeof' is set to Yes, so using typeof() won't bring you any problems.

Xcode project settings

If you want to be (arguably) safer when using the Clang compiler, use __typeof__(). This way you won't be affected if the C Language Dialect being used for compilation changes or if someone decides to turn off the allowance of 'typeof'.

MultiColourPixel
  • 1,222
  • 10
  • 19
6

Hope this will be helpfull:

-ansi and the various -std options disable certain keywords. This causes trouble when you want to use GNU C extensions, or a general-purpose header file that should be usable by all programs, including ISO C programs. The keywords asm, typeof and inline are not available in programs compiled with -ansi or -std (although inline can be used in a program compiled with -std=c99 or -std=c11). The ISO C99 keyword restrict is only available when -std=gnu99 (which will eventually be the default) or -std=c99 (or the equivalent -std=iso9899:1999), or an option for a later standard version, is used.

The way to solve these problems is to put ‘__’ at the beginning and end of each problematical keyword. For example, use __asm__ instead of asm, and __inline__ instead of inline.

http://gcc.gnu.org/onlinedocs/gcc/Alternate-Keywords.html#Alternate-Keywords

https://clang.llvm.org/docs/UsersManual.html#c-language-features

DawnSong
  • 4,752
  • 2
  • 38
  • 38
parbo
  • 243
  • 3
  • 7
  • 3
    This explains `__typeof__`, but what about `__typeof`? – Ciro Santilli OurBigBook.com May 09 '15 at 21:49
  • 4
    @CiroSantilli新疆改造中心996ICU六四事件 AFAIK GCC/clang support to wrap non-standard keywords with `__` on both sides, however, it will not enforce the presence of the trailing `__`. So `__typeof` is just a `__typeof__` where the trailing underscores are missing and the compiler doesn't care. Works also with `__asm` and `__inline`. – Mecki Sep 05 '19 at 11:59