Friday 15 July 2011

c - Confusion about the output of this program -


I am new to C programming and I am currently learning to modify data types. In the program below, I have O / P 36, but the compiler is showing O / P 35.

  main () {char ch = 291; Printf ("\ n% d% c", ch, ch); }   

Can anyone explain to me why the O / P 35 is coming? I am currently using the GCC 32-bit compiler.

You are actually creating an overflow with a signed character values ​​only in 8-bit character systems-128 From 127 (256 values ​​= 2 8 ) (which is very much everywhere). So we go with a real character which is equal to 291% 256 = 35.

Do not forget that the first letter is 0, not 1.

is presented with:

  unsigned 0 ------- 127 128 ------- 255 signed 0 ------ - 127 -128---- --- -1   

So in fact a signed letter c1 = -128 is equal to a unsigned char C2 = 128

But this problem here is just irrelevant. We are talking about the module because only the last eight bits are kept in mind (where the second will be stored when memory Only eight bills for this Will be available?).

  291 =% 1 0010 0011   

(% means binary representation)

This Only % 0010 0011 holds 35 and which will be considered as the same or will you sign it or not.

No comments:

Post a Comment