How to Convert a String to Integer in C

  1. atoi() Function to Convert a String to an Integer in C
  2. strtol() Function to Convert a String to an Integer in C
  3. strtoumax() Function for Convert String to an Integer in C

This article introduces different methods in a C programming language to convert a string into an integer. There are several methods for convert a string into an integer in C like atoi(),strtoumax() and strol().

atoi() Function to Convert a String to an Integer in C

The atoi() function converts a string into an integer in the C programming language. The atoi() function neglects all white spaces at the beginning of the string, converts the characters after the white spaces, and then stops when it reaches the first non-number character.

The atoi() function returns the integer representation of the string.

We need to include the <stdlib.h> header to use the atoi() function.

atoi() Syntax

int atoi(const char *str);

*str is a pointer to a string to be converted to an integer.

atoi() Example Codes

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (void) 
{
    int value;
    char str[20];
    strcpy(str,"123");
    value = atoi(str);
    printf("String value = %s, Int value = %d\n", str, value);

    return(0);
}

Output:

String value=123, Int value=123

strtol() Function to Convert a String to an Integer in C

The strtol() function converts a string into a long integer in the C programming language. The strtol() function omits all white-spaces characters at the beginning of the string, after it converts the subsequent characters as part of the number, and then stops when it finds the first character that isn’t a number.

The strtol() function returns the long integer value representation of a string.

We need to include the <stdlib.h> header to use the atoi() function.

strtol() Syntax

long int strtol(const char *string, char **laststr,int basenumber);
  • *string is a pointer to a string to be converted to a long integer.
  • **laststr is a pointer to indicate where the conversion stops.
  • basenumber is the base with the range of [2, 36].
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    char str[10];
    char *ptr;
    long value;
    strcpy(str, " 123");
    value = strtol(str, &ptr, 10);
    printf("decimal %ld\n", value);

    return 0;
}

Output:

decimal 123

strtoumax() Function for Convert String to an Integer in C

The strtoumax() function interprets a string’s content as in the form of an integral number of the specified base. It omits any whitespace character until the first non-whitespace character. It then takes as many characters as possible to form a valid base integer number representation and converts them to an integer value.

strtoumax() returns the corresponding integer value of a string. This function returns 0 if the conversion is not successfully done.

strtoumax() Syntax

uintmax_t strtoumax(const char* string, char** last, int basenumber);
  • *string is a pointer to a string to be converted to a long integer.
  • **last is a pointer to indicate where the conversion stops.
  • basenumber is the base with the range of [2, 36].

strtoumax() Example

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main(void)
{
    char str[10];
    char *ptr;
    int value;
    strcpy(str, " 123");
    printf("The integer value:%d",strtoumax(str, &ptr,10));   

    return 0;
}

Output:

The long integer value: 123

Related Article - C String

  • How to Convert an Integer to a String in C
  • How to Get a Substring in C
  • Related Article - C Integer

  • How to Get a Substring in C