Please give explanation for this program. Me too a basic c learner.
Prudhvi said:
(Sun, Jul 17, 2011 07:51:45 AM)
Please guys any one let me know this explanation very soon.
Needprayers4Me. said:
(Wed, Aug 3, 2011 11:30:50 PM)
char *ptr = &buff[sizeof(buff)-1]; makes ptr point to 32nd index of buff.
buff[32] is made null in the next statement. (*ptr='\0'). Reason being strings are supposed to be terminated with null.
Now considering 128,2
128%2=0 so "0123456789abcdef"[num %base]; returns 0. ptr is decremented and then the value 0 is assigned. This value 0 is written on to the next six places from the right towards left till num becomes 1. Now when num becomes 1, "0123456789abcdef"[num %base] returns 1. and hence the binary rep of 128.
Similar analysis will lead one to see that with 128,16 the value returned is 80, the hexadecimal rep pf 128.
Saurav said:
(Mon, Sep 5, 2011 02:34:42 AM)
The question asked is based on the function and not the main program's output.....
For the given program it will print the binaray and hexadecimal equivalent of 128.