1,816 views
0 votes
0 votes

Consider a hypothetical machine which supports the following data types:

unsigned char :1 Byte
unsigned short : 2 Bytes
int :4 Bytes

Consider the following function red()


int red (unsigned char a, unsigned short b)
{
  if (a==0) return b;
  else
      {
      a=a+1;
      b=b*2;
      return red(a,b);}

}
int main(){
     printf("%d",red(char(240),1));
     return 0;
}

What will be the output of the following program?

 

A The program terminates abnormally

B The program goes into infinite loop

C The program outputs 65536 (216)

D None of these

Answer is given as D.

That is fine but my question is that what will be the output of the program.

Because according to me it should be 32768, but in madeeasy solution it is given as output 0.

1 Answer

0 votes
0 votes
So function starts from

red (char(240),1))

UNSIGNED CHAR = 1 BYTE WHICH MEANS 0-255 RANGE

and it goes to above loop and a is incrementing every time . i am not taking care of b here because i am not interested in this.

SO NOW  a increment and a=241

a =242

a=243

..........................................   a=256

suppose when a=256 b is something p

so red (256,p)

AS UNSIGNED CHAR = 1 BYTE MEANS 8 BITS RANGE 0 -255

SO WHEN WE HAVE 256 WE HAVE 255 MAX

SO BITS 0  ALL IN ALL 8 BITS AND 1 BIT REMAINING CANT BE IN THIS RANGE SO WE HAVE ALL ZEROES THAT IS WHY IT IS ZERO

Related questions

0 votes
0 votes
1 answer
1
0 votes
0 votes
1 answer
2
tishhaagrawal asked Dec 16, 2023
378 views
Below is my approach to solving this question, can anyone please explain if I am doing it the right way?Let X = #free slotssince, m =7 and n = 3So, $4 \leqslant x\leqsla...
0 votes
0 votes
1 answer
4
jugnu1337 asked Oct 22, 2023
356 views
Suppose A is a 12 by 9 incidence matrix from a connected (but unknown) graph with 9 nodes and 12 edges. The diagonal entries of $A^{T}.A$give the number of edges into eac...