First, I need to point out that the gets
function is so dangerous that it's been removed from the language. You should never use it.
In your case, since your line
array is 100 elements long, it won't actually cause any problems as long as each input line is short enough to fit. You can use gets
"safely", but only if you have complete control over what input your program will receive. In practical terms, you hardly ever have such control. If you enter 120 characters on a line, your program will clobber memory outside the array, with arbitrarily bad results.
The fgets()
function is safer, since it lets you specify a maximum length. It's also a bit harder to use; for one thing, it leaves the newline '\n'
in the input array.
Now on to the question you actually asked.
char line[100];
while (gets(line), strlen(line)){//some stuff}
This uses the comma operator. The comma operator evaluates both of its operands in order (in this case, gets(line)
and strlen(line)
and then yields the result of its right operand.
gets(line)
returns its argument, a pointer to the first character of line
, or a null pointer if it failed. That result is discarded. (It still reads input data into line
.)
The right operand of the comma operator is the length of the line you just read.
A condition in a while
or if
statement can be of any scalar type (integer, floating-point, or pointer). The condition is false if the value is equal to 0
, true if it's anything else.
So your while
loop will continue to execute as long as the length of the line you just read is non-zero. In other words, the loop will repeatedly read lines of input, stopping when it sees an empty line.
It's important to understand that not all commas are comma operators. For example, if a function takes two or more arguments, those arguments are separated by commas; that's part of the syntax of a function call, not the comma operator.
(Did I mention that you shouldn't use gets
?)