将一个数字字符串转换为一个整数

[复制链接]
查看11 | 回复3 | 2017-10-14 10:32:07 | 显示全部楼层 |阅读模式
#include#includelong fun(char *p) //返回值类型没有给你变{long temp;
//你的返回要long型的,所以定义个long型的temp=atol(p);
//atol是string.h中包含的一个方法,把字符串转成longreturn temp;
//返回temp}main(){char s[6]; long n; printf("Enter a string:\n"); gets(s); n=fun(s); printf("%1d\n",n);}...
回复

使用道具 举报

千问 | 2017-10-14 10:32:07 | 显示全部楼层
#includelongfun(char*s) {longx=0;//初始化x while(*s)x=x*10+*s++-48;//如果s没有结束就取当前的s+原先的x*10; returnx;//返回最后的结果 }intmain(){longn=fun("3100...
回复

使用道具 举报

千问 | 2017-10-14 10:32:07 | 显示全部楼层
#include#include "stdlib.h"int fun(char *p){return atoi(p);}main(){char s[6];printf("Enter a string:\n"); gets(s); int n=fun(s); printf("%d\n...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行