Convert it into a C string and you're done

string s;

int i = atoi(s.c_str());
Answer from Matteo Pacini on Stack Overflow
🌐
cppreference.com
en.cppreference.com › cpp › string › byte › atoi
std::atoi, std::atol, std::atoll - cppreference.com
#include <array> #include <cstdlib> #include <iostream> #include <locale> #include <numeric> int main() { std::cout.imbue(std::locale("en_US.UTF-8")); std::array<char, 8> arr; std::iota(arr.begin(), arr.begin() + arr.size() - 1, '1'); arr.back() = '\0'; const int num{std::atoi(arr.data())}; ...
Discussions

How do you use 'atoi'? (C)
atoi will return the value of the integer stored in the string (assuming it contains an integer, if it doesn't you'll get a 0). So using the same basic code that you showed before: char* s = "123"; int x = atoi(s); Is how it is used. The examples on the atoi page show the result going straight to printf as a parameter to be printed, but it can be stored as well. If you use sscanf or sscanf_s you'd pass in the address of the variable x along with s and the format string: sscanf(s, "%d", &x); All the scanning functions return a number which is the number of matches and assignments (so if your s were "abcd" the return would be 0, but if it's a valid integer then 1) or EOF for some error conditions. More on reddit.com
🌐 r/learnprogramming
2
0
June 4, 2022
Atoi.
The atoi function is for converting a string (that is, char*) to an int. A char is not a string. The good news is that char is already a numeric type. You can just write int num = argv[1][k]; print("success: %d\n", num); Even if you do need to convert a string to an int, you shouldn't use atoi, because it lacks error handling. You can pass in "flurbl" and it will still return an int value. Instead you should use strtol or sscanf. In the former case you need to set errno=0 first, in order to distinguish overflow from a correct conversion of INT_MAX or INT_MIN. More on reddit.com
🌐 r/C_Programming
8
0
April 17, 2019
After 3 hours of pain and misery, my solution to “String to Integer” on LeetCode
Your approach to taking screenshots is deeply disturbing. More on reddit.com
🌐 r/programminghorror
169
2245
January 4, 2024
strconv.Atoi is causing my test to fail and I can't figure out why
Your errors.New(whatever) is distinct/not equal to any other errors.New(whatever) as documented here : New returns an error that formats as the given text. Each call to New returns a distinct error value even if the text is identical. More on reddit.com
🌐 r/golang
9
1
April 1, 2023
🌐
TutorialsPoint
tutorialspoint.com › c_standard_library › c_function_atoi.htm
C library - atoi() function
#include <stdlib.h> #include <stdio.h> int main(void) { int val; char *str; str = "1509.10E"; val = atoi(str); printf("integral number = %d", val); } ... The below c example concatenate two string and then convert the resulting string into a ...
🌐
GeeksforGeeks
geeksforgeeks.org › c language › c-atoi-function
atoi() Function in C - GeeksforGeeks
October 13, 2025 - The atoi() function accepts a string (which represents an integer) as a parameter and yields an integer value in return.
🌐
W3Schools
w3schools.com › c › ref_stdlib_atoi.php
C stdlib atoi() Function
The atoi() function is defined in the <stdlib.h> header file. Tip: If the number is not at the beginning of the string you can use a pointer to a different position in the string. See More Examples below for an example.
🌐
LeetCode
leetcode.com › problems › string-to-integer-atoi
String to Integer (atoi) - LeetCode
Note:​​​​​​​ You ... int(), parseInt, Number()). Perform the conversion manually. Example 1: Input: s = "42" Output: 42 · Explanation: The underlined characters are what is read in and the caret is the ...
🌐
cppreference.com
en.cppreference.com › c › string › byte › atoi
atoi, atol, atoll - cppreference.com
The name stands for “ASCII to integer”. Run this code · #include <stdio.h> #include <stdlib.h> int main(void) { printf("%i\n", atoi(" -123junk")); printf("%i\n", atoi(" +321dust")); printf("%i\n", atoi("0")); printf("%i\n", atoi("0042")); // treated as a decimal number with leading zeros ...
Find elsewhere
🌐
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
🌐
Cplusplus
cplusplus.com › reference › cstdlib › atoi
Atoi - Convert string to integer
int atoi (const char * str); Convert string to integer · Parses the C-string str interpreting its content as an integral number, which is returned as a value of type int. The function first discards as many whitespace characters (as in isspace) as necessary until the first non-whitespace character ...
🌐
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
🌐
ZetCode
zetcode.com › clang › atoi
C atoi Tutorial: String to Integer Conversion with Practical Examples
It's declared in stdlib.h and takes one parameter: the string to convert. atoi skips whitespace, then converts subsequent characters until it encounters a non-digit. It has no error detection - invalid input returns 0. For robust code, prefer strtol which provides error checking and supports different number bases. This example demonstrates converting a simple numeric string using atoi.
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-runtime-library › reference › atoi-atoi-l-wtoi-wtoi-l
atoi, _atoi_l, _wtoi, _wtoi_l | Microsoft Learn
November 15, 2023 - Convert a string to integer. int atoi( const char *str ); int _wtoi( const wchar_t *str ); int _atoi_l( const char *str, _locale_t locale ); int _wtoi_l( const wchar_t *str, _locale_t locale );
🌐
Cppreference
cppreference.net › c › string › byte › atoi.html
atoi, atol, atoll - cppreference.net
The name stands for “ASCII to integer”. Run this code · #include <stdio.h> #include <stdlib.h> int main(void) { printf("%i\n", atoi(" -123junk")); printf("%i\n", atoi(" +321dust")); printf("%i\n", atoi("0")); printf("%i\n", atoi("0042")); // treated as a decimal number with leading zeros ...
🌐
SAS
support.sas.com › documentation › onlinedoc › sasc › doc700 › html › lr1 › z2054672.htm
Function Descriptions : atoi
Note that the input may still not represent */ /* a valid integer. Consider +-14+5. **/ if (strspn(input, " +-0123456789") == strlen(input)){ value = atoi(input); printf(" The integer equivalent of given string is %d\n", value); } else printf("Invalid count value: %s\n", input); }
🌐
Scaler
scaler.com › home › topics › atoi() function in c
atoi() function in C - Scaler Topics
October 14, 2022 - The atoi function in C takes a string (which represents an integer) as an argument & returns its value of type int. So basically the function is used to convert a string argument to an integer.
🌐
O'Reilly
oreilly.com › library › view › c-in-a › 0596006977 › re15.html
atoi - C in a Nutshell [Book]
December 16, 2005 - NameatoiSynopsisConverts a string to an integer#include <stdlib.h> intatoi( const char *s ); long atol( const char *s ); long long atoll( const char *s ); (C99)The atoi()... - Selection from C in a Nutshell [Book]
Authors: Peter PrinzTony Crawford
Published: 2005
Pages: 618
🌐
Reddit
reddit.com › r/learnprogramming › how do you use 'atoi'? (c)
r/learnprogramming on Reddit: How do you use 'atoi'? (C)
June 4, 2022 -

I want to assign an integer variable as a string. So if char *s = "123";, and int x;, I want to assign x to be whatever s is, so in this case "123". I asked a few hours ago and a lot of people said to use atoi. I tried researching it and from what I gathered it's a function that's in the header file stdlib.h, but no one really explained how to use it or what it does specifically. Also in every website I went to, they all said to use the syntax int atoi(const char *string), but if the function has already been declared in the header file, I don't see why there is a need to put a prototype. Can anyone explain this in a little more detail?

Thank you.

🌐
TutorialKart
tutorialkart.com › c-programming › c-stdlib-atoi
atoi() Function - C stdlib.h - Syntax, Parameters, Examples
February 21, 2025 - This example demonstrates how atoi() converts a simple numeric string to an integer.
🌐
W3Resource
w3resource.com › c-programming › stdlib › c-atoi.php
C atoi() function
#include <stdlib.h> #include <stdio.h> int main(void) { int in_val; char *s; s = " -4938"; printf("String value: %s",s); in_val= atoi(s); printf("\nInteger value = %d\n",in_val); s = "+4938"; printf("\nString value: %s",s); in_val= atoi(s); printf("\nInteger value = %d\n",in_val); s = "4938"; printf("\nString value: %s",s); in_val= atoi(s); printf("\nInteger value = %d\n",in_val); s = "0038"; printf("\nString value: %s",s); in_val= atoi(s); printf("\nInteger value = %d\n",in_val); s = "003800"; printf("\nString value: %s",s); in_val= atoi(s); printf("\nInteger value = %d\n",in_val); }
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › cpp-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.