How to implement a function that converts int number to string in C

1 Answer

0 votes
#include <stdio.h>
#include <math.h>

void int2str(int intval, char* buf) {
	int intvallen = log10(intval) + 1;
	char* const pStart = buf;
	char* const pEnd = buf + intvallen;
	
	for (; intval > 0 && (buf < pEnd); intval /= 10, buf++) {
		*buf = '0' + intval % 10;
	}

	*buf = 0;
	
	strrev(pStart);
}

int main()
{
	int n = 89014;
	char buf[16] = "";

	int2str(n, buf);

	printf("%s", buf);

	return 0;
}


/*

89014

*/

 



answered Sep 28, 2024 by avibootz

Related questions

1 answer 142 views
1 answer 148 views
1 answer 206 views
2 answers 294 views
1 answer 209 views
...