Home >Common Problem >How to convert C language characters into numbers

How to convert C language characters into numbers

zbt
zbtOriginal
2023-08-09 15:06:4131963browse

Method to convert c language characters into numbers: 1. Use forced type conversion to force the character '5' to an integer type, and then subtract the ASCII code value of the character '0' to get the number 5; 2. Use the library function atoi() to convert the string "123" into the integer 123 through the atoi() function, and print out the result; 3. Use the library function sscanf() to convert the string "456" from the string "456" through the sscanf() function Read the integer from , store the result in the variable num, and finally print out the result.

How to convert C language characters into numbers

The operating environment of this tutorial: Windows 10 system, C 20 version, DELL G3 computer.

In C language, characters are stored in the form of ASCII code. ASCII code is an encoding system that corresponds characters to numbers. It defines a standard encoding of 128 characters. In C language, there are some methods to convert characters into numbers.

1. Use type casting:

In C language, you can use type casting to convert characters into corresponding numbers. For example, to convert the character '5' to the corresponding number 5, you can use the following code:

char c = '5';
int num = (int) c - '0';

In this example, the character '5' is cast to an integer, and then the character '0' is subtracted ASCII code value to get the number 5. This is because the ASCII codes of characters ‘0’ to ‘9’ increase in sequence, and by subtracting the ASCII code value of character ‘0’, the corresponding number can be obtained.

2. Use the library function atoi():

The stdlib.h library in C language provides a function atoi() that can convert a string into an integer . The parameter of this function is a string, and the return value is the corresponding integer.

#include
#include
int main() {
char str[] = "123";
int num = atoi(str);
printf("%d\n", num);
return 0;
}

In this example, the string "123" is converted into the integer 123 through the atoi() function, and the result is printed.

3. Use the library function sscanf():

The stdio.h library in C language provides a function sscanf(), which can be read from Read data from string. By specifying the format as "%d", you can convert a string into an integer.

#include
int main() {
char str[] = "456";
int num;
sscanf(str, "%d", &num);
printf("%d\n", num);
return 0;
}

In this example, the sscanf() function is used to read the integer from the string "456", store the result in the variable num, and finally print out the result.

Whether you use forced type conversion, atoi() function or sscanf() function, characters can be converted into corresponding numbers. It should be noted that the range of the conversion result should be within the range that the integer can represent, otherwise it may overflow. In addition, make sure the characters are numeric characters during the conversion process, otherwise the results may be inaccurate.

The above is the detailed content of How to convert C language characters into numbers. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What does dmz host mean?Next article:What does dmz host mean?