78

Is typeof in C, really an operator?
I'm thinking because there is no polymorphism in C, that there is nothing to do at run-time. That is, the answer to typeof is known at compile-time. (I can't think of a use of typeof that would not be known at compile time.) So it appears to be more of a compile-time directive, than an operator.

Does typeof use any (processor) run-time (in GCC)?

Doug
  • 2,783
  • 6
  • 33
  • 37

5 Answers5

99

Since typeof is a compiler extension, there is not really a definition for it, but in the tradition of C it would be an operator, e.g sizeof and _Alignof are also seen as an operators.

And you are mistaken, C has dynamic types that are only determined at run time: variable modified (VM) types.

size_t n = strtoull(argv[1], 0, 0);
double A[n][n];
typeof(A) B;

can only be determined at run time.

Add on in 2021: there are good chances that typeof with similar rules as for sizeof will make it into C23.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
  • 22
    Don't be afraid to link to [interesting sources](http://gustedt.wordpress.com/2011/01/09/dont-be-afraid-of-variably-modified-types/) regarding this topic :) – Nikolai Ruhe Aug 23 '12 at 16:17
  • 1
    @NikolaiRuhe, wouldn't have thought of that as a principal resource for VM types... but if you say so :) – Jens Gustedt Aug 23 '12 at 16:21
  • @dubbaluga, thanks for your suggested edits, I applied them myself, now. – Jens Gustedt Aug 26 '13 at 11:06
  • 3
    Whoops! The same holds for 'sizeof()': I thought the result was fixed at compile time, but now I see it is not. – not-a-user Jan 16 '14 at 18:58
  • @JensGustedt I' want to ask you if I can i call this a "dynamic type" the same way we call types in other languages like python as this is just plain old VLA which has known type of ```double``` with just missing boundaries sizes. Anyway, ```VLA's``` are not recommended as they generate lots of code and they are slow. ex: Linux removed all it;s – KMG May 29 '21 at 06:23
  • Yes, these are VLA, and no, they are not "slow", in the contrary. As given here they are not recommended, without checking that the allocation is not too big for the stack. There is a lot of half baked information and ideology about VM types floating around. – Jens Gustedt May 29 '21 at 07:14
  • @Khaled please start learning [from here](https://gustedt.wordpress.com/2014/09/08/dont-use-fake-matrices/). In particular, make sure to detour to the first two links in that article. To see modern XXI century C in action you might start from [systemd repository](https://raw.githubusercontent.com/systemd/systemd/main/src/basic/macro.h). HTH – Chef Gladiator Jul 02 '21 at 09:52
41

It's a GNU extension. In a nutshell it's a convenient way to declare an object having the same type as another. For example:

int x;         /* Plain old int variable. */
typeof(x) y;   /* Same type as x. Plain old int variable. */

It works entirely at compile-time and it's primarily used in macros. One famous example of macro relying on typeof is container_of.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • 8
    Don't think that it works entirely at compile, though I have to admit that I didn't check: I think it also works for VLA and other VM types. So this then can only be determined at run time. – Jens Gustedt Aug 22 '12 at 22:04
14

It is a C extension from the GCC compiler , see this.

Mehdi Charife
  • 722
  • 1
  • 7
  • 22
André Oriani
  • 3,553
  • 22
  • 29
5

It's not exactly an operator, rather a keyword. And no, it doesn't do any runtime-magic.

Antonio
  • 19,451
  • 13
  • 99
  • 197
1

One case in which you will need to use this extension is to get the pointer of an anonymous structs. You can check this this question for an example.