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
๐ŸŒ
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. Defined inside <stdlib.h> header file. ... #include <stdio.h> #include <stdlib.h> int main() { char str[] = "12345"; int ...
๐ŸŒ
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 ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ c_standard_library โ€บ c_function_atoi.htm
C library - atoi() function
Following is the syntax of the c atoi() function โˆ’ ... This function returns an integral number, represents int value. If the input string is not a valid string's number, it returns 0.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ convert-string-to-int-in-c
Convert String to int in C - GeeksforGeeks
July 23, 2025 - There are 4 methods to convert a string to int which are as follows: ... The atoi() function in C takes a character array or string literal as an argument and returns corresponding value as integer.
๐ŸŒ
TechOnTheNet
techonthenet.com โ€บ c_language โ€บ standard_library_functions โ€บ stdlib_h โ€บ atoi.php
C Language: atoi function (Convert String to Integer)
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.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ dsa โ€บ write-your-own-atoi
String to Integer - Write your own atoi() - GeeksforGeeks
... Traverse the string from left to right, skipping whitespaces, handling the optional sign, and building the number digit by digit. At each step, ensure the value stays within 32-bit integer limits to avoid overflow.
Published: April 14, 2026
๐ŸŒ
ZetCode
zetcode.com โ€บ clang โ€บ atoi
C atoi Tutorial: String to Integer Conversion with Practical Examples
The atoi function converts a string to an integer. 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.
Find elsewhere
๐ŸŒ
SysAdminSage
sysadminsage.com โ€บ home โ€บ blog โ€บ convert string to int c: full guide
Convert String to Int C: Full Guide - SysAdminSage
August 21, 2026 - If you require a quick, basic conversion and are not concerned with validating the input data, atoi() is straightforward. It takes a C-style string (const char*) and attempts to convert the beginning of the string into an integer (int).
๐ŸŒ
Udemy
blog.udemy.com โ€บ home โ€บ it & development โ€บ software development โ€บ c string to int: simple ways to convert to numeric values
C String to Int: Simple Ways to Convert to Numeric Values - Udemy Blog
April 14, 2026 - Next, we loop through the string and convert the string into decimal values. Finally, the string is converted into an integer and printed on the screen. The atoi() function converts a string data type to integer data type in the C language.
๐ŸŒ
WsCube Tech
wscubetech.com โ€บ resources โ€บ c-programming โ€บ programs โ€บ string-to-int
How to Convert String to Int in C? 5 Ways With Code
September 17, 2026 - Learn five easy ways to convert a string to an int in C using atoi(), strtol(), sscanf(), and manual conversion. Includes code examples!
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c++ โ€บ convert-string-to-int-in-cpp
Convert String to int in C++ - GeeksforGeeks
May 23, 2026 - "3.14159" stops conversion at . and returns 3. "31337 geek" stops at the first non-digit character and returns 31337. The atoi() function in C++ takes a character array or string literal as an argument and returns its value in an integer.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ How-to-convert-a-string-to-a-integer-in-C
How to convert a string to a integer in C
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char *name = "The Matrix(1999)"; char *ps; char year_str[5] = ""; int year, i; /* Find the opening bracket */ ps = strchr(name, '('); if (ps != NULL) { /* Extract characters between brackets */ for (i = 1; i < strlen(ps) - 1 && i < 5; i++) { year_str[i-1] = ps[i]; } year_str[4] = '\0'; /* Convert to integer */ year = atoi(year_str); printf("Movie: %s<br>", name); printf("Year: %d<br>", year); } return 0; }
๐ŸŒ
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.
๐ŸŒ
C++ Better Explained
cppbetterexplained.com โ€บ home โ€บ posts โ€บ how to convert string to int in c++: stoi, atoi, and stringstream | cppbe
How to Convert String to int in C++: stoi, atoi, and stringstream | CppBE
May 23, 2026 - std::string s = "123abc"; size_t ... Just $19. atoi is the older C-style function from <cstdlib>. It converts a const char* (not a std::string) to an int....
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ c-program-to-write-your-own-atoi
C Program To Write Your Own atoi()
March 15, 2026 - String: "12345" -> Integer: 12345 String: " 678" -> Integer: 678 String: "99abc" -> Integer: 99 ยท This enhanced version handles both positive and negative numbers ยท #include <stdio.h> int myAtoi(const char *str) { int result = 0; int sign = 1; int i = 0; /* Skip leading whitespaces */ while (str[i] == ' ' || str[i] == '\t' || str[i] == '<br>') { i++; } /* Handle sign */ if (str[i] == '+' || str[i] == '-') { sign = (str[i] == '-') ?
๐ŸŒ
Linux Man Pages
linux.die.net โ€บ man โ€บ 3 โ€บ atoi
atoi(3): convert string to integer - Linux man page
The atoi() function converts the initial portion of the string pointed to by nptr to int. The behavior is the same as strtol(nptr, NULL, 10); except that atoi() does not detect errors.
๐ŸŒ
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 );
๐ŸŒ
Northern Michigan University
nmu.edu โ€บ Webb โ€บ ArchivedHTML โ€บ MathComputerScience โ€บ c_programming โ€บ c_068.htm
C Programming - atoi()
atof() converts an ascii character array to a float atoi() converts an ascii character array to an integer itoa() converts an integer to a character array ยท Example ยท /* convert a string to an integer */ #include <stdio.h> #include <stdlib.h> char string[] = "1234"; main() { int sum; sum ...