Chinaunix首页 | 论坛 | 博客
  • 博客访问: 256145
  • 博文数量: 71
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 920
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-16 13:07
个人简介

我喜欢蓝天,也喜欢雨天。

文章分类

全部博文(71)

文章存档

2014年(14)

2013年(57)

分类: C/C++

2013-12-10 13:12:48

函数原型:
int atoi(
   const char *str 
);
int _wtoi(
   const wchar_t *str 
);
包含头文件:#include <stdlib.h>
函数参数:str:要转换的字符串。
函数返回值:成功返回转换后的数值,失败返回0.超过最大值返回LONG_MAX,小于最小值返回LONG_MIN.
示例程序:
#include "stdafx.h"
#include 
#include 

int _tmain(int argc, _TCHAR* argv[])
{
 char    *str = NULL;
 wchar_t *wstr = NULL;
 int     value = 0;

 // An example of the atoi function.
 str = "  -3536 ";
 value = atoi( str );
 printf( "Function: atoi( \"%s\" ) = %d\n", str, value );

 // Another example of the atoi function.
 str = "41412765";
 value = atoi( str );
 printf( "Function: atoi( \"%s\" ) = %d\n", str, value );

 // Another example of the atoi function 
 // with an overflow condition occuring.
 str = "5556402735171707160320";
 value = atoi( str );
 printf( "Function: atoi( \"%s\" ) = %d\n", str, value );
 if (errno == ERANGE)
 {
 printf("Overflow condition occurred.\n");
 }
 //////////////////////////////////////////
 wstr = L"  -36309 ";
 value = _wtoi( wstr );
 printf( "Function: _wtoi( \"%ws\" ) = %d\n", wstr, value );

 // Another example of the _wtoi function.
 wstr = L"124544667";
 value = _wtoi( wstr );
 printf( "Function: _wtoi( \"%ws\" ) = %d\n", wstr, value );

 // Another example of the _wtoi function 
 // with an overflow condition occuring.
 wstr = L"548556564171707160320";
 value = _wtoi( wstr );
 printf( "Function: _wtoi( \"%ws\" ) = %d\n", wstr, value );
 if (errno == ERANGE)
 {
 printf("Overflow condition occurred.\n");
 }

 getchar();
 return 0;
}
运行结果:
 
另外,字符串转长整型介绍:http://blog.chinaunix.net/uid-29121609-id-4030955.html
字符串转换为double型:http://blog.chinaunix.net/uid-29121609-id-4031108.html
                                   
阅读(2315) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~