beginner, simple c question.
-
int i=-20; unsigned j=10; i+j=? And why? I am a beginner to C. Thanks for reply first.
-
int i=-20; unsigned j=10; i+j=? And why? I am a beginner to C. Thanks for reply first.
-
int i=-20; unsigned j=10; i+j=? And why? I am a beginner to C. Thanks for reply first.
I guess this should be correct: i + j = -20 + 10 = -10
-
int i=-20; unsigned j=10; i+j=? And why? I am a beginner to C. Thanks for reply first.
antionette wrote:
int i=-20; unsigned j=10;
Fine.
antionette wrote:
i+j=?
The above doesn't compile.
antionette wrote:
And why?
Because it is not a valid
C
statement.If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
int i=-20; unsigned j=10; i+j=? And why? I am a beginner to C. Thanks for reply first.
antionette wrote:
i+j=?
that's actually a tricky question. there are a lot of (really strange and non-intuitive, IMO) rules about how mixed signed/unsigned math is done in C. and a lot of those rules depend on how your compiler interprets the standard, and which standard. i've found that whenever i'm doing signed/unsigned arithmetic, it's best to cast everything to a signed value, then perform the arithmetic. if the result needs to be unsigned, then i range-check the value to make sure it's non-negative and small enough to fit the result. in this case, the answer is a huge positive integer.
int i = -20; unsigned j = 10; \_\_int64 k = i + j;
k = 4294967286
image processing toolkits | batch image processing
modified on Friday, January 15, 2010 11:22 AM
-
What has this to do with C? This is simple mathematics, and if you find this hard to understand, I doubt that you will find programming too easy. I would suggest you get a copy of a good C or C++ book and read it thoroughly.
MVP 2010 - are they mad?
Richard MacCutchan wrote:
This is simple mathematics
it really isn't. try it in a compiler.
-
I guess this should be correct: i + j = -20 + 10 = -10
-
Richard MacCutchan wrote:
This is simple mathematics
it really isn't. try it in a compiler.
Chris Losinger wrote:
try it in a compiler.
int i = -20; unsigned int j = 10; int k = i + j;
answer is -10. However if I just print the value (i + j) it returns 4294967286 obviously an unsigned integer, so some interesting automatic casting going on. I stand corrected! [edit]the answer is -10 of course![/edit]
MVP 2010 - are they mad?
modified on Friday, January 15, 2010 12:50 PM
-
antionette wrote:
i+j=?
that's actually a tricky question. there are a lot of (really strange and non-intuitive, IMO) rules about how mixed signed/unsigned math is done in C. and a lot of those rules depend on how your compiler interprets the standard, and which standard. i've found that whenever i'm doing signed/unsigned arithmetic, it's best to cast everything to a signed value, then perform the arithmetic. if the result needs to be unsigned, then i range-check the value to make sure it's non-negative and small enough to fit the result. in this case, the answer is a huge positive integer.
int i = -20; unsigned j = 10; \_\_int64 k = i + j;
k = 4294967286
image processing toolkits | batch image processing
modified on Friday, January 15, 2010 11:22 AM
-
Good info - I think the general rule is: don't use unsigned numbers in calculations.
MVP 2010 - are they mad?
Richard MacCutchan wrote:
Good info - I think the general rule is: don't use unsigned numbers in calculations.
Don't mix unsigned and signed numbers, without proper explicit casts, in calculations. FFY. :-D
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]