整数转化为字符串和字符串转化为整数
来源:互联网 发布:淘宝运营靠谱吗 编辑:程序博客网 时间:2024/05/01 11:07
//整数转化为字符串#include<stdio.h>int main(){ int num=12345,i=0,j=0; char temp[10],str[10]; while(num) { temp[i] = num%10 + '0'; i++; num/=10; } temp[i] = 0; //为什么等于0就可以,因为将temp定义为字符串数组后,等号右边的数相当于ASCII码值,0就相当于'/0'。 //如果写为61,输出就为 12345= //printf("%s",temp); i = i-1; while(i>=0) str[j++] = temp[i--]; str[j] = 0; printf("%s",str); getchar(); return 0;}
//字符串转化为整数#include<stdio.h>int main(){char temp[] = { '1', '2' ,'3','4','5','\0'};//printf("%d",sizeof(temp));//printf("%s",temp);char str[10];int sum=0,i=0;int num;while(temp[i]){num = (temp[i]-'0');sum = sum*10 + num; i++; }printf("%d",sum);getchar();return 0;}