字符串转整数 —— atoi() 函数

头文件:<stdlib.h>

函 数: int atoi( const char *str )

功 能: 把一个数字字符串 str 转化为一个整型数(int型)

atoi() 会扫描 str ,跳过前面的空格,直到遇见数字或正负号开始转换,再遇到非数字或字符串(’ \0 ')时,结束转换,返回结果。

示例:

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

int main(){
	char str[20];
	
	strcpy(str, "123456789");  //字符串赋值
	
	int temp = atoi(str);  //转化为整型数
	
	printf("%d", temp);  //整型格式输出
	
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章