My second while condition is being ignored and I don't understand why. I would think I have 1 and 0 = 0 so it would break out.
while(fgets(line, sizeof line, stdin) && sscanf_counter != 2)
{
}My second while condition is being ignored and I don't understand why. I would think I have 1 and 0 = 0 so it would break out.
while(fgets(line, sizeof line, stdin) && sscanf_counter != 2)
{
}Short answer: the expression uses C's left-to-right, short‑circuit evaluation. If the read fails (fgets returns NULL), the right‑hand test is never evaluated — that looks like the "second condition is ignored" but is actually how && works. Making the intent explicit with comparisons and clearer control flow avoids surprises.
A robust pattern is to do the read first, then check the sscanf result. This separates I/O from logic and makes debugging straightforward:
char line[256];
while (fgets(line, sizeof line, stdin) != NULL) {
int matches = sscanf(line, "%d %d", &a, &b); /* example */
if (matches == 2)
break; /* stop when two conversions succeed */
/* handle other cases or continue reading */
} Common causes for the behavior described here:
sscanf_counter =! 2 (wrong) instead of sscanf_counter != 2 will assign 0 and mask logic.sscanf_counter because sscanf return value is ignored or assigned to a different variable.This advice expands on 's note about parentheses and 's suggestion to separate reading from checking; ’s idea of breaking out after checking inside the loop is the same pattern shown above. Simple logging of the read result and the match count each iteration quickly reveals which of the above is happening.
Jump to Post— rproffitt 3,266Look carefully at your use of left and right parentheses on line 1.
Did you mean to compare gets(line, sizeof line, stdin) && sscanf_counter to not be equal to 2?
Or did you mean gets(line, sizeof line, stdin) then and the next condition.Be clear about that. Don't rely on …
Jump to Post— rubberman 1,355while(fgets(line, sizeof line, stdin) { if (sscanf_counter != 2) #breakout here# }
Look carefully at your use of left and right parentheses on line 1.
Did you mean to compare gets(line, sizeof line, stdin) && sscanf_counter to not be equal to 2?
Or did you mean gets(line, sizeof line, stdin) then and the next condition.
Be clear about that. Don't rely on "a C compiler works like this."
And I will add the following : in the begining, the first version of your code, it is recommanded to not mix actions; separate reading from checking. Read what you need to read then check the result in the while condition even if that means you need to put the same line twice (before and inside the loop block). The code will be easier to read and you can log (print) the content to check if it's correct.
I want the loop to continue until sscanf_count equals 2 or the user hits ctrl + D.
@C. Then state so in your while condition. Never rely on the compiler to read our minds. Spell it out with a few more parentheses.
I know folk that want to debate the too many parantheses but they are here to debate, not solve it.
while(fgets(line, sizeof line, stdin)
{
if (sscanf_counter != 2)
#breakout here#
}We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.