We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hello,
there might be an undetected overrun in the result of 'a * b'. So i used the cast ((unsigned long long int)a to force the result of 'a * b' to long long. Unfortunately this compiler warning.
unsigned int n,a,b,c; //************************************************************************* void main(void) { a = 1; b = 1024; printf("Hello\n"); for(n=0;n<8;n++) { c = ((unsigned long long int)a * b) >> 10; printf("%u %10u %10u\n",n,a,c); a *= 10; } while(1){} }
Should I ignore the warning? or better how to work around?
If you typecast one of the parameters to the multiply, you make sure that the multiply will be made with a big enough data type.
Then you have to inform the compiler that you guarantee that the result (after the shift) will fit, bu typecasting the full expression back to the smaller data type.
Thanks :-)
c = (unsigned int)(((unsigned long long int)a * b) >> 10);
There are a lot of braces. :-)
No, actually not a single one :)
en.wikipedia.org/.../Bracket
There are four main types of brackets:
round brackets or parentheses: ( ) square brackets or box brackets: [ ] curly brackets or braces or Nances: { } angle brackets or chevrons: < >
scnr :-)
Your wiki page describes four types of brackets but only one type of braces.
It also says: "The term curly braces is redundant since no other type of brace exists."
So, still no braces in your expression :)
Then you have to inform the compiler that you guarantee that the result (after the shift) will fit
Close, but not quite true. What that cast does is inform the compiler that you don't care about the case where the result might be too large for an "unsigned int". This cast is an expression of intent, not one of knowledge.