Typeof is used today. Let's take a look at its usage.
TypeofThe keyword is a new extension in C. As long as it is acceptableTypedefName, the Sun Studio C compiler can acceptTypeofIncluding the following syntax categories:
- Statement
- Parameter type linked list and return type in function declaration
- Type Definition
- Type operator s
- SizeofOperator
- Compound text
- TypeofReal Parameters
The compiler accepts double-underline keywords:_ TypeofAnd_ Typeof __. The examples in this Article do not follow the conventions of double-underline. In terms of statement composition,TypeofThe keyword is enclosed in parentheses, including the type or expression name. This is similarSizeofOperands accepted by the keyword (SizeofThe difference is that bit fields can be usedTypeofReal parameters, and are interpreted as corresponding integer types ). In terms of semantics,TypeofThe keyword is used as the type name (typedef name) and the type is specified.
Use TypeofDeclaration example
The following two equivalent declarations are used to declareIntType VariableA.
typeof(int) a; /* Specifies variable a which is of the type int */
typeof('b') a; /* The same. typeof argument is an expression consisting of
character constant which has the type int */
The following example is used to declare pointers and arrays. For comparisonTypeof.
typeof(int *) p1, p2; /* Declares two int pointers p1, p2 */
int *p1, *p2;
typeof(int) * p3, p4;/* Declares int pointer p3 and int p4 */
int * p3, p4;
typeof(int [10]) a1, a2;/* Declares two arrays of integers */
int a1[10], a2[10];
IfTypeofUsed for expressions, the expression is not executed. Only the type of the expression is obtained. The following example declares the int typeVarVariable, because the expressionFoo ()YesIntType. Because the expression will not be executed, it will not be calledFooFunction.
extern int foo();
typeof(foo()) var;
UseTypeofStatement restrictionsPlease note that,TypeofThe type name in the constructor cannot contain the storage class specifier, as shown in figureExternOrStatic. However, type delimiters are allowed, suchConstOrVolatile. For example, the following code is invalid because itTypeofDescription in ConstructionExtern:
typeof(extern int) a;
The following code uses an external link to declare an identifier:BIs valid, indicatingIntType object. The next statement is also valid. It declares a usageConstQualifierCharType pointer, indicating pointerPCannot be modified.
extern typeof(int) b;
typeof(char * const) p = "a";
Use in macro voiceTypeofTypeofThe main application of construction is used in macro definition. AvailableTypeofKeyword to reference the type of macro parameters. Therefore, it is possible to construct an object with the required type without explicitly specifying the type name as a macro real parameter.