Thursday 18 April 2013

char data type cycle


What will be output if you will compile and execute the following c code?

void main()
{
char c=125;
    c=c+10;
    printf("%d",c);
}


(A) 135

(B) +INF

(C) -121

(D) -8

(E) Compiler error

________________________________________
Explanation:
As we know char data type shows cyclic properties i.e. if you will increase or decrease the char variables beyond its maximum or minimum value respectively it will repeat same value according to following cyclic order:

So,
125+1= 126
125+2= 127
125+3=-128
125+4=-127
125+5=-126
125+6=-125
125+7=-124
125+8=-123
125+9=-122
125+10=-121

1 comment:

  1. pls describe first two steps action of value out of range(range of signed char).

    ReplyDelete