5

Written a function with unsigned char arguments as shown below

void Address_set(unsigned char x1,unsigned char y1,unsigned char x2,unsigned char y2)

The above function is called in main() function of C Code as

Address_set(0,0,239,319);

I received a warning as

large integer implicitly truncated to unsigned type [-Woverflow]

How to avoid this warning.

5
  • 5
    By not using larger values than the types can handle. Sep 28, 2016 at 5:09
  • Either change (unsigned char y2) to int, or avoid using a number that's greater than max value of unsigned char
    – Jun Yuan
    Sep 28, 2016 at 5:15
  • What did you expect, trying to convert 319 to an unsigned char?
    – Jason C
    Sep 28, 2016 at 5:16
  • Since we are dealing with unsigned chars, the value would be "wrapped".
    – babon
    Sep 28, 2016 at 5:22
  • Then what will be the possible method to pass 319 to that function?
    – Sha
    Sep 28, 2016 at 5:22

1 Answer 1

2

The range of unsigned char is [0 ... 255], the value 319 is truncated (wrap-around) to 319 % 256 = 63

How to avoid this warning.

The value is still truncated even if you can avoid this warning using a cast:

Address_set(0,0,239,(unsigned char)319);

Use a longer type, i.e: unsigned short

2
  • 1
    Or pass a smaller value (e.g. 255 instead of 319). Sep 28, 2016 at 5:30
  • After changing it to unsigned short. It looks fine. void Address_set(unsigned short x1,unsigned short y1,unsigned short x2,unsigned short y2)
    – Sha
    Sep 28, 2016 at 9:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.