工程師應知道的0x10個問題(4): 無窮迴圈

MuLong PuYang
1 min readFeb 19, 2022

--

英文原參考網址

A ‘C’ Test: The 0x10 Best Questions for Would-be Embedded Programmers

中文參考網址

C語言測試 應知道的0x10個基本問題

原文翻譯

4. Infinite loops often arise in embedded systems. How does one code an infinite loop in C?

無窮迴圈常出現在嵌入式系統。如何用C寫一行無窮迴圈的程式

There are several solutions to this question. My preferred solution is:

while(1)

{

}

有多種方法來解答這提問題,我比較喜歡的解法是:

while(1)

{

}

Another common construct is:

for(;;)

{

}

另一個常見的結構是:

for(;;)

{

}

Personally, I dislike this construct because the syntax doesn’t exactly spell out what is going on. Thus, if a candidate gives this as a solution, I’ll use it as an opportunity to explore their rationale for doing so. If their answer is basically — ‘I was taught to do it this way and I have never thought about it since’ — then it tells me something (bad) about them. Conversely, if they state that it’s the K&R preferred method and the only way to get an infinite loop passed Lint, then they score bonus points.

本身來講,我不喜歡這種結構因為這個語法沒有精確的拼出發生什麼事。因此,如果一個被面試的人給出這個解法,我會使用這個當成一個機會來探索用這個合理的原因。如果他們的答案基本上是 — ‘我被教使用這種方法,而且之前我從來沒有想過關於它’ — 然後告訴我一些關於它們事情(不好的)。相反的,如果他們描述說這是K&R喜歡用的方式,並且是為一個方法來達成無窮迴圈且通過Lint,那他們會獲得bonus points。

A third solution is to use a goto:

Loop:

goto Loop;

第三個方法使用goto:

Loop:

goto Loop;

Candidates that propose this are either assembly language programmers (which is probably good), or else they are closet BASIC / FORTRAN programmers looking to get into a new field.

被面試者提出這個,這代表他或許是一個組合語言的程式設計師(這也許是好事),又或者他們是想進入新領域的BASIC/FORTRAN程式設計師

--

--