只是找了一些例子並運行,沒有總結出個規律來,想不清楚內部機制,大家一起給總結一下吧~
首先是跟宏定義結合,這是筆試題中可能遇到的題目,然後展開討論,關於後自增在邏輯運算符和加法運算符中的區別,但是沒有總結出規律,也不明白本質上怎麼破。。歡迎大家留言給予指導~
[cpp]
#define BAND1(x) (((x) >= 5)&&((x) <= 10) ? (x) : 0)
#define BAND2(x) (((x) >= 5)&&((x) <= 5) ? (x) : 0)
int main()
{
int a;
int i = 5;
printf("\ntest1----->>>>>>>>>>>>\n");
printf("BAND1(++i) = %d\n", BAND1(++i)); //8
printf("i = %d\n", i); //8
i = 5;
printf("\ntest2----->>>>>>>>>>>>\n");
printf("BAND1(i++) = %d\n", BAND1(i++)); //7
printf("i = %d\n", i); //8
i = 5;
printf("\ntest3----->>>>>>>>>>>>\n");
printf("BAND2(++i) = %d\n", BAND2(++i)); //0
printf("i = %d\n", i); //7
/*說明在((x) >= 5)&&((x) <= 5)部分執行了兩次自增 */
i = 5;
printf("\ntest4----->>>>>>>>>>>>\n");
printf("BAND2(i++) = %d\n", BAND2(i++)); //0
printf("i = %d\n", i); //7
/*說明在((x) >= 5)&&((x) <= 5)部分執行了兩次自增 */
i = 0;
a = (i++) + (i++) + (i++);
printf("\ntest5----->>>>>>>>>>>>\n");
printf("a = %d\n", a); //0
printf("i = %d\n", i); //3
/*問題是在條件運算符內部就自增了,可是加法時就沒有自增,自增操作的時機是?*/
i = 4;
a = (i++ >= 5);
printf("\ntest6----->>>>>>>>>>>>\n");
printf("a = %d\n", a); //0
printf("i = %d\n", i); //5
/*說明在(i++ >= 5)部分沒有自增,比較完後才自增 */
i = 4;
a = (i++ >= 5)+(i++ >= 5);
printf("\ntest7----->>>>>>>>>>>>\n");
printf("a = %d\n", a); //1
printf("i = %d\n", i); //6
/*說明在第一個(i++ >= 5)之後,第二個(i++ >= 5)之前自增, 再執行加法*/
i = 4;
a = (i++ >= 4)&&(i++ >= 5);
printf("\ntest8----->>>>>>>>>>>>\n");
printf("a = %d\n", a); //1
printf("i = %d\n", i); //6
/*說明在(i++ >= 4)之後,(i++ >= 5)之前自增, 再執行邏輯運算*/
i = 1;
a = (i--)&&(i);
printf("\ntest9----->>>>>>>>>>>>\n");
printf("a = %d\n", a); //0
printf("i = %d\n", i); //0
/*說明在(i--)之後,i之前自減,再執行邏輯運算*/
i = 1;
a = (i)&&(i--);
printf("\ntest10----->>>>>>>>>>>>\n");
printf("a = %d\n", a); //1
printf("i = %d\n", i); //0
/*說明在(i)之後,i--之前沒有自減,再執行邏輯運算*/
#define BAND1(x) (((x) >= 5)&&((x) <= 10) ? (x) : 0)
#define BAND2(x) (((x) >= 5)&&((x) <= 5) ? (x) : 0)
int main()
{
int a;
int i = 5;
printf("\ntest1----->>>>>>>>>>>>\n");
printf("BAND1(++i) = %d\n", BAND1(++i)); //8
printf("i = %d\n", i); //8
i = 5;
printf("\ntest2----->>>>>>>>>>>>\n");
printf("BAND1(i++) = %d\n", BAND1(i++)); //7
printf("i = %d\n", i); //8
i = 5;
printf("\ntest3----->>>>>>>>>>>>\n");
printf("BAND2(++i) = %d\n", BAND2(++i)); //0
printf("i = %d\n", i); //7
/*說明在((x) >= 5)&&((x) <= 5)部分執行了兩次自增 */
i = 5;
printf("\ntest4----->>>>>>>>>>>>\n");
printf("BAND2(i++) = %d\n", BAND2(i++)); //0
printf("i = %d\n", i); //7
/*說明在((x) >= 5)&&((x) <= 5)部分執行了兩次自增 */
i = 0;
a = (i++) + (i++) + (i++);
printf("\ntest5----->>>>>>>>>>>>\n");
printf("a = %d\n", a); //0
printf("i = %d\n", i); //3
/*問題是在條件運算符內部就自增了,可是加法時就沒有自增,自增操作的時機是?*/
i = 4;
a = (i++ >= 5);
printf("\ntest6----->>>>>>>>>>>>\n");
printf("a = %d\n", a); //0
printf("i = %d\n", i); //5
/*說明在(i++ >= 5)部分沒有自增,比較完後才自增 */
i = 4;
a = (i++ >= 5)+(i++ >= 5);
printf("\ntest7----->>>>>>>>>>>>\n");
printf("a = %d\n", a); //1
printf("i = %d\n", i); //6
/*說明在第一個(i++ >= 5)之後,第二個(i++ >= 5)之前自增, 再執行加法*/
i = 4;
a = (i++ >= 4)&&(i++ >= 5);
printf("\ntest8----->>>>>>>>>>>>\n");
printf("a = %d\n", a); //1
printf("i = %d\n", i); //6
/*說明在(i++ >= 4)之後,(i++ >= 5)之前自增, 再執行邏輯運算*/
i = 1;
a = (i--)&&(i);
printf("\ntest9----->>>>>>>>>>>>\n");
printf("a = %d\n", a); //0
printf("i = %d\n", i); //0
/*說明在(i--)之後,i之前自減,再執行邏輯運算*/
i = 1;
a = (i)&&(i--);
printf("\ntest10----->>>>>>>>>>>>\n");
printf("a = %d\n", a); //1
printf("i = %d\n", i); //0
/*說明在(i)之後,i--之前沒有自減,再執行邏輯運算*/ [cpp] view plaincopyprint?
i = 1;
a = (i)&&(i--)&&(i);
printf("\ntest11----->>>>>>>>>>>>\n");
printf("a = %d\n", a); //0
printf("i = %d\n", i); //0
/*說明在(i)之後,i--之前沒有自減,再執行邏輯運算*/
i = 1;
a = (i)&&(i--)&&(i);
printf("\ntest11----->>>>>>>>>>>>\n");
printf("a = %d\n", a); //0
printf("i = %d\n", i); //0
/*說明在(i)之後,i--之前沒有自減,再執行邏輯運算*/[cpp] view plaincopyprint? i = 1;
a = (i--)+(i);
printf("\ntest12----->>>>>>>>>>>>\n");
printf("a = %d\n", a); //2
printf("i = %d\n", i); //0
/*說明在i--之後,i之前沒有自減,在整個加法運算完後才自減*/
/*只能說明整型運算符和邏輯運算符不一樣。。。略亂。。。本質上怎麼說呢?*/
printf("DONE!\n");
system("pause");
return 0;
}
i = 1;
a = (i--)+(i);
printf("\ntest12----->>>>>>>>>>>>\n");
printf("a = %d\n", a); //2
printf("i = %d\n", i); //0
/*說明在i--之後,i之前沒有自減,在整個加法運算完後才自減*/
/*只能說明整型運算符和邏輯運算符不一樣。。。略亂。。。本質上怎麼說呢?*/
printf("DONE!\n");
system("pause");
return 0;
}跟同學討論過後,覺得他說的很有道理哈~
對於邏輯表達式&&,會把前後表達式均作為一個完整的表達式來處理,所以,&&之前,i有後自增時,就會執行自增,測試9、10、11可以對照說明;而加法運算符,就會要求所有的連加執行完後,再進行自增,這就把整個加法表達式作為一個最小粒度的完整的表達式來處理。
而有>=、<=等運算符的表達式,也會作為一個最小粒度的完整的表達式來處理,測試6、7可以對照說明。
自增運算符與指針結合:
[cpp]
int main()
{
char temp;
char s[] = "hello";
char *p = s;
temp = *p++;
printf("temp = %c\n", temp); //h
printf("*p = %c\n", *p); //e
/* 說明在*p++中,先取出*p的值h賦給temp,指針p再自增。
原因:++與*同級,但結合方向自右向左,所以,++先與p結合,作用在指針上,
再取*p的值,然後++作用於指針之上 */
p--; //使得初始狀態與上面的初始狀態相同
temp = *(p++);
printf("temp = %c\n", temp); //h
printf("*p = %c\n", *p); //e
/* 與第一個測試結果相同,說明了按照結合方向運算 */
temp = ++*p;
printf("temp = %c\n", temp); //f
printf("*p = %c\n", *p); //f
/*說明只是取*p的值e,並自增1,值為f,賦給temp。
按結合方向從右至左,說得通。*/
temp = ++(*p);
printf("temp = %c\n", temp); //g
printf("*p = %c\n", *p); //g
/* 與上面測試結果同規律,說明了按照結合方向運算 */
printf("DONE!\n");
system("pause");
return 0;
}
int main()
{
char temp;
char s[] = "hello";
char *p = s;
temp = *p++;
printf("temp = %c\n", temp); //h
printf("*p = %c\n", *p); //e
/* 說明在*p++中,先取出*p的值h賦給temp,指針p再自增。
原因:++與*同級,但結合方向自右向左,所以,++先與p結合,作用在指針上,
再取*p的值,然後++作用於指針之上 */
p--; //使得初始狀態與上面的初始狀態相同
temp = *(p++);
printf("temp = %c\n", temp); //h
printf("*p = %c\n", *p); //e
/* 與第一個測試結果相同,說明了按照結合方向運算 */
temp = ++*p;
printf("temp = %c\n", temp); //f
printf("*p = %c\n", *p); //f
/*說明只是取*p的值e,並自增1,值為f,賦給temp。
按結合方向從右至左,說得通。*/
temp = ++(*p);
printf("temp = %c\n", temp); //g
printf("*p = %c\n", *p); //g
/* 與上面測試結果同規律,說明了按照結合方向運算 */
printf("DONE!\n");
system("pause");
return 0;
}