Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

atoi implementation in C

Tags:

c

atoi

atof

I can't understand the following atoi implementation code, specifically this line:

k = (k << 3) + (k << 1) + (*p) - '0'; 

Here is the code:

int my_atoi(char *p) {     int k = 0;     while (*p) {         k = (k << 3) + (k << 1) + (*p) - '0';         p++;      }      return k; } 

Can someone explain it to me ?

Another question: what should be the algorithm of atof implementation ?

like image 281
Adam Avatar asked Oct 08 '12 23:10

Adam


People also ask

What is atoi in C example?

C library function - atoi() The C library function int atoi(const char *str) converts the string argument str to an integer (type int).

How does atoi function work in C?

The atoi() function converts a character string to an integer value. The input string is a sequence of characters that can be interpreted as a numeric value of the specified return type. The function stops reading the input string at the first character that it cannot recognize as part of a number.

What does atoi return in C?

Returns. The atoi function returns the integer representation of a string. The atoi function skips all white-space characters at the beginning of the string, converts the subsequent characters as part of the number, and then stops when it encounters the first character that isn't a number.


2 Answers

<< is bit shift, (k<<3)+(k<<1) is k*10, written by someone who thought he was more clever than a compiler (well, he was wrong...)

(*p) - '0' is subtracting the value of character 0 from the character pointed by p, effectively converting the character to a number.

I hope you can figure out the rest... just remember how the decimal system works.

Here is a specification for the standard function atoi. Sorry for not quoting the standard, but this will work just as fine (from: http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/ )

The function first discards as many whitespace characters (as in isspace) as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many base-10 digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed and zero is returned.

like image 87
Karoly Horvath Avatar answered Oct 13 '22 19:10

Karoly Horvath


k = (k << 3) + (k << 1); 

means

k = k * 2³ + k * 2¹ = k * 8 + k * 2 = k * 10 

Does that help?

The *p - '0' term adds the value of the next digit; this works because C requires that the digit characters have consecutive values, so that '1' == '0' + 1, '2' == '0' + 2, etc.

As for your second question (atof), that should be its own question, and it's the subject for a thesis, not something simple to answer...

like image 38
R.. GitHub STOP HELPING ICE Avatar answered Oct 13 '22 17:10

R.. GitHub STOP HELPING ICE