Convert a string to an integer using atoi

int atoi ( const char * str );

Getting input from keyboard (stdin)

#include <stdio.h>
#include <stdlib.h>
int main ()
{
    int i;
    char str[256];
    printf ("Enter a number: ");
    fgets ( str, 256, stdin );
    i = atoi (str);
    printf ("The integer value is: %d.",i);
    return 0;
}

Using a const char*

#include <stdio.h>
#include <stdlib.h>
int main ()
{
     const char* str = "12345";
     int i = atoi(str);
     printf ("The integer value is: %d.",i);
     return 0;
}

Using a C++ style string

#include <cstdlib>
#include <string>
int main ()
{
     std::string text = "12345";
     int i= std::atoi( text.c_str() );
     printf ("The integer value is: %d.",i);
     return 0;
}

The atoi function converts a const char* into an integer. The string should start with whitespace, a plus or minus sign, or a digit. The function will continue reading from the string until it reaches a non-numerical character.

On success, the function returns the converted number as an int.
If no valid conversion could be performed, a zero is returned.
If the correct value is out of the range of representable values, INT_MAX or INT_MIN is returned.

This method of converting to an integer is not recommended because there is no way of distinguishing between the string "0" and and error.