1

Can anybody please clarify the below:

byte a = 10 + 20; // b=30;
-------------
byte b=10,c=20;
byte a = b + c; //error. Casting required.

What was the data type of result of addition in the first line before assigning it to 'a'? Do literals have a data type. Or do arithmetic result assign them one ?

pragun
  • 471
  • 1
  • 6
  • 9
  • read also this https://stackoverflow.com/questions/13100019/why-can-not-i-add-two-bytes-and-get-an-int-and-i-can-add-two-final-bytes-get-a-b – Youcef LAIDANI Jun 08 '17 at 11:45

1 Answers1

1

since b + c is an operation that can overflow you need to do

byte a = (byte)(b + c);
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97