////////////////////////////////////////////////////////////////////////////////// // File : stdlib.c // Date : 05/12/2013 // Author : Clément DEVIGNE // Copyright (c) UPMC-LIP6 /////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////// // int atoi ( char * str ) /////////////////////////////////////////////////////////////////////////////////// int atoi(char *str) { int res = 0; // Initialize result int sign = 1; // Initialize sign as positive int i = 0; // Initialize index of first digit if (str[0] == '-') //If number is negative, then update sign { sign = -1; i++; // Also update index of first digit } for (; str[i] != '\0'; ++i) // Iterate through all digits and update the result { res = res*10 + str[i] - '0'; } // Return result with sign return sign*res; } // Local Variables: // tab-width: 4 // c-basic-offset: 4 // c-file-offsets:((innamespace . 0)(inline-open . 0)) // indent-tabs-mode: nil // End: // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4