Can we at the same time check the loop of two arguments without && / ||

I tested the Beej manual on IPC , and one line of code caught my attention.

On a specific page, the while loop speak.chas two conditions for checking while (gets(s), !feof(stdin)).

So my question is how is this possible, as I saw during checking only one state most of the time.

PS: I'm a little new to this. We will be grateful for any help. Thank!

+3
source share
5 answers

Excerpt

while (gets(s), !feof(stdin))

uses a comma operator, first it executes gets(s), then it tests !feof(stdin)what is the result of the condition.

, gets, . , , , , .

while(gets(s), !feof(stdin)) {
    /* loop body */
}

gets(s);
while(!feof(stdin)) {
    /* loop body */
    gets(s);
}

, gets .

+6

. , , , gets () - .

, : feof(file) , / , . , , ( ) EOF - (- ), , .

- fgets :

while (fgets(s, length_of_s, stdin))
    process(s);

fgets , .

: fgets , ( gets ). , , , , (, , , , ).

+3

gets(s) end-of-file !feof(stdin).

+2

. gets(s), !feof(stdin), gets().

, gets(), , (, ).

+2

@DanielFischer @trojanfoe, .

, , , , . , , .

, - , LINT (http://en.wikipedia.org/wiki/PC-Lint), , .

, @DanielFischer, gets() . fgets(), .

http://www.cplusplus.com/reference/clibrary/cstdio/fgets/

You will not regret the habit of always using fgets () instead of get, especially if it is production code.

You may also consider looking for string functions.

http://linux.die.net/man/3/snprintf

They are not part of the standard ANSI / C99 C, but if you are just developing for a specific platform, they do not allow you to crash your program or open security holes.

Good luck

+1
source

All Articles