C 库函数 - atoi()

C 语言教程 · 2019-02-24 05:56:57

描述

C 库函数 int atoi(const char *str) 把参数 str 所指向的字符串转换为一个整数(类型为 int 型)。

声明

下面是 atoi() 函数的声明。

int atoi(const char *str)

参数

  • str -- 要转换为整数的字符串。

返回值

该函数返回转换后的长整数,如果没有执行有效的转换,则返回零。

实例

下面的实例演示了 atoi() 函数的用法。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
   int val;
   char str[20];
   
   strcpy(str, "98993489");
   val = atoi(str);
   printf("字符串值 = %s, 整型值 = %d\n", str, val);

   strcpy(str, "codercto.com");
   val = atoi(str);
   printf("字符串值 = %s, 整型值 = %d\n", str, val);

   return(0);
}

让我们编译并运行上面的程序,这将产生以下结果:

字符串值 = 98993489, 整型值 = 98993489
字符串值 = codercto.com, 整型值 = 0

点击查看所有 C 语言教程 文章: https://codercto.com/courses/l/17.html

查看所有标签

C# Primer Plus

C# Primer Plus

Klaus Michelsen / Sams / 2001-12-15 / USD 49.99

C# Primer Plus is a tutorial based introduction to the C# language and important parts of the .Net Framework. Throughout the book the reader will be exposed to proven principles enabling him to write ......一起来看看 《C# Primer Plus》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试