Convert it into a C string and you're done
string s;
int i = atoi(s.c_str());
Answer from Matteo Pacini on Stack Overflowcppreference.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())}; ...
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
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
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
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
09:36
LeetCode String to Integer (atoi) | Java - YouTube
16:59
String to Integer atoi 🔥| Leetcode 8 | String - YouTube
07:47
String To Integer (atoi) - 8. LeetCode - Java - YouTube
07:35
Implement Atoi || String 5|| Placement Preparation Series || ...
15:04
STRING TO INTEGER (ATOI) | PYTHON | LEETCODE # 8 - YouTube
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 ...
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.
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 ...
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 ...
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.
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.
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); }