This blog post will teach you how to convert a string to an integer in C. Almost programming language provides the library function to convert a string to an integer. The atoi() function converts a string to an integer in the C programming language.
So let’s see the first example using the atoi(). But the atoi() function has some limitations, so if you are not familiar with the atoi() function, then it is my suggestion that you should first read the atoi library function. You can check the other post “How to use atoi() and how to make own atoi() “.
Using the atoi():
The atoi() function converts a string to the int representation. It takes a string as an argument and returns its integer value.
Consider the below example where the atoi function is converting a numeric string to an integer.
#include <stdio.h> #include<stdlib.h> int main() { const char* str1 = "2706"; int num = atoi(str1); printf("%d",num); return 0; }
Output: 2706
Using the strtol():
The strtol() function converts a string to the long int representation. But if no conversion could be performed, zero is returned.
The strtol() function discard all leading whitespace character at the beginning of the string until the first non-whitespace character is found. And after it starts conversion until it finds the first character that is not a number.
If the converted value is outside the range of long int
, function returns LONG_MAX
or LONG_MIN
and errno is set to ERANGE.
strtol() Syntax:
long int strtol(const char *restrict nptr, char **restrict endptr, int base);
Parameters:
nptr
– pointer to the null-terminated byte string to be interpreted
endptr
– is a pointer to indicate where the conversion stops (could be null).
base
– set of valid values for base is {0,2,3,…,36}.
Now I think you have sufficient knowledge of strtol, so let’s see the example of how to strtol converts a string to int.
#include<stdio.h> #include<stdlib.h> int main() { const char str[] = "2324"; char *endPtr; const long int d = strtol(str, &endPtr, 10); printf("%ld\n",d); return 0; }
Output: 2324
Using the strtoimax() or strtoumax():
The strtoimax() and strtoumax() functions are equivalent to the strtol, strtoll, strtoul, and strtoull functions, except that the initial portion of the string is converted to intmax_t
and uintmax_t
representation, respectively.
Syntax:
#include <inttypes.h> intmax_t strtoimax(const char * restrict nptr, char ** restrict endptr, int base); uintmax_t strtoumax(const char * restrict nptr, char ** restrict endptr, int base);
Parameters:
nptr
– pointer to the null-terminated byte string to be interpreted
endptr
– is a pointer to indicate where the conversion stops (could be null).
base
– set of valid values for base is {0,2,3,…,36}.
#include <stdio.h> #include <inttypes.h> int main() { char* endptr1; char* endptr2; /* base 10*/ printf("%ld\n", strtoimax("2324",&endptr1,10)); /* base 10*/ printf("%lu\n", strtoumax("2324",&endptr2,10)); return 0; }
Output:
2324
2324
Using own implemented method:
#include<stdio.h> #define Is_NUMERIC_STRING(d) (*(char*)d >= 48) && (*(char*)d<= 57) int StringToInt(const char *pStr) { int result = 0; // variable to store the result int sign = 1; //Initialize sign as positive if(pStr == NULL) //If pointer is null return 0; // if whitespaces then ignore. while ((*pStr) == ' ') { ++pStr; } //If number is negative, then update sign if((*pStr) == '-') { sign = -1; ++pStr; //Increment the pointer } while(Is_NUMERIC_STRING(pStr)) //check string validity { result = (result*10)+ (*pStr-48); pStr++; //Increment the pointer } return (sign * result); } int main() { const char str[] = "2324"; const int d = StringToInt(str); printf("%d\n",d); return 0; }
Recommended Post:
- C Programming Courses And Tutorials.
- CPP Programming Courses And Tutorials.
- Python Courses and Tutorials.
- C program to convert a given number to words.
- Use of strncat in C.
- Stringizing operator (#) in C.
- C String Literals with Its Types.
- How to use atoi() and how to make your own atoi()?
- Token Pasting Operator in C/C++ programming.
- C program to trim trailing white spaces from a string.
- C Program to convert uppercase string to lowercase string