Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - How to Convert String into Integer Value in C Language
How to Convert String into Integer Value in C Language
There are two ways to convert a string into an integer in C language.

1 Use the atoi function.

Atoi is used to convert a string into an integer and return it. Its statement is

int atoi(char * str);

For example, ATOI ("1234"); Returns the integer 1234.

To call atoi, you need to refer to the header file stdio.h

2 use sscanf.

Sscanf is similar to the standard input function scanf, but the source is not a standard input, but a string.

Sscanf can handle more complex strings.

For example, the string char * str = "a= 1, b = 2 ";;

Define int a, b; After ...

can use

Sscanf(str, "a=%d, b=%d", & i, & b);

Extract the values of a and b, and after calculation, a = 1 and b = 2.

To use sscanf, you also need to refer to the header file stdio.h