printing integers to screen without using an array
printing integers to screen without using an array
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
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
There's always the recursive route:
Optimised slightly to reduce code size and eliminate extra variables. (Code hacked together without testing while I'm a bit ill, so no guarantees)
Code: Select all
int printUInt(unsigned int c)
{
if (c > 0)
{
printUInt(c / 10)
printChar(digitToChar(c % 10));
}
return 0;
}
Re:printing integers to screen without using an array
here's a test case: try 0Code hacked together without testing
p.s. why does it have return type "int", only to return 0?
Re:printing integers to screen without using an array
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.
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.
Re:printing integers to screen without using an array
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.here's a test case: try 0
Re:printing integers to screen without using an array
@solar
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
printInt(400, 40) will fail misserably ;DThe 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.
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
Solar: Why do: puts( "" ); ?
Re:printing integers to screen without using an array
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.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
Every good solution is obvious once you've found it.
Re:printing integers to screen without using an array
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?)bubach wrote: Solar: Why do: puts( "" ); ?
Every good solution is obvious once you've found it.
Re:printing integers to screen without using an array
Oh.. Well, my C is pretty weak..
Re:printing integers to screen without using an array
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.