I have an assignment where I need to print an integer in C without using printf, putchar, etc. No header files allowed to be included. No function calls except for anything
One option might be to do this recursively, so the number gets printed out in the right order.
In this case, instead of a do/while loop, you'd have a construction more like this, with a base case of num=0.
if(num==0)
return;
j = num % 10;
c = j + '0';
my_int(num/10);
my_char(c);
Edit: Noticed that you aren't allowed to use recursion. It's a bit ugly, but you could check for the digits in the number, and then loop backwards across the number.
To find the number of digits,
int digitDivide = 1;
int tempNum = num;
while(tempNum>0){
tempNum= tempNum/10;
digitDivide=digitDivide*10;
}
and then use that to loop through the number as follows:
digitDivide = digitDivide/10;
while(digitDivide>0){
tempNum = (num/digitDivide)%10;
c = j + '0';
my_char(c);
digitDivide=digitDivide/10;
}