1
for (i = 0; isspace(s[i]); i++) { ... }

The above for loop is the part of the program for converting a string to an integer in K&R on page 61.

There's no condition check in that for loop. How does it work?

Bob__
  • 12,361
  • 3
  • 28
  • 42

4 Answers4

2

The loop terminates whenever the condition expression evaluates to 0. If for instance you see an expression like i < 3, you can think of it as (i < 3) != 0. So in this case, it's isspace(s[i]) != 0 meaning it terminates at the first character that is not a space.

Kurt Stutsman
  • 3,994
  • 17
  • 23
  • Surely it terminates when the condition evaluates to false, not 0? – DoctorMick Mar 21 '16 at 16:42
  • 4
    @DoctorMick Sort of semantics there. C didn't even used to have a boolean type. Back then _true_ was any value that could decay to an integer value that was not equal to 0. Since `false == 0`, it doesn't really matter. – Kurt Stutsman Mar 21 '16 at 16:46
  • @KurtStutsman Sorry I didnt get your answer . may i get a simple example ? `for (i=3; 7; i--) printf("hello world \n"); getch(); return 0;` –  Mar 21 '16 at 16:46
  • 1
    @DoctorMick: This is C. C does not have `false`. C conditions evaluate to integer `0` or `1`. C99 introduced `false` as a macro defined in `` and that macro still stands for `0`. – AnT stands with Russia Mar 21 '16 at 16:48
  • @AnT Yes and no. See [this answer](http://stackoverflow.com/a/1608350/459615). _stdbool.h_ does define `false`. – Kurt Stutsman Mar 21 '16 at 16:51
  • 1
    To expand on @AnT C: conditions (e.g. `i > 9`) evaluate to `0` or `1` but _decisions_ (in `if`, `while` etc.) are based on whether the expression evaluates to `0` or "not `0`". – TripeHound Mar 21 '16 at 17:07
  • 1
    `for (i=3; 7; i--) ` this loop will never terminate, since 7 is always not zero. – FredK Mar 21 '16 at 17:09
  • @Kurt Stutsman: Um... The answer you linked to is *my* answer. And it states exactly what I said here. – AnT stands with Russia Mar 21 '16 at 17:19
  • @AnT lol didn't see it was your answer. But you said there is no `false` in C on here, and that's not true by your own answer. You could say there is no `false` keyword I suppose. By that measure you could say `NULL` is not part of C either. – Kurt Stutsman Mar 21 '16 at 17:21
  • 1
    @Kurt Stutsman: What I meant is that the *core language* does not include dedicated boolean-specific constants. Even tough core language included a dedicated boolean type `_Bool`, this type still relies on `0` and `1` constants to represent "false" and "true" values. Everything else are library-level macro definitions. – AnT stands with Russia Mar 21 '16 at 17:26
1

isspace(s[i]) is the condition, since it returns a zero value for 'false' (i.e. the provided character is not a space character), and non-zero values for 'true'. In this case, only one space character exists, but in other functions such as isalpha or isalphanum, non-zero values mean different things like 1 means it is an upper-case letter (so it is an alphabetical character), or 2 means it is a lower-case letter) and so on (I may have mixed those numbers up :/).

In otherwords, the for loop is treating this function as returning a boolean value, like most other expressions, meaning it treats zero-values as false, and non-zero as true.

SolaGratia
  • 309
  • 4
  • 18
0

First of all, you're going to get out of bounds, which is gonna give you a segfault. You're just increasing i, but not checking whether it's still in the bounds or not. For example, what if s would be equal to " "? isspace(i[0]) will return a non-zero value (treated as true) and the loop will continue and will try to access the second (non-existent!) value of s. You'd better add another tiny check: for (i = 0; (i<SIZE_OF_S) && isspace(s[i]); i++) { ... }

Let's now talk about the missing condition. No, it's not missing, it's here: isspace(s[i]). This function checks whether s[i] is considered space or not. It returns a non-zero value if yes, and 0 otherwise (docs). So, it is the missing condition, just in a slightly different form (maybe you're used to different comparisons, but there exist a lot more ways.

ForceBru
  • 43,482
  • 10
  • 63
  • 98
0

Yes If the for loop have without condition checking , the for loop will exit whenever the condition part will be zero

For example

` int i ;

for (i=0; 7-i; i++)
printf("hello world \n");
getch();
return 0;

}`

See the above program the 'i' minus the seven each time . and the loop will exit whenever the value of condition part will be zero (ie 7-7) .enter image description here