![]() |
VOOZH | about |
Write a C program to convert the given numeric character to integer.
Example:
Input: '3'
Output: 3
Explanation: The character '3' is converted to the integer 3.Input: '9'
Output: 9
Explanation: The character '9' is converted to the integer 9.
There are 3 main methods to convert the char to int in C language as follows:
Table of Content
Let's discuss each of these methods in detail.
Each character in C has an ASCII value, a unique numerical representation. For numeric characters like '0', '1', etc., the ASCII value starts from 48 for '0' and increases sequentially. To convert a char to its integer equivalent, subtract the ASCII value of '0' from the character.
The integer value of character '7' is 7
Time Complexity: O(1)
Space Complexity: O(1)
The sscanf() function can be used to read formatted input from a string. It can convert a character to an integer by treating the character as a string and reading it using the format specifier for an integer (%d).
103
Time Complexity: O(1)
Space Complexity: O(1)
The atoi() function from the C Standard Library <stdlib.h> can convert a string of digits into an integer. Although atoi() is designed for strings, it can be used with single-character strings.
The integer value of character '5' is 5
Time Complexity: O(1)
Space Complexity: O(1)