730 views

2 Answers

Best answer
3 votes
3 votes

(*s++ - '0') this will subtract ascii value of stored character with ascii value of 0 and will give numeric (decimal ) value of stored character during each iteration,

example: ascii value of 0 and 1 is 48 and 49 , hence during iteration 1:

(*s++ - '0')=s[0]-'0'='0'-'0'=48-48=0(decimal value)

now evaluation of ur program is as given below : fun("000001010")

iteration r<<1 (*s++-'0')=value stored at s[i] r = r<<1|(*s++ - '0')
1 0 0 0
2 0 0 0
3 0 0 0
4 0 0 0
5 0 0 0
6 0 1 1
7 2 0 2
8 4 1 5
9 10 0 10
10   \n(ascii value 0) loop fail

therefore 10 will be return at the end

selected by
1 votes
1 votes
r   s

0  0                                                                                                                                                                            

0  0                                                                                                                                                                            

0  0                                                                                                                                                                            

0  0                                                                                                                                                                            

0  0                                                                                                                                                                            

0  1                                                                                                                                                                            

1  0                                                                                                                                                                            

2  1                                                                                                                                                                            

5  0     

at last r = r*2 = 10

 

return value will be 10

Related questions

0 votes
0 votes
1 answer
1
Debargha Mitra Roy asked 1 day ago
35 views
#include <stdio.h int main() { int a[3] = {1, 3, 5, 7, 9, 11}; int *ptr = a[0]; ptr += sizeof(int); printf("%d", *ptr); return 0; }(Assume size of int to be $2$ bytes.)T...
0 votes
0 votes
2 answers
3
Debargha Mitra Roy asked Apr 10
97 views
What is the output of the below code?#include <stdio.h void main() { static int var = 5; printf("%d ", var ); if (var) main(); }a. 1 2 3 4 5b. 1c. 5 4 3 2 1d. Error
1 votes
1 votes
1 answer
4
SSR17 asked Feb 29
251 views
#include <stdio.h int main() { int i = -1; int x = (unsigned char)i; printf("%d", x); return 0; }output is 255 , but please explain how