C Programming - Bitwise Operators - Discussion
Discussion Forum : Bitwise Operators - Point Out Correct Statements (Q.No. 3)
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;
}
Discussion:
19 comments Page 2 of 2.
Shyamala said:
1 decade ago
Please give explanation for this program. Me too a basic c learner.
Cloud said:
1 decade ago
Why we use 0123456789abcdef?
Saurabh said:
1 decade ago
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..
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..
Uma said:
1 decade ago
Please give breaf explanation about this program.
Dinesh said:
1 decade ago
Why we need char to be defined as static?
Hey said:
1 decade ago
What does "0123456789abcdef" do here?
Saurav said:
1 decade ago
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.
For the given program it will print the binaray and hexadecimal equivalent of 128.
NeedPrayers4Me. said:
1 decade ago
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.
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.
Prudhvi said:
1 decade ago
Please guys any one let me know this explanation very soon.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers