C / C ++ atoi()函数教程–将字符串转换为整数

C and C++ programming languages provide string or character to integer conversion with the atoi() function. atoi simply the short form of chArTOInteger where the uppercase letters stand side by side. The function is provided by the standard library which means we do not need to install an extra library or package.

C和C ++编程语言使用atoi()函数提供字符串或字符到整数的转换。 atoi只是chArTOInteger的缩写,大写字母并排放置。 该功能由标准库提供,这意味着我们不需要安装额外的库或软件包。

atoi()函数语法 (atoi() Function Syntax)

atoi() function has a very simple syntax.

atoi()函数的语法非常简单。

int atoi (const char * str);
  • int integer type which is the type of returned value.

    int整数类型,它是返回值的类型。

  • const char * is a constant char array that is equal to a string whose variable name is str.

    const char *是一个常量char数组,它等于一个变量名称为str

在C中包含<stdlib.h>标头 (Include <stdlib.h> Header In C)

atoi function is provided from the standard library which provides basic and popular functions for application development. So in order to use atoi() function stdlib.h the header should be included as below.

标准库提供了atoi函数,该函数提供了应用程序开发的基本功能和常用功能。 因此,为了使用atoi()函数stdlib.h,标题应如下所示。

#include <stdlib.h>

在C ++中包含<cstdlib>标头 (Include <cstdlib> Header In C++)

In C++ atoi() function can be used with the cstdlib header or library. So in order to use atoi() function in C++, we should include this header.

在C ++中, atoi()函数可与cstdlib标头或库一起使用。 因此,为了在C ++中使用atoi()函数,我们应包含此标头。

#include <cstdlib>

在C和C ++中将字符串/字符转换为整数 (Convert String/Char To Integer In C and C++)

We will start with a simple example where we will convert a number in string or char format. In this example, we will convert the “1234” string into an integer. As we can see the “1234” string consist of 4 numbers and can be stored in a int or integer variable.

我们将从一个简单的示例开始,在该示例中,我们将转换字符串或char格式的数字。 在此示例中,我们将“ 1234”字符串转换为整数。 如我们所见,“ 1234”字符串由4个数字组成,可以存储在int或整数变量中。

C: (C:)

/* String To Integer with atoi() function */
#include <stdio.h>      /* printf, fgets */
#include <stdlib.h>     /* atoi */

int main ()
{
  int i;
  char num[4] = "1234";
  i = atoi (num);
  printf ("The value entered is %d.",i);
  return 0;
}

C ++: (C++:)

/* String To Integer with atoi() function */
#include <iostream>      /* printf, fgets */
#include <cstdlib>     /* atoi */

int main ()
{
  int i;
  char *num = "1234";
  i = atoi (num);
  printf ("The value entered is %d.",i);
  return 0;
}

将字符串/字符转换为负整数 (Convert String/Char To Negative Integer)

In the previous example, we have converted a string that expresses a positive number into an integer type. We can also convert or cast a negatively expressed number into an integer. In this example, we will convert “-4321” string into an integer. Keep in mind that there are 5 characters in string presentation of the negative number so the char array or string will be 5 characters long.

在前面的示例中,我们已将表示正数的字符串转换为整数类型。 我们还可以将负数表示的数字转换或转换为整数。 在此示例中,我们将“ -4321”字符串转换为整数。 请记住,字符串表示形式中有5个负数,因此char数组或字符串的长度为5个字符。

C: (C:)

/* String To Integer with atoi() function */
#include <stdio.h>      /* printf, fgets */
#include <stdlib.h>     /* atoi */

int main ()
{
  int i;
  char num[5] = "-1234";
  i = atoi (num);
  printf ("The value entered is %d.",i);
  return 0;
}

C ++: (C++:)

/* String To Integer with atoi() function */
#include <iostream>      /* printf, fgets */
#include <cstdlib>     /* atoi */

int main ()
{
  int i;
  char *num = "-1234";
  i = atoi (num);
  printf ("The value entered is %d.",i);
  return 0;
}

更多atoi()函数示例 (More atoi() Function Examples)

We have already examined standard and simple examples of the atoi() function but there may be some complex and hard to understand cases about converting string or char integer into an integer data type.

我们已经检查了atoi()函数的标准示例和简单示例,但是关于将字符串或char整数转换为整数数据类型的情况可能有些复杂且难以理解。

#include <iostream>
#include <cstdlib>

int main()
{
    const char *str1 = "57";
    const char *str2 = "314.159";
    const char *str3 = "52345 some text";
    const char *str4 = "some text 25";

    int mynum1 = std::atoi(str1);
    int mynum2 = std::atoi(str2);
    int mynum3 = std::atoi(str3);
    int mynum4 = std::atoi(str4);

    std::cout << "atoi(\"" << str1 << "\") is " << mynum1 << '\n';
    std::cout << "atoi(\"" << str2 << "\") is " << mynum2 << '\n';
    std::cout << "atoi(\"" << str3 << "\") is " << mynum3 << '\n';
    std::cout << "atoi(\"" << str4 << "\") is " << mynum4 << '\n';
}

The output will be like below.

输出将如下所示。

More atoi() Function Examples
More atoi() Function Examples
更多atoi()函数示例

We can see that there are some rules about converting string or char array into integer where we can list them below.

我们可以看到有一些关于将字符串或char数组转换为整数的规则,我们可以在下面列出它们。

  • If given string or char array is floating-point like “314.159”  in only integer part will be converted where the result will be “314”

    如果给定的字符串或char数组是浮点数,例如“ 314.159”,则仅整数部分将被转换,结果为“ 314”
  • If there are some non-numerical chars in the given char array or string they will not convert and there will be no error where only the integer part will be converted. As an example “52345 some text” will be converted 52345

    如果给定的char数组或字符串中有一些非数字char,它们将不会转换,并且只有整数部分会被转换也不会出错。 例如,“ 52345 some text”将被转换为52345
  • If the start of the char array or string and there are numerical characters after them this will be converted into 0 as an integer value. As an example “some text 25” will be converted into 0.

    如果char数组或字符串的开头,并且后面有数字字符,则将其转换为0作为整数值。 例如,“一些文本25”将转换为0。
  • If the numerical characters are between non-numeric characters the conversion result will be 0 too.

    如果数字字符介于非数字字符之间,则转换结果也将为0。
  • If the char array or string starts with numeric character and after that, there is a non-numeric, and then numeric character only the starting numeric character will be converted. As an example “25 text 50” will be converted into 25.

    如果char数组或字符串以数字字符开头,并且之后是非数字字符,则仅将起始数字字符转换为数字字符。 例如,“ 25个文本50”将转换为25。

翻译自: https://www.poftut.com/c-c-atoi-function-tutorial-convert-string-to-integer/

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值