Bill said:
Shao Miller wrote:
Here's where I get lost. I've seen people put all kinds of things in
those while parenthesis. I see ustr ?dereferenced? This code will need a lot
of study from me. I don't see it sorry no.
Ah yes. Well it might not help that there's a typo in the comment
above. It should have read "While 'ustr' points to..."
The 'while' loop has a condition which must be true for the 'while' loop
to execute/run. The condition is '*str && rc > 0', as you can see
in-between the parentheses.
The logical AND operator ('&&') actually connects two sub-conditions.
The first sub-condition is '*ustr' which, as you noted, is a
dereference. Since 'ustr' is an 'unsigned char *', then '*ustr' is an
'unsigned char'. So the first sub-condition is whether or not the
'unsigned char' value currently pointed-to by 'ustr' is non-zero.
The second sub-condition is 'rc > 0'. You can see that 'rc' was
declared above and initialized to the value 1.
SO: The 'while' loop will execute if and only if 'ustr' currently points
to a non-zero 'unsigned char' value AND 'rc' has a value greater than 0.
In each iteration of the loop, we change 'rc' to contain the value
yielded by 'printf'. If 'printf' encounters an error (given the
provided format string), it should yield a value <= 0. We store this in
'rc' and it will be checked the next time we check the loop's condition.
To tell the whole truth, it must be mentioned that the second
sub-condition 'rc > 0' is NOT checked if the first sub-condition fails.
That's just the nature of the '&&' operator.
As three others have noted, the use of "%c" in the 'printf' format
string is not really the best if you are actually trying to produce code
for a real situation. If that's your situation, please don't take this
simple, silly (but possibly useful) example code as anything more than
some reference code for discussion, should you care to.