非常教程

C参考手册

C 语法

Logical operators

逻辑运算符对其操作数应用标准布尔代数运算。

Operator

Operator name

Example

Result

!

logical NOT

!a

the logical negation of a

&&

logical AND

a && b

the logical AND of a and b

||

logical OR

a || b

the logical OR of a and b

逻辑 NOT

逻辑 NOT 表达式具有这种形式。

! expression

其中

expression

-

an expression of any scalar type

逻辑 NOT 运算符具有类型int。它的值是​0​如果表达式评估为一个比较不等于零的值。它的值是1如果表达式计算出的值等于零。(如此!E相同(0==E))。

#include <stdbool.h>
#include <stdio.h>
#include <ctype.h>
int main(void)
{
    bool b = !(2+2 == 4); // not true
    printf("!(2+2==4) = %s\n", b ? "true" : "false");
 
    int n = isspace('a'); // zero if 'a' is a space, nonzero otherwise
    int x = !!n; // "bang-bang", common C idiom for mapping integers to [0,1]
                 // (all non-zero values become 1)
    char *a[2] = {"nonspace", "space"};
    printf("%s\n", a[x]); // now x can be safely used as an index to array of 2 ints
}

输出:

!(2+2==4) = false
nonspace

逻辑 AND

逻辑“与”表达式具有这种形式。

lhs && rhs

其中

lhs

-

an expression of any scalar type

rhs

-

an expression of any scalar type, which is only evaluated if lhs does not compare equal to ​0​

逻辑 AND 运算符具有类型int和值,1如果 lhs 和rhs 比较不等于零。它具有其他值​0​(如果lhs或rhs或两者都等于零)。

lhs评估后有一个序列点。如果lhs的结果等于零,则根本不评估rhs(所谓的短路评估)。

#include <stdbool.h>
#include <stdio.h>
int main(void)
{
    bool b = 2+2==4 && 2*2==4; // b == true
 
    1 > 2 && puts("this won't print");
 
    char *p = "abc";
    if(p && *p) // common C idiom: if p is not null
                // AND if p does not point at the end of the string
    {           // (note that thanks to short-circuit evaluation, this
                //  will not attempt to dereference a null pointer)
    // ...      // ... then do some string processing
    }
}

逻辑或

逻辑 OR 表达式具有表格。

lhs || rhs

其中

lhs

-

an expression of any scalar type

rhs

-

an expression of any scalar type, which is only evaluated if lhs compares equal to ​0​

如果lhs或rhs比较不等于零,则逻辑或运算符具有类型int和值1。它有价值​0​否则(如果lhs和rhs比较等于零)。

lhs 评估后有一个序列点。如果lhs的结果不等于零,则根本不评估 rhs(所谓的短路评估)。

#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main(void)
{
    bool b = 2+2 == 4 || 2+2 == 5; // true
    printf("true or false = %s\n", b ? "true" : "false");
 
    // logical OR can be used simialar to perl's "or die", as long as rhs has scalar type
    fopen("test.txt", "r") || printf("could not open test.txt: %s\n", strerror(errno));
}

可能的输出:

true or false = true
could not open test.txt: No such file or directory

参考

  • C11 standard (ISO/IEC 9899:2011):
    • 6.5.3.3 Unary arithmetic operators (p: 89)
    • 6.5.13 Logical AND operator (p: 99)
    • 6.5.14 Logical OR operator (p: 99)
  • C99 standard (ISO/IEC 9899:1999):
    • 6.5.3.3 Unary arithmetic operators (p: 79)
    • 6.5.13 Logical AND operator (p: 89)
    • 6.5.14 Logical OR operator (p: 89)
  • C89/C90 standard (ISO/IEC 9899:1990):
    • 3.3.3.3 Unary arithmetic operators
    • 3.3.13 Logical AND operator
    • 3.3.14 Logical OR operator

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.Expressions
42.External and tentative definitions
43.File scope
44.floating constant
45.for loop
46.Function declarations
47.Function definitions
48.Functions
49.Generic selection
50.goto statement
51.Identifier
52.if statement
53.Implicit conversions
54.Increment/decrement operators
55.Initialization
56.inline function specifier
57.integer constant
58.Lifetime
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 语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。