<< 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.

Answer from Karoly Horvath on Stack Overflow
Top answer
1 of 6
34

<< 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.

2 of 6
32
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...

๐ŸŒ
LeetCode
leetcode.com โ€บ problems โ€บ string-to-integer-atoi
String to Integer (atoi) - LeetCode
Can you solve this real interview question? String to Integer (atoi) - Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer. The algorithm for myAtoi(string s) is as follows: 1. Whitespace: Ignore any leading ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ dsa โ€บ write-your-own-atoi
String to Integer - Write your own atoi() - GeeksforGeeks
Write your own atoi() that takes a string (which represents a floating point value) as an argument and returns its value as double.
Published: April 14, 2026
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ c-atoi-function
atoi() Function in C - GeeksforGeeks
October 13, 2025 - One of the possible method to implement is shown below: Initialize res to 0. Iterate through each character of the string and update the res by multiplying it by 10 and keep adding the numeric value of the current digit.
๐ŸŒ
Aticleworld
aticleworld.com โ€บ home โ€บ how to use atoi() and how to make own atoi() ?
How to use atoi() and how to make own atoi() ? - Aticleworld
October 17, 2022 - The atoi() function converts the numeric string to the int representation. It takes a string as an argument and returns its integer value.
๐ŸŒ
GitHub
gist.github.com โ€บ alfhh โ€บ 20d627db7867c6890d88
Implementation of atoi function in C. ยท GitHub
Implementation of atoi function in C. GitHub Gist: instantly share code, notes, and snippets.
Find elsewhere
๐ŸŒ
Techie Delight
techiedelight.com โ€บ home โ€บ basic โ€บ implement atoi() function in c | iterative & recursive
Implement atoi() function in C | Iterative & Recursive | Techie Delight
July 7, 2026 - Write an efficient function to implement the atoi function in C. The standard `atoi()` function converts input C-string to its corresponding integer value.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ dsa โ€บ c-program-to-write-your-own-atoi
C Program To Write Your Own atoi() - GeeksforGeeks
July 23, 2025 - The atoi() function in C takes a string (which represents an integer) as an argument and returns its value of type int.
๐ŸŒ
cppreference.com
en.cppreference.com โ€บ cpp โ€บ string โ€บ byte โ€บ atoi
std::atoi, std::atol, std::atoll - cppreference.com
Actual C++ library implementations fall back to C library implementations of atoi, atoil, and atoll, which either implement it directly (as in MUSL libc) or delegate to strtol/strtoll (as in GNU libc).
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ problems โ€บ implement-atoi โ€บ 1
Implement Atoi | Practice | GeeksforGeeks
Given a string s, convert it into a 32-bit signed integer (similar to the atoi() function) without using any built-in conversion functions.The conversion follows these rules:Ignore Leading Whitespaces: Skip all leading whitespace characters
๐ŸŒ
w3resource
w3resource.com โ€บ c-programming-exercises โ€บ c-snippets โ€บ implementing-custom-atoi-function-in-c.php
C - Implementing a custom atoi() function
November 1, 2025 - # include <stdio.h> int my_atoi(const char *str) { int result = 0; int sign = 1; int i = 0; // Check for leading sign character if (str[0] == '-') { sign = -1; i++; } else if (str[0] == '+') { i++; } // Convert digits to integer value while (str[i] != '\0') { if (str[i] < '0' || str[i] > '9') { break; } result = result * 10 + (str[i] - '0'); i++; } return sign * result; } int main() { const char *str1 = "-1234"; int result = my_atoi(str1); printf("Converted integer: %d\n", result); const char *str2 = "1234"; result = my_atoi(str2); printf("\nConverted integer: %d\n", result); return 0; }
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ c_standard_library โ€บ c_function_atoi.htm
C library - atoi() function
The C stdlib library atoi() function is used to convert a numeric string into an integer value. An integer is a whole number that can be positive or negative, including zero. It can't be fraction, decimal, or percentage, For example, numbers like 1,
๐ŸŒ
Simplilearn
simplilearn.com โ€บ home โ€บ resources โ€บ software development โ€บ atoi in c++: an ultimate guide to atoi() function
Atoi in C++: An Ultimate Guide to atoi() Function
October 17, 2024 - Atoi in C++ accepts a string parameter that contains integer values and converts the passed string to an integer value. Learn all about the atoi() function!
Address: 5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
๐ŸŒ
YouTube
youtube.com โ€บ tech dose
Implement atoi | Convert string to integer - YouTube
This video lecture explains the idea behind how to implement the atoi function which is very frequently asked in interviews and programming rounds in differe...
Published: November 1, 2019
Views: 7K
๐ŸŒ
John Kavanagh
johnkavanagh.co.uk โ€บ home โ€บ articles โ€บ string to integer (atoi): decoding strings in javascript
String to Int. (atoi): Decoding Strings in Web Dev | John Kavanagh
April 28, 2023 - This is often abbreviated as 'atoi': 'aSCII to integer', which has its roots in the C programming language, where there are standard library functions named atoi, atol, and atoll that convert ASCII strings to integers of various sizes (int, long, ...
๐ŸŒ
DHANRAJ NIKAM's blog
djnikam.hashnode.dev โ€บ dhanraj nikam's blog โ€บ #gfg โ€บ implement atoi
Java Atoi Implementation - DHANRAJ NIKAM's blog
March 21, 2023 - class Solution { int atoi(String str1) { int res = 0; char str[] = str1.toCharArray(); int sign = 1; int i = 0; if (str[0] == '-') { sign = -1; i++; } for (; i < str.length; ++i) { if(0<=str[i]-'0' && str[i]-'0'<=9 ) res = res * 10 + str[i] - '0'; else { return -1; } } return sign * res; } }
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ ref_stdlib_atoi.php
C stdlib atoi() Function
The atoi() function is defined in the <stdlib.h> header file.