I always see while(1); at the end of the C codes such as:
#include <stdio.h>
int main(void)
{
//Some code here...
while(1);
}
If I'm not wrong that it always exists, what is the function of that?
edit:
#include <stdio.h>
int main(void)
{
while(1);
//Some code here...
}
3 Answers 3
It's because in non-embedded systems, when main()
finishes it returns to the operating system and the program is removed from memory. In embedded systems there's no operating system to return to, and the program can't be removed from memory. So... what do you do when the program is finished? Simple: you do nothing. Forever. And that's what while(1);
does.
Some compilers and C libraries automatically add while(1);
in the exit()
routine, but not all do - so it's better to do it explicitly than rely on something that may or may not be there.
-
Its very confusing syntax-wise as well. The "while" is used with curly brackets. But here at the end there is while(1);. How can we articulate this to understand how it functions on the code?GNZ– GNZ2019年11月21日 14:48:42 +00:00Commented Nov 21, 2019 at 14:48
-
1
while
, likeif
, can be used with curly brackets. It doesn't have to be.while(something) dosomething();
andwhile(something) { dosomething(); }
are the same. You only need the brackets if you want to do more than one thing.Majenko– Majenko2019年11月21日 14:56:47 +00:00Commented Nov 21, 2019 at 14:56 -
Does that mean(for forever loop) while(1) can be on the top of the code instead of at the end?GNZ– GNZ2019年11月21日 14:59:08 +00:00Commented Nov 21, 2019 at 14:59
-
You can use it anywhere you like. You also sometimes see
for (;;);
which means the same thing... "for ever..."Majenko– Majenko2019年11月21日 15:03:08 +00:00Commented Nov 21, 2019 at 15:03 -
To be more specific. Please see my edit. I added another code to my question. Are these two codes will act the same way?GNZ– GNZ2019年11月21日 15:21:26 +00:00Commented Nov 21, 2019 at 15:21
while(1);
should actually be read as
while(true)
{
// No statements
}
// Statements following here will never be executed
The result is: while the condition is true, it performs no statements. Since the condition true
is always true, it keeps performing the statements within the { and }, which are no statements, resulting that no statement is executed but it will also not executing statements after the while.
-
1The "while" is normally used with curly brackets. But here at the end there is while(1);. How can we articulate this to understand how it functions on the code?GNZ– GNZ2019年11月21日 14:54:28 +00:00Commented Nov 21, 2019 at 14:54
-
Majenko already answered this, in about similar words: while (true) <X> where x can be a single statement like while (true) a = 4; (thus a = 4; is the statement). Or it can be a compound (multiple statements) in the form of while (true) { a = 4; b = 5; }. In the case of your question a single (but empty) statement is used: while (true) /* empty statement */ ;Michel Keijzers– Michel Keijzers2019年11月21日 15:01:31 +00:00Commented Nov 21, 2019 at 15:01
-
Can while(1) be on the top of the code instead of at the end? Would it work same way? I dont get why they put it at very end instead of at very beginning.GNZ– GNZ2019年11月21日 15:03:23 +00:00Commented Nov 21, 2019 at 15:03
-
It can be anywhere in the code, however, note that code after such while statement, will not be performed anymore as the MCU keeps executing ... nothing.Michel Keijzers– Michel Keijzers2019年11月21日 15:06:57 +00:00Commented Nov 21, 2019 at 15:06
-
1No, they are not the same. The first executes <some code here>, and then waits indefinitely. The second waits indefinitely, and then executes <some code here> (which is never).Gerben– Gerben2019年11月21日 15:29:23 +00:00Commented Nov 21, 2019 at 15:29
The line while(1) in a C program creates an infinite loop- this is a loop that never stops executing. It executes over and over and over again, unless the program is intentionally stopped or there is some condition under this loop that gets met that takes the program out of this infinite loop.
Let's take the following C program.
include
void main() { printf("Hello World"); while(1); }
This program prints the words "Hello World" to the standard output, which is most likely a serial port.
After it prints it, it goes to execute the next line, while(1). This line makes the microcontroller sit and wait, forever or until the microcontroller is reset.
;
is empty command. it is the nothing repeated withwhile (1)