By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,905 Members | 1,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,905 IT Pros & Developers. It's quick & easy.

Printing int without using printf()

P: n/a
How to print out an integer (int) in C language without using printf()?

Thanks in advance!
Nov 13 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Yammy Hung wrote:
How to print out an integer (int) in C language without using printf()?

Thanks in advance!

#include <stdio.h>

int
main()
{
int i = 12;
char tmp[32];
snprintf(tmp, sizeof tmp, "%d", i);
puts(tmp);
return 0;
}


Nov 13 '05 #2

P: n/a
Martin Ambuhl <ma*****@earthlink.net> wrote (09 Jul 2003) in
news:Xn*********************************@207.217.7 7.23 / comp.lang.c:
"Yammy Hung" <yh***@sunny.com> wrote (09 Jul 2003) in
news:be***********@justice.itsc.cuhk.edu.hk / comp.lang.c:
How to print out an integer (int) in C language without using
printf()?
Your question stinks of cheating by asking someone else to do your
homework, but what the hell ...


A note to Yammy Hung: don't try turning this in as is. There are
valid values of ints for which it fails. Fix that first.

#include <stdio.h>

void putint(int x);

int main(void)
{
puts("[output]");
putint(13725);
putchar('\n');
putint(5500);
putchar('\n');
return 0;
}

void putint(int x)
{
if (x) {
putint(x / 10);
putchar('0' + x % 10);
}
}

[output]
13725
5500


--
Martin Ambuhl
Returning soon to the
Fourth Largest City in America
Nov 13 '05 #3

P: n/a
Yammy Hung wrote:

How to print out an integer (int) in C language without using printf()?

#include <stdio.h>
#include <limits.h>
...
char buff[1+(CHAR_BIT*sizeof(int)+2)/3+1];
sprintf (buff, "%d", the_integer);
puts (buff);
Thanks in advance!


You're welcome.

--
Er*********@sun.com
Nov 13 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.