87

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

5
  • 49
    C has no typeof operator, that is a compiler extension. Commented Aug 22, 2012 at 21:16
  • 7
    @EdS. I'm patiently waiting for the day where I can flag your comment for being outdated, no offence. :) Commented Nov 17, 2021 at 17:26
  • 1
    @user426 I'm finna become a Jedi master waiting this long for the C23 committee to add no language features *sobs into pillow*. Commented Apr 8, 2022 at 12:10
  • 4
    And the day came! Typeof is now a standard feature (of C23). Commented Jan 29, 2024 at 15:40
  • 4
    @user426 Feel free to flag it as outdated now. C23 has typeof. :) Commented May 26, 2024 at 18:03

6 Answers 6

110

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 2024: typeof with similar rules as for sizeof made it into C23.

Sign up to request clarification or add additional context in comments.

7 Comments

Don't be afraid to link to interesting sources regarding this topic :)
@NikolaiRuhe, wouldn't have thought of that as a principal resource for VM types... but if you say so :)
@dubbaluga, thanks for your suggested edits, I applied them myself, now.
Whoops! The same holds for 'sizeof()': I thought the result was fixed at compile time, but now I see it is not.
@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
|
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.

1 Comment

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

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

Comments

5

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

1 Comment

(as explained in other answers, there are cases in which it does operate at runtime)
2

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.

2 Comments

There's a simpler solution, and it's fully C89-compliant: tag your structs.
I needed it to deal with a code that I can't change.
0

There is no function like that in c to determine, But you can use preprocessor + GCC feature

#define typeof(x) _Generic((x), \
    int: "int", \
    float: "float", \
    double: "double", \
    char: "char", \
    default: "unknown")

And this is an example of how to use it

int x = 10;
printf("Typeof x: %s \n", typeof(x));

1 Comment

This doesn't really provide the same functionality as typeof() since the output of the _Generic() is a character string. You can do int a; char cc[] = typeof(a); however you can't do int a; typeof(a) b; since it doesn't return the type but rather a string indicating the type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.