Re: Printf
From: Alex (me_at_privacy.net)
Date: 04/23/04
- Next message: Martin Ambuhl: "Re: const structs in other structs"
- Previous message: aruna: "Valid operations on pointers in C"
- In reply to: pete: "Re: Printf"
- Next in thread: pete: "Re: Printf"
- Reply: pete: "Re: Printf"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 23 Apr 2004 15:35:29 +0100
"pete" <pfiland@mindspring.com> wrote in message
news:40886568.17A1@mindspring.com...
> hello123 wrote:
> > If you have an integer(int), how to print it out without using printf?
>
> /* BEGIN integer.c */
[snip]
> int fsput_u(unsigned integer)
[snip]
> int fsput_u_plus_1(unsigned integer)
[snip]
> int fsput_d(int integer)
> {
> int count;
>
> if (0 > integer) {
> if (put_char('-') == EOF) {
> return EOF;
> }
> count = fsput_u_plus_1(-(integer + 1));
> return count == EOF ? EOF : count + 1;
> } else {
> return fsput_u(integer);
> }
> }
>
> /* END integer.c */
I guess fsput_u_plus_1() is because -INT_MIN may be greater than INT_MAX?
How about:
int fsput_u(unsigned n)
{
int count = 0;
int c = '0' + (n % 10);
n /= 10;
if (n && (count = fsput_u(n)) == EOF) return EOF;
return (put_char(c) == EOF) ? EOF : (count + 1);
}
int fsput_d(int n)
{
if (n < 0) {
int count, units;
if (put_char('-') == EOF) return EOF;
units = n % 10;
n /= 10;
if (units < 0) units = -units;
count = 0;
if (n && (count = fsput_u(-n)) == EOF) return EOF;
return (put_char('0' + units) == EOF) ? EOF : (count + 2);
}
return fsput_u(n);
}
- Next message: Martin Ambuhl: "Re: const structs in other structs"
- Previous message: aruna: "Valid operations on pointers in C"
- In reply to: pete: "Re: Printf"
- Next in thread: pete: "Re: Printf"
- Reply: pete: "Re: Printf"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|