Introduction to typeof keywords

Source: Internet
Author: User

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 restrictions

Please 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 voiceTypeof

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.