C语言进阶剖析 18 三目运算符和逗号表达式

三目运算符

  • 三目运算符(a ? b : c)可以作为逻辑运算的载体
  • 规则 : 当 a 为真时,返回 b 的值; 否则返回 C 的值

下面的程序运行结束后,a,b,c的值分别会是多少呢?

int a = 1;
int b = 2;
int c = 0;
c = a < b ? a : b;
(a < b ? a : b) = 3;  // error: lvalue required as left operand of assignment

实例分析: 三目运算符初探

#include <stdio.h>

int main()
{
    int a = 1;
    int b = 2;
    int c = 0;
    
    c = a < b ? a : b;
    
    // (a < b ? a : b) = 3;
    
    printf("%d\n", a);
    printf("%d\n", b);
    printf("%d\n", c);
}
输出:
1
2
1

分析:【三目运算符返回 a 或 b 的数值】
(a < b ? a : b) = 3; ==> error: lvalue required as left operand of assignment

对变量进行操作:

  • *(a < b ? &a : &b) = 3
  • 三目运算符的返回类型
        ○ 通过隐式类型转换规则返回 b 和 c 中的较高类型
        ○ 当 b 和 c 不能隐式转换到统一类型时将编译出错

实例分析: 三目运算符的返回类型

#include <stdio.h>

int main()
{
    char c = 0;
    short s = 0;
    int i = 0;
    double d = 0;
    char* p = "str";
    
    printf("%d\n", sizeof(c ? c : s));
    printf("%d\n", sizeof(i ? i : d));
    // printf("%d\n", sizeof(d ? d : p));

    return 0;
}
输出:
4
8
分析:
d ? d : p ==> error: type mismatch in conditional expression

逗号表达式

  • 逗号表达式是 C 语言的 "粘贴剂"
  • 逗号表达式用于将多个子表达式连接为一个表达式
  • 逗号表达式的值为最后一个子表达式的值
  • 逗号表达式中的前 N-1 个子表达式可以没有返回值
  • 逗号表达式按照从左向右的顺序计算每个子表达式的值
        ○ exp1, exp2, exp3, …, expN;

实例分析:逗号表达式的示例

#include <stdio.h>

void hello(void)
{
    printf("Hello\n");
}

int main()
{
    int a[3][3] = {
        (0, 1, 2),
        (3, 4, 5),
        (7, 8, 9)
    };
    int i = 0;
    int j = 0;
    
    while( i < 5 )
        printf("i = %d\n", i),
    
    hello(),
    
    i ++;
    
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            printf("a[%d][%d] = %d\n", i, j, a[i][j]);
        }
    }

    return 0;
}
输出:
i = 0
Hello
i = 1
Hello
i = 2
Hello
i = 3
Hello
i = 4
Hello
a[0][0] = 2
a[0][1] = 5
a[0][2] = 9
a[1][0] = 0
a[1][1] = 0
a[1][2] = 0
a[2][0] = 0
a[2][1] = 0
a[2][2] = 0

分析:
while( i < 5 )
    printf("i = %d\n", i),

hello(),

i ++;
==> while( i < 5 ) printf("i = %d\n", i), hello(), i ++;
----------------------------------------------------------
    int a[3][3] = {
    (0, 1, 2),
    (3, 4, 5),
    (7, 8, 9)
};
==>
int a[3][3] = {
    (2),
    (5),
    (9)
};

编程实验: 一行代码实现 strlen

#include <stdio.h>
#include <assert.h>

int strlen_g(const char* s)
{
    return (assert(s), *s ? strlen_g(s + 1) + 1 : 0);
}

int main()
{
    printf("%d\n", strlen_g("D.T.Software"));
    printf("%d\n", strlen_g(NULL));

    return 0;
}
输出:
12
a.out: test.c:6: strlen: Assertion `s' failed.
Aborted


小结

  • 三目运算符返回变量的值,而不是变量本身
  • 三目运算符通过隐式类型转换规则确认返回值类型
  • 逗号表达式按照从左向右的顺序计算每个子表达式的值
  • 逗号表达式的值为最后一个子表达式的值

内容参考狄泰软件学院系列课程,如有侵权,请联系作者删除!感谢~

已标记关键词 清除标记
<div class="post-text" itemprop="text"> <h3>What is this?</h3> <p>This is a number of answers about warnings, errors and notices you might encounter while programming PHP and have no clue how to fix. This is also a Community Wiki, so everyone is invited to participate in adding to and maintaining this list. </p> <h3>Why is this?</h3> <p>Questions like <a href="https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12770075#12770075">"Headers already sent"</a> or <a href="https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12769983#12769983">"Calling a member of a non-object"</a> pop up frequently on Stack Overflow. The root cause of those questions is always the same. So the answers to those questions typically repeat them and then show the OP which line to change in his/her particular case. These answers do not add any value to the site because they only apply to the OP's particular code. Other users having the same error can not easily read the solution out of it because they are too localized. That is sad, because once you understood the root cause, fixing the error is trivial. Hence, this list tries to explain the solution in a general way to apply.</p> <h3>What should I do here?</h3> <p>If your question has been marked as a duplicate of this, please find your error message below and apply the fix to your code. The answers usually contain further links to investigate in case it shouldn't be clear from the general answer alone.</p> <p>If you want to contribute, please add your "favorite" error message, warning or notice, one per answer, a short description what it means (even if it is only highlighting terms to their manual page), a possible solution or debugging approach and a listing of existing Q&A that are of value. Also, feel free to improve any existing answers.</p> <h2>The List</h2> <ul> <li><a href="https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12772851#12772851">Nothing is seen. The page is empty and white.</a> (also known as <em>White Page/Screen Of Death</em>)</li> <li><a href="https://stackoverflow.com/a/24423272/476">Code doesn't run/what looks like parts of my PHP code are output</a></li> <li><a href="https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12770075#12770075">Warning: Cannot modify header information - headers already sent</a></li> <li><a href="https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12770072#12770072">Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given</a> a.k.a.<br> <a href="https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12770072#12770072">Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource</a> a.k.a.<br> <a href="https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12770072#12770072">Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given</a> (or similar variations)</li> <li><a href="https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/14067623#14067623">Warning: <em>[function]</em> expects parameter 1 to be resource, boolean given</a></li> <li><a href="https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12820243#12820243">Warning: <em>[function]</em>: failed to open stream: <em>[reason]</em></a></li> <li><a href="https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12772244#12772244">Warning: open_basedir restriction in effect</a></li> <li><a href="https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12770089/#21507314">Warning: Division by zero</a></li> <li><a href="https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12770089/#27678436">Warning: Illegal string offset 'XXX'</a></li> <li><a href="https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/22316776#22316776">Parse error: syntax error, unexpected '['</a></li> <li><a href="https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12770089/#12770089">Parse error: syntax error, unexpected T_XXX</a></li> <li><a href="https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/13935532#13935532">Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE</a></li> <li><a href="https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12770129#12770129">Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM</a></li> <li><a href="https://stackoverflow.com/questions/23726288/php-error-unexpected-require-once-expecting-function">Parse error: syntax error, unexpected 'require_once' (T_REQUIRE_ONCE), expecting function (T_FUNCTION)</a></li> <li><a href="https://stackoverflow.com/a/15539535/1723893">Parse error: syntax error, unexpected T_VARIABLE</a></li> <li><a href="https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12770179#12770179">Fatal error: Allowed memory size of XXX bytes exhausted (tried to allocate XXX bytes)</a></li> <li><a href="https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12769983#12769983">Fatal error: Call to a member function ... on a non-object</a></li> <li><a href="https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12784048#12784048">Fatal Error: Call to Undefined function XXX</a></li> <li><a href="https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/13879461#13879461">Fatal Error: Cannot redeclare XXX</a></li> <li><a href="https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12770530#12770530">Fatal error: Can't use function return value in write context</a></li> <li><a href="https://stackoverflow.com/questions/1885302/php-what-does-this-error-mean-abstract-method-must-be-compatible">Fatal error: Declaration of AAA::BBB() must be compatible with that of CCC::BBB()</a> '</li> <li><a href="https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12770317#12770317">Fatal error: Using $this when not in object context</a></li> <li><a href="https://stackoverflow.com/a/24507107/476">Notice: Array to string conversion</a></li> <li><a href="https://stackoverflow.com/a/26572398/367456">Notice: Trying to get property of non-object error</a></li> <li><a href="https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12778634#12778634">Notice: Undefined variable</a></li> <li><a href="https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12770836#12770836">Notice: Undefined Index</a></li> <li><a href="https://stackoverflow.com/a/21793407/3933332">Notice: Undefined offset XXX</a> [Reference]</li> <li><a href="https://stackoverflow.com/a/17630869/367456">Notice: Uninitialized string offset: XXX</a></li> <li><a href="https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12773272#12773272">Notice: Use of undefined constant XXX - assumed 'XXX'</a></li> <li><a href="https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12771239#12771239">MySQL: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ... at line ...</a></li> <li><a href="https://stackoverflow.com/a/21942832/367456">Strict Standards: Non-static method <em>[<class>::<method>]</em> should not be called statically</a></li> <li><a href="https://stackoverflow.com/a/52771226/1255289">Warning: function expects parameter X to be boolean/string/integer</a></li> </ul> <p>Also see </p> <ul> <li><a href="https://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php">Reference - What does this symbol mean in PHP?</a></li> </ul> </div> <p>转载于:https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php</p>
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页