Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The example usage of atoi () function in C++

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains the "example usage of the atoi () function in C++". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn the example usage of the atoi () function in C++.

Catalogue

1 function

2 format

3 points for attention

3.1 points for attention on parameters

3.2 considerations about the return value

3.3 determine whether the conversion is successful or not

Conversion of 4 wide characters

1 function

The atoi () function converts a string in numeric format to an integer type. For example, convert the string "12345" to the number 12345.

2 format

The format of this function is

Int atoi (const char* str)

Where the parameter str is the string to be converted and the return value is the converted integer.

3 Note 3.1 matters needing attention on parameters

In format 2, it is mentioned that the argument to the atoi () function is the string to be converted. The format of the string is

[space] [symbol] [number]

Among them, the space can be a space character in the keyboard or a Tab character; the symbol can be a "+" for a positive number or a "-" for a negative number; a number is a numeric string. So, the argument to the atoi () function can be

"+ 123"

"- 456"

It is important to note that spaces and "+" can be omitted. So, the argument to the atoi () function can also be

"123"

"- 456"

3.2 considerations about the return value

If the atoi () function is converted successfully, the return value of the function is the converted integer. If the conversion of the atoi () function fails, for example, the type to be converted exceeds the range represented by int, return INT_MAX (2147483647) if you want to convert a positive number, or INT_MIN (- 2147483648) if you want to convert a negative number. The code is as follows

Str1 = "33364027351707160320"; value1 = atoi (str1); if (INT_MAX = = value1) {printf ("the value to be converted exceeds the upper boundary of int.\ n");} str2 = "- 33364027351707160320"; value2 = atoi (str2); if (INT_MIN = value2) {printf ("the value to be converted exceeds the lower boundary of int.\ n");}

It is important to note that the atoi () function is defined in stdlib.h, so you need to include this header file when using the atoi () function.

The atoi () function stops reading and returns when it reads the'\ 0' character in the parameter.

3.3 determine whether the conversion is successful or not

Although the arguments and return values of the atoi () function do not contain a flag for the success of the conversion, you can determine the success of the conversion by the value of the system variable errno.

Related links:

The system variable errno:errno is the last error code that records the system. The code is an int value defined in errno.h. Looking at the error code errno is an important way to debug a program.

When calling the atoi () function, if the conversion is successful, the value of errno is 0; if you encounter the out-of-range error mentioned in "3.2 Notes about return values", the value of errno will be set to ERANGE.

If (errno = = ERANGE) {printf ("the number to be converted is out of the int range.\ n");} 4 wide character conversion

The wide-string version of the atoi () function is the _ wtoi () function. The format of this function is

Int _ wtoi (const wchar_t* str)

The meaning of its parameters and return values is similar to the atoi () function.

Thank you for your reading. The above is the content of "the example usage of the atoi () function in C++". After the study of this article, I believe you have a deeper understanding of the example usage of the atoi () function in C++, and the specific usage still needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report