Help understanding the function of this program?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  #include <stdio.h>

#define number 12266  
#define number2 4074  
#define number3 16    
#define number4 10   

char                  
trans[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
main()
{ 
   printf("%d base 10 in base 16 is:\n",number);
   conv(number);        
   printf("\n");
   printf("%d base 10 in base 16 is:\n",number2);
   conv(number2);       
   printf("\n");
   printf("%d base 10 in base 16 is:\n",number3);
   conv(number3);       
   printf("\n");
   printf("%d base 10 in base 16 is:\n",number4);
   conv(number4);      
   printf("\n");
}


conv(n)     
int n;     
{
   if (n > 0)  
   {
      if ((n / 16) > 0) 
         conv(n/16);   
                       
      printf(" %c ",trans[n%16]);

    system("PAUSE");     
   }

}
  

Having trouble on understanding how and why the function select these specific characters from the array.
12266 base 10 in base 16 is: 2 F E A


Much thanks

Last edited on
It is using a recursion which does the work on the way back.
conv(12266)
	conv(766)
		conv(47)
			conv(2)
				conv(0)
				2%16 = 2
				trans[2] = '2'
				prints '2'
			47%16=15
			trans[15]='F'
			prints 'F'
		766%16=14
		trans[14]='E'
		prints 'E'
	12266%16=10
	trans[10]='A'
	prints 'A'
ahhh I see. Thank you very much
Topic archived. No new replies allowed.