非常教程

C参考手册

C 语法

cast operator

执行明确的类型转换。

句法

( type-name ) expression

其中

类型名

-

无论是 void 类型还是任何标量类型

表达

-

标量类型的任何表达式(除非类型名称是无效的,在这种情况下,它可以是任何东西)

说明

如果 type-name 是void,那么表达式会计算其副作用,并将其返回值作为表达式语句放弃,与将表达式单独使用时相同。

否则,如果 type-name 恰好是表达式的类型,则不做任何事情(除非表达式具有浮动类型,并且表示的范围和精度比其类型指示的大 - 参见下文)。

否则,表达式的值将转换为由 type-name 命名的类型,如下所示:

每个隐式转换就像是通过赋值一样是允许的。

除了隐式转换之外,还允许进行以下转换:

  • 任何整数都可以转换为任何指针类型。除了空指针常量NULL(不需要强制转换)之外,结果是实现定义的,可能未正确对齐,可能不指向引用类型的对象,并且可能是陷阱表示。
  • 任何指针类型都可以转换为任何整数类型。结果是实现定义的,即使对于空指针值(它们不一定会导致值为零)。如果结果不能在目标类型中表示,则行为是未定义的(无符号整数不能对来自指针的强制执行模运算)
  • 任何指向对象的指针都可以转换为任何其他指向对象的指针。如果该值未针对目标类型正确对齐,则行为未定义。否则,如果该值被转换回原始类型,则它与原始值相等。如果指向对象的指针转换为指向任何字符类型的指针,则结果指向该对象的最低字节,并且可以递增至目标类型的大小(换言之,可用于检查对象表示或制作memcpy或通过复制memmove)。
  • 任何指向函数的指针都可以转换为指向任何其他函数类型的指针。如果生成的指针转换回原始类型,则它与原始值相等。如果转换的指针用于进行函数调用,则行为是未定义的(除非函数类型兼容)
  • 在指针(对象或函数)之间进行转换时,如果原始值是其类型的空指针值,则结果是目标类型的正确空指针值。

无论如何(在执行隐式转换时以及在同一类型转换中),如果 expression 和 type-name 是浮点类型,并且表达式的范围和精度比其类型所指示FLT_EVAL_METHOD的范围和精度要高(参见范围和精度被删除)关闭以匹配目标类型。

转换表达式的值类别始终是非左值。

笔记

因为const,volatile,restrict 和 atomic 限定符只对左值有影响,所以对 cvr 限定或原子类型的转换完全等同于转换为相应的非限定类型。

无效的转换有时可以用来消除关于未使用结果的编译器警告。

未列在这里的转换不被允许。尤其是,

  • 指针和浮点类型之间没有转换
  • 在指向函数的指针和指向对象的指针之间没有转换(包括void*
  • 在指向函数和整数的指针之间没有转换

#include <stdio.h>
 
int main(void)
{
    // examining object representation is a legitimate use of cast
    double d = 3.14;
    printf("The double %.2f(%a) is: ", d, d);
    for(size_t n = 0; n < sizeof d; ++n)
        printf("0x%02x ", ((unsigned char*)&d)[n]);
 
    // edge cases
    struct S {int x;} s;
//    (struct S)s; // error; not a scalar type
                   // even though casting to the same type does nothing
    (void)s; // okay to cast any type to void
}

可能的输出:

The double 3.14(0x1.91eb851eb851fp+1) is: 0x1f 0x85 0xeb 0x51 0xb8 0x1e 0x09 0x40

参考

  • C11 standard (ISO/IEC 9899:2011):
    • 6.5.4 Cast operators (p: 91)
  • C99 standard (ISO/IEC 9899:1999):
    • 6.5.4 Cast operators (p: 81)
  • C89/C90 standard (ISO/IEC 9899:1990):
    • 3.3.4 Cast operators

C 语法相关

1.#define directive
2.#elif directive
3.#else directive
4.#endif directive
5.#error directive
6.#if directive
7.#ifdef directive
8.#ifndef directive
9.#include directive
10.#line directive
11.#pragma directive
12.alignas
13.Alternative operators and tokens
14.Analyzability
15.Arithmetic operators
16.Arithmetic types
17.Array declaration
18.Array initialization
19.ASCII Chart
20.Assignment operators
21. types
22.Basic concepts
23.Bit fields
24.break statement
25.C language
26.C Operator Precedence
27.character constant
28.Comments
29.Comparison operators
30.compound literals
31.Conditional inclusion
32.Conformance
33.const type qualifier
34.Constant expressions
35.continue statement
36.Declarations
37.do-while loop
38.Enumerations
39.Escape sequences
40.Expressions
41.External and tentative definitions
42.File scope
43.floating constant
44.for loop
45.Function declarations
46.Function definitions
47.Functions
48.Generic selection
49.goto statement
50.Identifier
51.if statement
52.Implicit conversions
53.Increment/decrement operators
54.Initialization
55.inline function specifier
56.integer constant
57.Lifetime
58.Logical operators
59.Lookup and name spaces
60.Main function
61.Member access operators
62.Memory model
63.Objects and alignment
64.Order of evaluation
65.Other operators
66.Phases of translation
67.Pointer declaration
68.Preprocessor
69.restrict type qualifier
70.return statement
71.Scalar initialization
72.Scope
73.sizeof operator
74.Statements
75.static assert declaration
76.Static storage duration
77.Storage-class specifiers
78.string literals
79.Struct and union initialization
80.Struct declaration
81.switch statement
82.Thread storage duration
83.Type
84.Type
85.Typedef declaration
86.Undefined behavior
87.Union declaration
88.Value categories
89.Variadic arguments
90.volatile type qualifier
91.while loop
92._Alignof operator
93._Noreturn function specifier
C

C 语言是一门通用计算机编程语言,应用广泛。C 语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。