. Advertisement .
..3..
. Advertisement .
..4..
Due to the low-level nature of C, calculations can’t just be done on strings even if they have numeric characters. You will need to convert a string to an integer in C first, and this tutorial will show you how.
How To Convert A String To An Integer In C
Using atoi()
The atoi() function in C takes a string as its input and returns an integer value. You can use it to convert an array of characters to int with this syntax:
int atoi(const char * str);
If the string doesn’t have a numeric character, atoi() returns 0. The function will stop the conversation when it encounters the alphabet or special characters.
Example:
#include <stdio.h>
#include <stdlib.h>
int main() {
// Convert a string of numeric characters
char string1[20] = "1981";
int result1 = atoi(string1);
printf("Converting '1981' to int: %d\n", result1);
// Convert a string of alphabet characters
char string2[20] = "Hello world!";
int result2 = atoi(string2);
printf("Converting 'Hello world!' to int: %d\n", result2);
// Convert a string of both alphabet and numeric characters
char string3[20] = "1981Hello!";
int result3 = atoi(string3);
printf("Converting '1981Hello!' to int: %d\n", result3);
// Convert a string of both alphabet and numeric characters
char string4[20] = "Hello1981!";
int result4 = atoi(string4);
printf("Converting 'Hello1981!' to int: %d\n", result4);
// Convert a string with spaces
char string5[20] = "19 81";
int result5 = atoi(string5);
printf("Converting '20 22' to int: %d\n", result5);
// Convert a string with decimals
char string6[20] = "19.81";
int result6 = atoi(string6);
printf("Converting '19.81' to int: %d\n", result6);
return 0;
}
Output:
Converting '1981' to int: 1981
Converting 'Hello world!' to int: 0
Converting '1981Hello!' to int: 1981
Converting 'Hello1981!' to int: 0
Converting '20 22' to int: 19
Converting '19.81' to int: 19
Note that the atoi() function doesn’t recognize whitespace characters (except leading ones) or decimals either. Like other scenarios when it runs into non-numeric characters, the string will no longer be processed.
Using sscanf()
The sscanf() function works in a similar way as scanf() and fscanf() and can also read formatted data. But unlike them, sscanf() reads the input from a string. It allows you to define the format and variable you want to store the data.
Example:
#include <stdio.h>
int main() {
char text_sample[]="1981";
int result;
sscanf(text_sample, "%04d", &result);
printf("Converting '1981' to int: %d\n", result);
return 0;
}
Output:
Converting '1981' to int: 1981
The sscanf() function in the snippet reads the text array and puts its content into the result – an int variable.
Using strtol() and strtoll()
If you want to convert C strings into long and long int values, check out the strtol() and strtoll() functions. They have the same syntax, which needs to know the string you want to convert, the pointer of the first non-numeric character, and the input’s integer base.
Example:
#include <stdlib.h>
#include <stdio.h>
int main() {
char string1[50] = "1012345678";
char string2[50] = "0x24D47CBED26TA";
char *remaining;
long result1;
long long result2;
long result3;
result1 = strtol(string1, &remaining, 10);
result2 = strtoll(string1, &remaining, 10);
result3 = strtol(string2, &remaining, 16);
printf("Convert '1012345678' to long int: %ld\n", result1);
printf("Convert '1012345678' to long long int: %ld\n", result2);
printf("Convert '0x24D47CBED26TA' to long int (hexadecimal): %ld\n", result3);
return 0;
}
Output:
Convert '1012345678' to long int: 1012345678
Convert '1012345678' to long long int: 1012345678
Convert '0x24D47CBED26TA' to long int (hexadecimal): 2530940284198
The code first converts str1 to long and long int values. It also demonstrates the ability of strtol() to take on non-decimal values like hexadecimal.
Wrapping Up
Like getting a substring, you can convert a string to an integer in C by using several functions in this language’s standard library. They work in different ways, and many factors need to be taken into account when you decide to go with one of them.
Leave a comment