Page 1 of 1

printing integers to screen without using an array

Posted: Wed Feb 22, 2006 3:48 pm
by sith
Hi guys,

I want to write an unsigned integer to the screen.

I tried this :


int printUInt(unsigned int c)
{
unsigned int temp;
unsigned int value;

while(c>0)
{
temp = c;
c = c/10;
value = temp%10;
printChar(digitToChar(value));
}
return 0;
}


Of course, this prints numbers in the opposite order (ex : when I want to print "168" , it prints "861").

How can I print a number correctly, without using an array or the sprintf function ?


Thanks,

Jeroen

Re:printing integers to screen without using an array

Posted: Wed Feb 22, 2006 4:18 pm
by Kemp
There's always the recursive route:

Code: Select all

int printUInt(unsigned int c)
{
   if (c > 0)
   {
      printUInt(c / 10)
      printChar(digitToChar(c % 10));
   }
   return 0;
}
Optimised slightly to reduce code size and eliminate extra variables. (Code hacked together without testing while I'm a bit ill, so no guarantees)

Re:printing integers to screen without using an array

Posted: Wed Feb 22, 2006 10:58 pm
by zloba
Code hacked together without testing
here's a test case: try 0

p.s. why does it have return type "int", only to return 0?

Re:printing integers to screen without using an array

Posted: Thu Feb 23, 2006 12:53 am
by Neo
Standard success return code.

Re:printing integers to screen without using an array

Posted: Thu Feb 23, 2006 3:22 am
by Solar
Returning a value from a function that is always zero and never checked is a bit brain-dead...

The code below fixes the "zero problem" as well as offering output to different bases and adding a newline. Wrappers for common bases (10, 16) are easily done. Consider it PD, it'll find its way into some PDCLib extension anyway. ;-) It's tested.

Code: Select all

#include <stdio.h>

static void printInt_internal( unsigned int c, int base )
{
    static char const * const digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    if ( c > 0 )
    {
        printInt_internal( c / base, base );
        putchar( digits[c % base] );
    }
}

void printInt( unsigned int c, int base )
{
    if ( c == 0 )
    {
        puts( "0" );
    }
    else
    {
        printInt_internal( c, base );
        puts( "" );
    }
}

Re:printing integers to screen without using an array

Posted: Thu Feb 23, 2006 3:37 am
by Kemp
here's a test case: try 0
Well yeah, like I said, it wasn't tested, I simply modified the code he was already using to be recursive to illustrate the point.

Re:printing integers to screen without using an array

Posted: Mon Mar 27, 2006 8:55 am
by RetainSoftware
@solar
The code below fixes the "zero problem" as well as offering output to different bases and adding a newline. Wrappers for common bases (10, 16) are easily done. Consider it PD, it'll find its way into some PDCLib extension anyway. ;-) It's tested.
printInt(400, 40) will fail misserably ;D

guess if you'll put in in the PDClib you should at least add some asserts on < 0 and > 16 reducing the string to "0123456789ABCDEF". The maya (20-base) counting method isn't used widely anymore ;)

Re:printing integers to screen without using an array

Posted: Mon Mar 27, 2006 2:38 pm
by bubach
Solar: Why do: puts( "" ); ?

Re:printing integers to screen without using an array

Posted: Tue Mar 28, 2006 1:12 am
by Solar
RetainSoft wrote: printInt(400, 40) will fail misserably ;D

guess if you'll put in in the PDClib you should at least add some asserts on < 0 and > 16 reducing the string to "0123456789ABCDEF". The maya (20-base) counting method isn't used widely anymore ;)
Works as designed. The code is basically my strtol() function "reversed", and strtol() is required to accept bases from 2 to 36. Besides, reacting in an undefined way on undefined input (like base 40) has a long tradition in C libraries. ;)

Re:printing integers to screen without using an array

Posted: Tue Mar 28, 2006 1:15 am
by Solar
bubach wrote: Solar: Why do: puts( "" ); ?
It's the only portable way to print the platform's end-of-line character sequence. ;) C++ does this with [tt]endl[/tt]... (You know there are platforms that do other things than '\n' to indicate newlines?)

Re:printing integers to screen without using an array

Posted: Tue Mar 28, 2006 1:17 am
by bubach
Oh.. Well, my C is pretty weak.. :P

Re:printing integers to screen without using an array

Posted: Tue Mar 28, 2006 1:27 am
by Solar
Don't worry. Mine would probably not be any better if I hadn't spent hours and hours digesting the C standard document for PDCLib. It teaches you to look at the fine print...