printing integers to screen without using an array

Programming, for all ages and all languages.
Post Reply
sith

printing integers to screen without using an array

Post 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
Kemp

Re:printing integers to screen without using an array

Post 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)
zloba

Re:printing integers to screen without using an array

Post 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?
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:printing integers to screen without using an array

Post by Neo »

Standard success return code.
Only Human
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:printing integers to screen without using an array

Post 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( "" );
    }
}
Every good solution is obvious once you've found it.
Kemp

Re:printing integers to screen without using an array

Post 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.
RetainSoftware

Re:printing integers to screen without using an array

Post 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 ;)
User avatar
bubach
Member
Member
Posts: 1223
Joined: Sat Oct 23, 2004 11:00 pm
Location: Sweden
Contact:

Re:printing integers to screen without using an array

Post by bubach »

Solar: Why do: puts( "" ); ?
"Simplicity is the ultimate sophistication."
http://bos.asmhackers.net/ - GitHub
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:printing integers to screen without using an array

Post 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. ;)
Every good solution is obvious once you've found it.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:printing integers to screen without using an array

Post 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?)
Every good solution is obvious once you've found it.
User avatar
bubach
Member
Member
Posts: 1223
Joined: Sat Oct 23, 2004 11:00 pm
Location: Sweden
Contact:

Re:printing integers to screen without using an array

Post by bubach »

Oh.. Well, my C is pretty weak.. :P
"Simplicity is the ultimate sophistication."
http://bos.asmhackers.net/ - GitHub
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:printing integers to screen without using an array

Post 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...
Every good solution is obvious once you've found it.
Post Reply