Trending News
while()的用法~~這2種有何不同!!
不才在開發一個flimware程式時遇到了小問題~~最後Debug發現出原因在這~~所以想請教一下while()的用法
例如一個程式是這樣:
寫法-->1
#include<stdlib.h>
#include<stdio.h>
int start(int x,char *b);
int nostart(int x,char *b);
void linst();
int main()
{
...................
.........................
..........................
while(!(start()))
{
while(!(nostart()))
{
linst();
}
}
.........
return 0;
}
寫法-->2
#include<stdlib.h>
#include<stdio.h>
int start(int x,char *b);
int nostart(int x,char *b);
void linst();
int main()
{
...................
.........................
..........................
while(!(start()));
{
while(!(nostart()));
{
linst();
}
}
.........
return 0;
}
若使用寫法1跟寫法2在做compile的時候~~都不會有任何warning和error~~但是寫法1將它Porting至CPU內~~它執行會有錯誤無法達到我想要的結果....
不過寫法2若將它Porting至CPU內~~它執行就會完全正確可以達到我想要的結果....
這2個寫法只不過差了while();~~一個冒號~~但是結果大不同!!
小弟不才~~無法理解原因出在哪~~所以來此向高手請教與指導
~~希望能有詳細的答案~~謝謝!!
給元氣果:
你的答案絕對是錯誤的~~我寫firmware有用示波器下去量測~~
希望有真的高手能出現~~感謝!!
忘記提到二點:
(1)開發環境Embedded Linux
(2)Kernel是arm-elf-gcc的編輯器
謝謝!!
2 Answers
- ?Lv 41 decade agoFavorite Answer
Novus大大寫得很清楚:寫法二等於
while(!(start()))
{}
while(!(nostart()))
{}
linst();
而寫法一等於
while(!(start()))
{ while(!(nostart()))
linst();
}
;是 C 語言的句點。
while 只執行它後面的一個區塊。
寫法二在 while 後都加了;
表示那一個區塊就到此結束了!
寫法一沒有;
它後面的 {} 就是它要跑的那一個區塊。
所以,寫法二和寫法一會完全不同。
Source(s): Novus 大大