Re: Printf

From: Alex (me_at_privacy.net)
Date: 04/23/04


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);
}



Relevant Pages

  • Re: Haldanes Dilemma - clarifications - and Felsenstein [LONG]
    ... This sounds like a consequence of the theory of 'nearly neutral' selection. ... We take two at random, and return the one with the highest fitness, ... int choose ...
    (sci.bio.evolution)
  • Re: Confused to printf %f
    ... Someone asked me a question about integer division and printf ... You gave it an int. ...
    (comp.lang.c)
  • Re: c interview
    ... undefined behaviour because printf is a varidac function. ... stdio.h before using printf and other headers as appropriate before ... Would make it a point that the size of int is assumption to be .... ...
    (comp.lang.c)
  • Re: Union Issue
    ... pete wrote: ... snip ... ... >> union data_type { ... >> int main ...
    (comp.lang.c)
  • Re: print binary
    ... pete wrote: ... snip ... ... int clearln{ ... Chuck F (cbfalconer at maineline dot net) ...
    (comp.lang.c)