Printing To Screen: Difference between revisions

→‎Printing Integers: Added C++ function
[unchecked revision][unchecked revision]
(→‎Printing Integers: Added C++ function)
Line 58:
 
digits to be displayed are '1','2','3','4' ... if you know the numerical value of number%10, you simply have to add this to the character '0' to have the correct character (e.g. '0'+4 == '4')
 
Here is some sample code:
<source lang="c">
void printint(int val) //Change the declaration to suit you
{
char tmp[9] = {0};
int temp = val;
int n = 1;
while(temp != 0)
{
int intChar = temp % 10; //Set character to least sig bit
tmp[9-n] = (char)(intChar+0x30); //ASCII offset for digits
temp /= 10;
n++;
}
n--; //Algorithm will leave n pointing to a 0
while(n!=0)
{
putc(tmp[9-n]);
n--;
}
}
</source>
 
(see more on [http://www.osdev.org/phpBB2/viewtopic.php?t=10319 the forum].)
Anonymous user