第一篇 CSDN 博文

虽然注册博客很多年了,但是一直太懒,只是潜水,只动鼠标,从来没有动过键盘。

最近本人升级了,当了父亲,故而想写点和记录点东西。

本人也是码农一个,先贴段代码供大家欣赏:

这是一个比较古老的C语言函数itoa,把一个整数转换为字符串返回:


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <fcntl.h>
#include <errno.h>
#include <math.h>
#include <poll.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/select.h>
#include <string.h>

char * itoa(int n)
{
	int i, tmp, flag;
	char str[32], t, *start, *end, *ret;

	i = 0;
	memset(str, 0, sizeof(str));

	if (n < 0) {
		flag = 1;
		n = -n;
	}

	do {
		tmp = n%10;
		n = n/10;
		str[i++] = '0' + tmp;
	} while (n > 0);

	if (flag)
		str[i++] = '-';

	ret = malloc(i+1);
	memcpy(ret, str, i+1);

	start = ret;
	end = ret + i - 1;

	while (start < end) {
		t = *start;
		*start ++ = *end;
		*end -- = t;
	}

	return ret;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值