C++ Studio( 一 ) ----------- <stdlib.h> atoi() --------- 将string转换为int型

atoi()函数

    原型:int  atoi (const  char  *nptr)

    用法:#include  <stdlib.h>

    功能:将字符串转换成整型数;atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负号才开始做转换,而再遇到非数字或字符串时('\0')才结束转化,并将结果返回。

    说明:atoi()函数返回转换后的整型数。

    举例:

  

 1 #include <stdio.h>  
 2 #include <stdlib.h>  
 3   
 4 int main()  
 5 {  
 6     char a[] = "-100";  
 7     char b[] = "456";  
 8     int c = 0;  
 9       
10     c = atoi(a) + atoi(b);  
11       
12     printf("c = %d\n",c);  
13 } 

 

    结果:

posted @ 2015-09-19 18:16  silent-bobo  阅读(487)  评论(0编辑  收藏  举报