I am a Python programmer to learn C from the book K & R. This will seem like a terribly trivial question, but nonetheless I'm at a dead end. The following is a snippet of code from the book K & R (RIP Ritchie!), Which implements the atoi () function.
atoi(s) char s[]; { int i, n, sign; for (i=0; s[i]==' '||s[i] == '\n' || s[i] == '\t'; i++) ; sign = 1; if (s[i] == '+' || s[i] = '-') sign = (s[i++] == '+') ? 1 : -1; for (n=0; s[i] >= '0' && s[i] <= '9'; i++) n = 10 * n + s[i] - '0'; return (sign * n); }
My questions:
1) Does the first 'for' loop use any target other than counting the number of valid characters?
2) If (1) is true, the first cycle sets the value of "i" to the number of valid characters - how does the second cycle work without reselling I to 0?
Say, for example, I enter "2992" as an input to a function. I set the first for the loop to 3, so how does the rest of the function work? I can have all my problems, but any help would be really appreciated. Thanks, -Craig
Craig source share