C Programming - Bitwise Operators - Discussion

3. 

Which of the following statements are correct about the program?

#include<stdio.h>
char *fun(unsigned int num, int base);

int main()
{
    char *s;
    s=fun(128, 2);
    s=fun(128, 16);
    printf("%s\n",s);
    return 0;
}
char *fun(unsigned int num, int base)
{
    static char buff[33];
    char *ptr = &buff[sizeof(buff)-1];
    *ptr = '\0';
    do
    {
        *--ptr = "0123456789abcdef"[num %base];
        num /=base;
    }while(num!=0);
    return ptr;
}

[A]. It converts a number to a given base.
[B]. It converts a number to its equivalent binary.
[C]. It converts a number to its equivalent hexadecimal.
[D]. It converts a number to its equivalent octal.

Answer: Option A

Explanation:

No answer description available for this question.

Shyamala said: (Jun 7, 2011)  
Please give explanation for this program. Me too a basic c learner.

Prudhvi said: (Jul 17, 2011)  
Please guys any one let me know this explanation very soon.

Needprayers4Me. said: (Aug 3, 2011)  
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: (Sep 5, 2011)  
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.

Hey said: (Feb 27, 2012)  
What does "0123456789abcdef" do here?

Dinesh said: (May 12, 2012)  
Why we need char to be defined as static?

Uma said: (Aug 24, 2012)  
Please give breaf explanation about this program.

Saurabh said: (Sep 17, 2012)  
0123456789abcdef is used to pick values for example binary conversion when we are calling fun(128,2)
here it is continuously dividing 128 with 2 and each time we are getting remainder equal to 0 because no. is 128 see step
*--ptr = "0123456789abcdef"[num %base]; this statement is in
the loop till the value of num is not equal to zero
Hence while call to the function fun(128,2) ptr is storing 00000001 and output in reverse.

Similarly while calling fun(128,16)
now we know representation in hexadecimal no. are like 0123456789abcdef
and it will pick one of the no. according to the remainder it get for example
fun(128,16) 128/16 fully divisible remainder 0 and quotient is 8
and in the next loop iteration 8/16 remainder is 8 so 08 and output in reverse order 80
cheers..

Cloud said: (Oct 31, 2012)  
Why we use 0123456789abcdef?

Ketan said: (Aug 3, 2013)  
Why specific size 33 of buffer?

Shubham said: (Aug 20, 2013)  
Understanding "0123456789abcdef"[num%base].

Think of 0123....ef as if char str[]="0123456789abcdef"

Now it becomes as:

str[num%base] .... I think rest is self explanatory!

Machindra Mohate said: (Jun 6, 2015)  
Because base is 16 for hexadecimal. Its range starting from 0 to f or 0 to F.

To express it we use 0123456789abcdef.

Zara said: (Feb 15, 2016)  
When we run this program the output is 80. Can one please explain how?

Emanuel said: (Sep 29, 2016)  
I don't think that it is possible to use "12345".

Chandu said: (Feb 10, 2017)  
I don't understand this problem please explain clearly.

Kalyan said: (May 24, 2017)  
I am not getting this, Can anyone explain it clearly?

Mounika said: (Jun 14, 2017)  
Consider num=128 and base=2
in first iteration
*--ptr = "0123456789abcdef"[128 %2]; which is equals to "0123456789abcdef"[0];.
It means ptr stores 0th index of the string "0123456789abcdef"
num/=base; here num = 64.

Next iterations, num equals to 64,32,16,8,4,2 and for all these num values , 0th index of the string will be stored.
when num = 1 base =2.
*--ptr = "0123456789abcdef"[1 %2]; which is equals to "0123456789abcdef"[1];.

Now ptr stores 1st index of string "0123456789abcdef".
Finally, ptr contains 10000000 (due to *--ptr digits are stored from the right towards left)
(10000000)base2 = 128.
Similarly for 128,16 returns 80.

Newb said: (Jul 5, 2018)  
main() assigns s twice.

Last value, i.e. a hexadecimal representation of 128, will be printed. I believe this is graded wrong.

Manjushree said: (Feb 27, 2022)  
Give an explanation for the given program.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.