Hi All, I am experiencing a basic problem in the arithametic of the instrucation A = ((B-C)*100)/204 during execution of the instruction, if the result of (B-C)*100 is greater than 16 bits what will happen? for avoiding data loss do I have to declare it as 32 bit variable or the proceesor will handle it without changing the size? Variable A is declared as 16 bit. do I have to use both B and C 32 bit if i conditionally change the instrucation as A = ((C-B)*100)/204 during execution ? my code will look like this if (B>C) A = ((B-C)*100)/204; else if (C>B) A = ((C-B)*100)/204 ; my processor is C8051F020 compiler is C51 best regards Deepak
This is a basic C question, with no particular relevance to C51. Look for the answers in your C textbook or in an appropriate place on the web, please, not here. But here's a hint: writing "100L" will suffice.
Another hint: 'C' assumes that eveything is an int unless advised otherwise...
hi, My doubt is that where the intermediate result is stored ? if all declared as int the final result will be wrong right? so i hope that there should be a long variable to store it. but which one should it be ? rgds deepak
if all declared as int the final result will be wrong right? Yep. but which one should it be ? That is entirely up to you. You could do all the calculations with a local long int variable (if you know that the end result will always fit in 16 bits), or you could make A a long int, for example.
"You could do all the calculations with a local long int variable (if you know that the end result will always fit in 16 bits), or you could make A a long int, for example." Or you could just let the compiler take care of it - think about the hint from Hans-Bernhard Broeker
View all questions in Keil forum