c - short type variable automatically extended to integer type? -


I want to print the value of [FFFC] like below,

  short Var = 0xFFFC; Printf ("% d \ n", b [var]);  

But it actually prints the value of B [FFFF FFFC].

Why does this happen?

In my computer windows XP 32-bit architecture.

small is a signed type. This represents 16 bits on your implementation 0xFFFC integer represents 65,532 continuously, but when 16 bit is converted to signed value, then the result is 4.

Then, your line short var = 0xFFFC; Sets var-4 (on your implementation).

There is 32 bit representation of 0xFFFFFFFC-4, whatever is happening is that your value is being converted from one type to a large extent, so that it can be used as an array index .

If you really want to use the 65,533th element of your array, then you should either:

  • Use a bigger type var < For 32 bit Windows / code>, int will be sufficient, but in general size_t is an unsigned type that is a great guarantee for non-negative array index.
  • Use a unsigned short , which simply gives enough space for this example, but if you want to take another 4 steps ahead Then it will be wrong.

Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

php - jQuery AJAX Post not working -