那天有人問我 a++ 與 ++a的差別在哪裡,雖然這是一個很簡單的觀念
但++a很少用 所以也常會忘記,所以就在此說明一下這兩個之間的差別
a++ ->>會先執行整個敘述後再將a的值加1
++a ->>先把a的值加1,再執行整個敘述
相信你們現在一定還是霧煞煞,看個例子吧
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
int main(int argc, char *argv[]) | |
{ | |
int a=3,b=3; | |
printf("a=%d",a); | |
printf(", a++的回傳值為%d",a++); | |
printf(", a=%d\n",a); | |
printf("b=%d",b); | |
printf(", ++b的回傳值為%d",++b); | |
printf(", b=%d\n",b); | |
system("PAUSE"); | |
return 0; | |
} |
文章標籤
全站熱搜