The result of an expression involving anything int-sized or smaller is always an int
nick woodward wrote:It seems obvious, but that means either operand, right?
nick woodward wrote:is there a good thread here that summarizes all the 'rules' for assignment?
=nick woodward- integer literals are ints.
nick woodward wrote:- for longs i assume ^ this is still the case, but implicit widening occurs
nick woodward wrote:- for anything smaller, if the literal is in the variable's scope then implicit narrowing occurs, otherwise you need to cast.
nick woodward wrote:- if you're assigning an expression (where the variable is less than an int) then you have to cast to that type, unless its a compound assignment, in which case it's implicit.
- in addition, i *think* that the last point doesn't apply if the expression operands are compile time constants, or in scope literals themselves.
Roel De Nijs wrote:
Hope it helps!
Kind regards,
Roel
nick woodward wrote:for once i'm actually surprised it doesn't work the way i assumed!
Roel De Nijs wrote:
nick woodward wrote:for once i'm actually surprised it doesn't work the way i assumed!
And what was a surprise for you? Because maybe there's a simple explanation for which makes it easier to understand.
nick woodward wrote:so to assign a literal (integer) to a long you have to use 'L'?
Roel De Nijs wrote:
nick woodward wrote:is there a good thread here that summarizes all the 'rules' for assignment?
Not that I'm aware of because it's all pretty straightforward.
Roel De Nijs wrote:
Roel De Nijs wrote:
nick woodward wrote:is there a good thread here that summarizes all the 'rules' for assignment?
Not that I'm aware of because it's all pretty straightforward.
Today I vaguely remembered a topic about widening, conversion, boxing,... of primitive data types like float and long. So using the forum's search engine, I found that topic. It's definitely worth a read and probably the closest you'll find here on CodeRanch to what you are looking for. And this one is about a long fitting in a float.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |