1

Summary

1) In C language, the result returned by the ternary operator is an rvalue, not a variable (cannot be placed on the left side of the assignment symbol)

2) The return type of the ternary operator:

  • Return the higher types of b and c through the implicit type conversion char and short will be implicitly converted to int during operation)
  • Compilation error when b and c cannot be implicitly converted to the same type

3) Comma expressions (exp1, exp2, ... expN)

  • The comma expression is used to multiple sub-expressions into one expression
  • value of the comma expression is the value of the last sub-expression (the first N-1 sub-expressions can have no return value)
  • The calculation order is from left to right

4) Generally, line of code to achieve a certain function often needs to use recursion + comma expression + ternary operator

Ternary operator and comma expression

1. Ternary operator

Ternary operator (expression? A: b): When the value of expression is true, returns the value of a; otherwise, returns the value of b.

  • Code reading

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

The return type of the ternary operator (expression? A: b):

  • Return the higher types of b and c through the implicit type conversion rule
  • Compilation error when b and c cannot be implicitly converted to the same type

    char c = 0;
    short s = 0;
    int i = 0;
    double d = 0;
    char* p = "str";
    
    printf("%d\n", sizeof(c ? c : s));    // 4, 返回类型隐式转换为int
    printf("%d\n", sizeof(i ? i : d));    // 8,返回类型隐式转换为double
    printf("%d\n", sizeof(d ? d : p));    // error,double和char*不能隐式类型转换为同一类型

2. Comma expression

Comma expressions (exp1, exp2, ... expN)

  • The comma expression is used to multiple sub-expressions into one expression
  • value of the comma expression is the value of the last sub-expression (the first N-1 sub-expressions can have no return value)
  • The order of calculation is from left to right

    // 以下代码会输出什么?
    int i = 0;
    
    while (i < 5)
      printf("i = %d\n", i),
    
    i++;
    
    // 分析,这段代码并不会编译失败,也不会死循环,因为这是一个逗号表达式
    int i = 0;
    
    while(i < 5)
      printf("i = %d\n", i), i++;
  • // 以下代码会输出什么?
    int a[3][3] = {
      (0, 1, 2),
      (3, 4, 5),
      (6, 7, 8)
    }; 
    
    int i=0, j=0;
    for(i = 0; i<3; i++)
    {
      for(j = 0; j<3; j++)
      {
          printf("a[%d][%d] = %d\n", i, j, a[i][j]);
      }
    }
    
    // 分析
    这段代码并不会像乍看一样输出 0 ~ 8,而输出的是2, 5, 8, 0, 0, ...
    原因在于逗号表达式。初始化时使用的是逗号表达式,而不是花括号{}
    相当于:
    int a[3][3] = {2, 5, 8};

3. A line of code to achieve strlen

recursion + ternary operator + comma expression

  • Version one, null pointer is not considered, it will crash when the input parameter is NULL

    int myStrlen(const char* s)
    {
      return (*s ? myStrlen(s+1)+1 : 0);
    }
  • Version two, consider null pointers and use nested ternary operators

    int myStrlen(const char* s)
    {
      return (s == NULL ? -1 : *s ? myStrlen(s+1)+1 : 0);
    }
  • Version three, use assert assertion, comma expression + ternary operator

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

This article is summarized from "C Language Advanced Course" by Tang Zuolin from "Ditai Software Academy".
If there are any errors or omissions, please correct me.


bryson
169 声望12 粉丝