Convert Char to Int in C
Convert Char to Int in C
Introduction:
- The conversion of one data type to another is an accurately common occurrence in programming. Functions that can only be used with particular data types are usually generated by user.
- In that case, converting data to the specific data type that the method can handle is necessary before calling the function if your data is in another format.
- As a result, we'll run through some of the several methods in this manually for C programmes that can be used to convert a character to an integer.
In C Programming Language Conversion Techniques for Char to Int
In the C programming language, there are three possible ways to convert a character to an integer, and we will go over each one in the components that follow:
- Typecasting Technique
- Usage of sscanf Technique
- Usage of atoi Technique
Typecasting Technique:
- Make a declaration for and create the conversion character.
- When converting a character to an int, typecast the character.
- Utilising printf, and display the integer.
Example:
// C program implementing the typcasting for the conversion of character to integer
#include <stdio.h>
#include <stdlib.h>
// main function
int main()
{
char ch = 'J';
int a;
a=(int)(ch);
printf("The converted value of the given character is :%d",a);
return 0;
}
OUTPUT:
![Convert Char to Int in C](https://static.tutorialandexample.com/c/convert-char-to-int-in-c1.png)
Explanation:
- With the inclusion of the needed libraries, we initialised the character "a" in the character type variable "c" of this script, which we would later convert to an integer.
- Next, we declare the variable "x" of integer type. The value of the letter "c" has been typecast into this variable to make it equal.
- Next, we used a "printf" statement to output the integer value to the terminal.
Usage of sscanf Technique:
s is used to read data from, and then it stores it where the additional arguments in the parameter format specify. The char to int conversion using sscanf in C is shown below.
Example:
OUTPUT:
![Convert Char to Int in C](https://static.tutorialandexample.com/c/convert-char-to-int-in-c2.png)
Explanation:
- In this programme, the constant character type variable "*ch" has been defined, and the value "123" has been given to it. Then we defined the integer "a".
- Then, we saved the value of "ch" to "a" after converting it to an integer using the "sscanf" function.
- Finally, we have printed the value of "x" using the "printf" statement.
Usage of atoi Technique:
If the execution is successful, the atoi() method returns the integer value that was transformed. If an integer cannot be created from the provided string, it will return 0.
The C program to use atoi() to convert char to int is shown below.
// C program implementing the usage of atoi() for the conversion of character to integer
#include <stdio.h>
#include <stdlib.h>
// main function
int main()
{
const char* ch = "136";
int y;
y = atoi(ch);
printf("\nThe converted value of the given character is %d", y);
return 0;
}
OUTPUT:
![Convert Char to Int in C](https://static.tutorialandexample.com/c/convert-char-to-int-in-c3.png)
Explanation:
- The constant character type variable we utilised in our second technique has been used once more in this code.
- The integer variable "y" was then defined using the "atoi" function, and it was equalised to the converted value of the constant character "ch."
- Last but not least, we printed this converted value on the terminal.
Handling Strings including characters (Exceptional):
- This only applies to the integers 0 through 9 because char can only hold a single character.
- The strtol (string to long) function, which is a part of C's stdlib, can be used if you have a number like "12" saved as a character array or string.
Example:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char eleven_string[] = "11";
char *output;
long result = strtol(eleven_string, &output, 10) + 1;
printf("%s + 1 is %ld\n", eleven_string, result);
}
OUTPUT:
![Convert Char to Int in C](https://static.tutorialandexample.com/c/convert-char-to-int-in-c4.png)
Explanation:
- The second argument for the strtol function is an output pointer. In the event that your input string includes both numbers and other characters, the output variable will point to the first character that comes after the parsed long.
- In the preceding example, neither this nor its application are necessary.
- Since the function also takes a base as its third argument, we use 10 in the example above to indicate that the long should be parsed in base 10.
- Remember that if there are any problems, this function will simply cast your variable to 0. Because it cannot convert the array "eleven" to an integer and must instead sum 0 and 1, the following code will print "eleven + 1 is 1".
They are simplified versions of the preceding strtol function that convert to long, integer, and float, respectively, and don't need the output variable or base to be supplied (base 10 is used by default).
atol
atof
atoi