Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int atoi(const char *str)
- {
- int n = 0; // nombre parsé
- char c = '0'; // caractère courant
- int i = 0; // itérateur
- int signe = 1;
- if(str[0] == '-')
- {
- signe = -1;
- ++i; // ++i est plus rapide que i++
- }
- while(c >= '0' && c <= '9')
- {
- n *= 10;
- n += c - '0';
- c = str[i++];
- }
- return n*signe;
- }
- int main(void)
- {
- const char* str = "-24987";
- int n = atoi(str);
- printf("%s = %d\n", str, n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement