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
🌐
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 integral number using the atoi().
🌐
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 ...
🌐
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
🌐
LeetCode
leetcode.com › problems › string-to-integer-atoi
String to Integer (atoi) - LeetCode
8. String to Integer (atoi) Medium · 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: Whitespace: Ignore any leading whitespace (" "). Signedness: ...
🌐
W3Schools
w3schools.com › c › ref_stdlib_atoi.php
C stdlib atoi() Function
The atoi() function reads a representation of a whole number from a string and returns its value.
🌐
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.
Find elsewhere
🌐
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
🌐
SAS
support.sas.com › documentation › onlinedoc › sasc › doc700 › html › lr1 › z2054672.htm
Function Descriptions : atoi
atoi converts the character string str to an integer. The string should consist of an optional plus or minus sign, followed by one or more decimal digits.
🌐
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.

🌐
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 - This program shows how numbers stored as strings can be converted to numeric values using the atoi functions.
🌐
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
🌐
Stack Overflow
stackoverflow.com › questions › 16346332 › cant-make-atoi-take-in-a-string-string-vs-c-string
c++ - Can't make atoi take in a string (string vs. C-string?) - Stack Overflow
May 22, 2017 - I have read a line from a file and I am trying to convert it to an int. For some reason atoi() (convert string to integer) won't accept a std::string as an argument (possibly some issue with string...
🌐
AlgoMonster
algo.monster › home › 8. string to integer (atoi)
8. String to Integer (atoi) - In-Depth Explanation
The problem asks you to implement a function myAtoi(string s) that converts a string to a 32-bit signed integer, similar to C/C++'s atoi function.
🌐
CS50
manual.cs50.io › 3 › atoi
atoi - CS50 Manual Pages
The atoi() function converts the initial portion of the string pointed to by nptr to int.