Thread: itoa explanation?

  1. #1
    Registered User xion's Avatar
    Join Date
    Jul 2003
    Posts
    63

    itoa explanation?

    im having trouble understanding the code in itoa's do while loop. why is this finding the remainder of dividing n by 10 and adding '0'? and the condition while((n/=10)!=0). whats happening here?
    Code:
    /* reverse:	reverse string s in place */
    void reverse(char s[])
    {
    	int c,i,j;
    
    	for(i = 0,j = strlen(s)-1; i < j; i++,j--)
    	{
    		c = s[i];
    		s[i] = s[j];
    		s[j] = c;
    	}
    }
    
    /* itoa:  convert n to characters in s */
    void itoa(int n,char s[])
    {
    	int i,sign;
    
    	if((sign = n) < 0)	/* record sign */
    		n = -n;			/* make n positive */
    
    	i = 0;
    
    	do {						/* generate digits in reverse order */
    		s[i++] = n % 10 + '0'; 	/* get next digit */
    	} while((n /= 10) > 0);		/* delete it */
    
    	if(sign < 0)
    		s[i++] = '-';
    	s[i] = '\0';
    	reverse(s);
    }
    int main()
    {
    	char string[10];
    	int x;
    
    	x = 30;
    	itoa(x,string);
    	printf("%s\n",string);
    	return 0;
    }
    Last edited by xion; 06-03-2004 at 04:14 PM.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    It is getting the current digit in the tens place and then adding '0' to it to get it's ascii character value. Then divide by 10 and repeat.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your numbers are ordered like so in the ASCII set:
    Code:
    '0', '1', '2' ... '9'
    Thus, if you have an integer, and you add the ASCII value of '0' to it, it will move numericly down the line, giving you the corresponding ASCII value.

    Example: You want '3'. You start with ASCII '0' and add a decimal 3 to it. This gives you:
    Code:
    '0' + 0 = '0'
    '0' + 1 = '1'
    '0' + 2 = '2'
    '0' + 3 = '3'
    Take a number, and keep dividing it by ten. Eventually, your result will be zero, and you'll have run out of number.
    Code:
    111 / 10 = 11
    11 / 10 = 1
    1 / 10 = 0
    When you run out of number, you're done, so you stop looping then.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User xion's Avatar
    Join Date
    Jul 2003
    Posts
    63
    thanks for the responses but still somewhat puzzled.
    i edited my first post and added a main function where itoa's n = 30. given that n has the value of 30 im trying to step through the code but it doesnt make sense to me.
    Code:
    n = 30
    first:
        (n % 10) + '0' = 0    /* s[i++] val */
        n /= 10 = 3
    second:
        (n % 10) + '0' = 0 ?    /* n = 3 now */
        n /= 10 = 0
        break from loop

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
     (n % 10) + '0' = 0 ?    /* n = 3 now */
    You're mixing up division with modulus. This gives you the remainder.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User xion's Avatar
    Join Date
    Jul 2003
    Posts
    63
    so what will
    (3 % 10) + '0' = ?

    0 right? it doesnt even divide let alone have a remainder.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Did you even try it?
    Code:
    printf("3 %% 10 is %d", (3%10) );
    Don't you remember basic math? Given a number N, divided by a bigger number, you get 0 remainder N. Surely you've done long division before?

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem using itoa
    By g_p in forum C Programming
    Replies: 2
    Last Post: 05-03-2008, 06:38 AM
  2. Linked Lists
    By Bleu_Cheese in forum C++ Programming
    Replies: 13
    Last Post: 12-21-2007, 09:17 PM
  3. Problem with itoa() perhaps?
    By TheSquid in forum C++ Programming
    Replies: 5
    Last Post: 05-08-2006, 02:04 AM
  4. Really Weird itoa Problem
    By Grantyt3 in forum C++ Programming
    Replies: 8
    Last Post: 12-20-2005, 12:44 AM
  5. itoa
    By coldcoyote in forum Linux Programming
    Replies: 4
    Last Post: 02-13-2003, 09:28 AM