类型转换之字符串转int型

来源:互联网 发布:青岛企业网站seo技巧 编辑:程序博客网 时间:2024/05/22 05:01
函数原型:
int atoi(
   const char *str );int _wtoi(   const wchar_t *str );
包含头文件:#include <stdlib.h>
函数参数:str:要转换的字符串。
函数返回值:成功返回转换后的数值,失败返回0.超过最大值返回LONG_MAX,小于最小值返回LONG_MIN.
示例程序:
#include "stdafx.h"#include <stdlib.h>#include <errno.h>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
           
阅读(50) | 评论(0) | 转发(0) |
0

上一篇:类型转换之字符串转换为长整型

下一篇:类型转换之字符串转double型

相关热门文章
  • 用Wrod功能实现任意纵横...
  • 即依法治国
  • 这就是法律规范法律关系实现权...
  • Linux进程间通信——使用消息...
  • web.py 学习 20140211
  • test123
  • 编写安全代码——小心有符号数...
  • 使用openssl api进行加密解密...
  • 一段自己打印自己的c程序...
  • sql relay的c++接口
  • 一个简单的shell脚本问题...
  • 网站如何做图片的防盗链功能呢...
  • 如何将printf输出的字符(含有...
  • 嵌入式linux wifi移植 libert...
  • Ø ⊆ {Ø} 是否是对的 ,这么...
给主人留下些什么吧!~~
原创粉丝点击