1. three mesh operator
(1) Three mesh operator (A?B:C) can be used as a carrier of logical operation
(2) Rule: When the value of a is true, the value of the variable B is returned (not the variable itself ); otherwise, the value of C is returned.
Preliminary study on "Case analysis" trinocular operator
#include <stdio.h>intMain () {intA =1; intb =2; intc =0; C= a < b? A:B;//(a<b)? a:b; //The following code attempts to assign a or B variable to a value selectively based on the condition. But this kind of//The code is wrong because the return value of the trinocular operator is the value of a or B variable,//instead of a or B variable itself, the following code is equivalent to 1=3; or 2=3;//that is to assign a value of 3 to 1 or 2. The compiler will make an error. //(A < b? a:b) = 3;//Error//to achieve the above purposes, the following code can be changed, as the Trinocular returns a variable//The address of A or B, not the value of a variable a or B. * (A < b? &a:&b) =3; printf ("A =%d\n", a);//3printf"B =%d\n", b);//2printf"C =%d\n", c);//1 return 0;}
(3) return type of three-mesh operator (A?B:C)
① returns a higher type in B or C through implicit type conversion rules
② Compile error when B and C cannot be implicitly converted to the same type.
return type of the "instance analysis" Trinocular operator
#include <stdio.h>intMain () {Charc =0; Shorts =0; inti =0; DoubleD =0; Char* p ="Str"; printf ("A =%d\n",sizeof(c?c:s));//Convert char and short implicitly to int, resulting in 4printf"B =%d\n",sizeof(I?I:D));//implicitly converts I to double with a result of 8//printf ("c =%d\n", sizeof (D?D:P));//error because the pointer cannot be implicitly converted to the base type return 0;}
2. comma expression:exp1,exp2,epx3,...,expN;
(1) The comma expression is the "binder" in the C language
(2) A comma expression is used to concatenate multiple sub-expressions into an expression
(3) The value of the comma expression is the value of the last sub-expression
(4) The first N-1 subexpression in a comma expression can have no return value
(5) The comma expression evaluates the value of each sub-expression in the left-to- right order
Example of an instance analysis comma expression
#include <stdio.h>voidHello () {printf ("hello!\n");}intMain () {inta[3][3] = { (0,1,2),//Note is a comma expression, not {0,1,2}. equivalent to a[0]=2;(3,4,5),//Note is a comma expression, not {3,4,5}. equivalent to a[1]=5;(6,7,8)//Note is a comma expression, not {3,4,5}. equivalent to a[1]=8; }; //int a[3][3] = {//{0, 1, 2},//is curly braces, equivalent to a[0]=0,a[1]=1,a[2]=2, and so on! //{3, 4, 5},//{6, 7, 8}//}; inti =0; intj =0; //Note that the following is a comma expression, and the following is a dead loop? //The answer is: No! Because the following statement is equivalent to//While (i<5)//printf ("i=%d\n", i), hello (), i++; //for the while statement, the loop body is the semicolon end (not the comma)//so the equivalent is the curly brace: {printf ("i=%d\n", i), hello (), i++;} while(I <5) printf ("i=%d\n", i),//It's a comma, not a semicolon! 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;}
"Programming Experiment" a line of code implementation strlen
#include <stdio.h>#include<assert.h>//One line of code implementation strlenintStrlenConst Char*s) { returnASSERT (s), (*s? strlen (S +1) +1:0);}intMain () {printf ("len =%d\n", strlen ("Hello world!\n"));//len=13;printf"len =%d\n", strlen (NULL));//passing in a null pointer will assert failure! return 0;}
3. Summary
(1) The three-mesh operator Returns the value of the variable, not the variable itself
(2) Three-mesh operator confirms return value type by implicit type conversion rules
(3) The comma expression evaluates the value of each sub-expression in the left-to-right order
(4) The value of the comma expression is the value of the last sub-expression .
Lesson 18th three-mesh and comma expressions