#include #include /** * atoi: convert s to integer * Based closely on version 2 from K&R 2nd. */ int atoi (char s[]) { int i, n, sign; // Skip over whitespace for (i = 0; isspace (s[i]); i++) ; // The body of this is empty /* Could also have been i = 0; while (isspace (s[i])) i++; */ // Determine the sign sign = (s[i] == '-') ? -1 : 1; /* Shorthand for if (s[i] == '-') sign = -1; else sign = 1; */ // Skip over the sign if (s[i] == '+' || s[i] == '-') i++; // Do the real work for (n = 0; isdigit(s[i]); i++) n = 10 * n + (s[i] - '0'); /* Look at each digit in the string. s[i] is a character i++ is stepping through those characters isdigit checks whether it's a digit How is n behaving? */ // And we're done return sign * n; } // atoi int main (int argc, char *argv[]) { int i = atoi (argv[1]); printf ("%d squared is %d.\n", i, i*i); return 0; } // main