In this article, we are going to learn different techniques of how to convert string to integer in C language. The techniques that we are going to learn in this article are:
How to convert string to Integer in C
- atoi() Function to Convert a String to an Integer in C
- strtol() Function to Convert a String to an Integer in C
- strtoumax() Function for Convert String to an Integer in C
1. atoi() Function to Convert a String to an Integer in C
In this example, we are going to make use of the atoi() function from the C standard library. This function can be used by just including the header file in your program. This function accepts a char input and returns the integer.Let us understand this function used in the below code example to convert a string to an integer number.If we have to convert integer to string
Syntax
int atoi(const char *str);
- *str is a pointer to a string to be converted to an integer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void)
{
int Num;
char str[20];
strcpy(str,"55");
Num = atoi(str);
printf("The String input = %s is equal to integer = %d\n", str, Num);
return(0);
}
Output
The String input = 55 is equal to integer = 55
2. strtol() Function to Convert a String to an Integer in C
In this example, we are going to make use of the strol() function from the C Standard library. This function can be used by just including the header file in your program. This function accepts a char input and returns the number equivalent in a long integer.
Syntax
long int strtol(const char *str, char **end,int base);
Parameters
- *str = is a pointer to a string to be converted to a long integer.
- **end = is a pointer to indicate where the conversion stops.
- base = is the base representation with the range of [2, 36]. For example 2 is binary, 8 is octal etc.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char str[10];
char *ptr;
long Num;
strcpy(str, "55");
Num = strtol(str, &ptr, 10);
printf("The String input = %s is equal to Base 10(Decimal) long integer = %ld\n", str, Num);
return 0;
}
Output
he String input = 55 is equal to Base 10(Decimal) long integer = 55
3. strtoumax() Function for Convert String to an Integer in C
In this example, we are going to make use of the strtoumax() function from the C Standard library. This function can be used by just including header file in your program.This function accepts a char input and return the number equivalent in integer.
Syntax
int strtoumax(const char* str, char** end, int base);
Parameters
- *str = is a pointer to a string to be converted to a long integer.
- **end = is a pointer to indicate where the conversion stops.
- base = is the base representation with the range of [2, 36]. For example 2 is binary, 8 is octal etc.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char str[10];
char *ptr;
int Num;
strcpy(str, "55");
Num = strtoumax(str, &ptr,10);
printf("The String input = %s is equal to integer = %d\n", str, Num);
return 0;
}
Output
The String input = 55 is equal to integer = 55