非常教程

C参考手册

C 语法

Expressions

表达式是一系列运算符及其操作数,用于指定计算。

表达式评估可能会产生一个结果(例如,2+2产生结果的评估4),可能会产生副作用(例如,printf("%d",4)将字符发送'4'到标准输出流的评估),并可能指定对象或函数。

通常

  • 值类别(左值,非左值对象,函数指示符)通过它们的值对表达式进行分类
  • 参数和子表达式的评估顺序指定了获得中间结果的顺序

操作符

| Common operators |

|:----|

| assignment | incrementdecrement | arithmetic | logical | comparison | memberaccess | other |

| a = b a += b a -= b a *= b a /= b a %= b a &= b a |= b a ^= b a <<= b a >>= b. | ++a --a a++ a-- | +a -a a + b a - b a * b a / b a % b ~a a & b a | b a ^ b a << b a >> b. | !a a && b a || b. | a == b a != b a < b a > b a <= b a >= b. | ab *a &a a->b a.b. | a(...) a, b (type) a ? : sizeof _Alignof (since C11). |

  • 运算符优先级定义了运算符绑定到它们的参数的顺序
  • 备选表述是一些运营商的备选拼写

转换

  • 隐式转换发生在操作数的类型与操作符的期望不匹配时
  • 强制转换可用于将值从一种类型显式转换为另一种类型。

Other

  • 可以在编译时计算常量表达式并在编译时上下文中使用(非 VLA 数组大小,静态初始化器等)
  • 通用选择可以根据参数的类型执行不同的表达式
  • 浮点表达式可能会引发异常并报告 math_errhandling 中指定的错误
  • 标准编译指示FENV_ACCESSFP_CONTRACTCX_LIMITED_RANGE以及浮点评价精度和舍入方向控制浮点表达式的执行方式。

主要表达式

任何操作符的操作数可能是其他表达式,或者它们可能是主要表达式(例如,在1+2*3,操作符的操作数是子表达式2*3和主表达式1)。

主要表达式有以下几种:

1)常量和文字(例如2"Hello, world"

2)适当地声明的标识符(例如nprintf

3)通用选择

括号中的任何表达式也被分类为主要表达式:这保证括号的优先级高于任何运算符。

常量和文字

某些类型的常量值可以使用称为文字(用于左值表达式)和常量(用于非左值表达式)的专用表达式嵌入C程序的源代码中。

  • 整数常量是整数类型的十进制,八进制或十六进制数字。
  • 字符常数是int类型的单个字符适于转换为字符类型或类型char16_tchar32_t(因为C11)或wchar_t
  • 浮动常数类型的值floatdoublelong double
  • 字符串文字的类型的字符序列char[]char16_t[]char32_t[],或wchar_t[]表示空终止字符串
  • 复合文字是直接嵌入程序代码中的结构体,联合体或数组类型的值

未评估的表达式

sizeof 运算符的操作数,_Alignof 运算符和一般选择的控制表达式(自C11以来)是不被评估的表达式(除非它们是 VLA)(自C99以来)。因此,size_t n = sizeof(printf("%d", 4));不执行控制台输出。

参考

  • C11 standard (ISO/IEC 9899:2011):
    • 6.5 Expressions (p: 76-105)
    • 6.6 Constant expressions (p: 106-107)
  • C99 standard (ISO/IEC 9899:1999):
    • 6.5 Expressions (p: 67-94)
    • 6.6 Constant expressions (p: 95-96)
  • C89/C90 standard (ISO/IEC 9899:1990):
    • 3.3 EXPRESSIONS
    • 3.4 CONSTANT EXPRESSIONS

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.cast operator
28.character constant
29.Comments
30.Comparison operators
31.compound literals
32.Conditional inclusion
33.Conformance
34.const type qualifier
35.Constant expressions
36.continue statement
37.Declarations
38.do-while loop
39.Enumerations
40.Escape sequences
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 语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。