Printing int without using printf()

Y

Yammy Hung

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

Thanks in advance!
 
M

Martin Ambuhl

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
 
J

Jeff

Yammy said:
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;
}
 
E

Eric Sosman

Yammy said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top