title | description | ms.date | helpviewer_keywords | ||
---|---|---|---|---|---|
typeof, __typeof__ (C23) | Describes Microsoft Visual C23 typeof operator | 02/06/2024 |
|
New in the C23 standard, the typeof
operator is a unary operator that returns the type of an expression. It can be used in type declarations, type casts, type checks, and so on. It gets the type of a variable, function, or any C expression.
The __typeof__
keyword is a Microsoft-specific extension that provides the same functionality as typeof
. The __typeof__
keyword differs from typeof
only in that it's available when compiling for all versions of C (not just /std:clatest
), and it may ease porting code between other compilers that support __typeof__
.
typeof(type) typeof(constant-expression) __typeof__(constant-expression)
This example uses typeof()
, but the behavior is the same if you use __typeof__
.
// Compile with /std:clatest#include<stdio.h>doublefunc() { 3.14; } #definePOINTER(T) typeof(T*) intmain() { auto a=func(); // the type for a (double) is inferred, but requires initialization at point of declarationtypeof(func()) b; // the type for b is double, but didn't have to be initialized at point of declaration// Some declarations using typeofPOINTER(int) p1=NULL; // p1 is int*typeof(double(void))*pFunc=func; // pFunc is a pointer to a function that takes no arguments and returns a doubleprintf("pFunc() returns %f\n", pFunc()); return0; }
Requires Visual Studio 17.9 or later, or cl.exe
version 19.39.33428 or later. To use typeof
, compile with /std:clatest
.