5

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 I wrote. I have one function my_char I am using (maybe its wrong) but it prints out a character. I currently have the following code which is printing the number out backwards. Not looking for an answer. Just looking for some direction, some help, maybe I'm looking at it completely wrong.

void my_int(int num)
{   
  unsigned int i;    
  unsigned int j;

  char c;

  if (num < 0)    
    {
      my_char('-');
      num = -num;
    }

  do    
    {
      j = num % 10;
      c = j + '0';
      my_char(c);
      num = num/10;
    }while(num >0);
}
Mat
  • 202,337
  • 40
  • 393
  • 406
CLZ828
  • 53
  • 1
  • 6
  • 1
    One possibility is to use recursion. Also note that your code will fail if `num == INT_MIN` (at least on your typical 2's complement machine). – Jerry Coffin Sep 09 '12 at 22:53
  • Forgot to mention, I'm sorry, not allowed to use recursion either (i know, frustrating) – CLZ828 Sep 09 '12 at 22:54
  • If literally no header files are allowed to be included, you can't print it to stdout - even the 'write' syscall (which is to be accessed using inline assembly) is declared in a header file... –  Sep 09 '12 at 22:55
  • Maybe http://stackoverflow.com/questions/468262/how-will-you-print-a-character-without-library-functions-in-c could help. :) –  Sep 09 '12 at 22:56
  • 2
    @user1658816 Can you use a text editor and a compiler or are you supposed to do this by magnetizing your hard drive directly with a needle? –  Sep 09 '12 at 22:56
  • the my_char function has the write system call included, but thats all I am allowed to call. inside this function and file I'm not allowed to call anything else. – CLZ828 Sep 09 '12 at 23:04
  • Also, I cannot malloc anything or declare any arrays. – CLZ828 Sep 09 '12 at 23:05
  • @user1658816 you're restricted from fundamental elements of the language. No arrays? No recursion? Seriously... What is this brainless task? –  Sep 09 '12 at 23:10
  • we are essentially writing our own version of printf, our own library. I don't make the rules. My professor does. – CLZ828 Sep 09 '12 at 23:12
  • @user1658816 I see, but still... Even if you're implementing printf, what's the point in not being able to use an array? Then how on Earth you store the result? Your prof must be a very stubborn and cruel man. –  Sep 09 '12 at 23:13
  • I know. Its very frustrating. I've been working on this for a few days. and I can't figure it out anyway without using one of the restrictions. – CLZ828 Sep 09 '12 at 23:16
  • I think you could find a good explanation right here: http://stackoverflow.com/questions/5528389/printing-a-number-without-using-printf – user1647551 Sep 09 '12 at 23:18
  • @user1647551 I can't use anything there. The answers there involve all things that are limitations on my assignment. – CLZ828 Sep 09 '12 at 23:21
  • How about you add all these other restrictions you've mentioned in the comments to the actual question!? Without knowledge of and access to the physical display hardware, you will need at least a function to display a character. – Clifford Apr 23 '13 at 19:08

3 Answers3

5

Instead of calling my_char() in the loop instead "print" the chars to a buffer and then loop through the buffer in reverse to print it out.

Turns out you can't use arrays. In which case you can figure out the max power of 10 (ie log10) with the loop. Then use this to work backwards from the first digit.

unsigned int findMaxPowOf10(unsigned int num) {
    unsigned int rval = 1;
    while(num) {
        rval *= 10;
        num /= 10;
    }
    return rval;
}

unsigned int pow10 = findMaxPowOf10(num);

while(pow10) {
    unsigned int digit = num / pow10;
    my_char(digit + '0');
    num -= digit * pow10;
    pow10 /= 10;
}
MD XF
  • 7,860
  • 7
  • 40
  • 71
James
  • 1,341
  • 7
  • 13
3

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;
}
Lincoded
  • 425
  • 2
  • 13
0

You can convert an int to char * , char * and display this char* :

char        *put_int(int nb)
{
 char       *str;

 str = malloc(sizeof(char) * 4);
 if (str == NULL)
   return (0);
 str[0] = (nb / 100) + '0';
 str[1] = ((nb - ((nb / 100 * 100 )) / 10) + '0');
 str[2] = ((nb % 10) + '0');
 return (str);
}

void        put_str(char *str)
{
 while (*str)
   write(1, str++,1);
}

int main(void)
{
 put_str(put_int(42));
 return (0);
}