Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

num*num, at compile time or runtime

Status
Not open for further replies.

StoneColdCrazy

Programmer
Jun 30, 2000
11
US
case 1).&nbsp;&nbsp;int a = 50*20;<br>case 2).&nbsp;&nbsp;int b=50, c=20, a=b*c;<br><br><br>When we compile that, will the resulting code in case one be a=1000 or will the code multiply 50*20 in order to get the number?&nbsp;&nbsp;Same question about case 2... <br>??<br><br>Is there a mandatory rule about this to all C or C++ compilers?
 
Hello.<br><br>Any normal C/C++ compiler always&nbsp;&nbsp;tries<br>to calculate all possible expressions in<br>preprocessig stage.<br><br>In your first case, if you declared a as a local variable, compiler will calculate <br>50*20 in preprocessing stage and will proguce<br>the approximately following assembler code:<br><br>...<br>movl $1000, -xxx(%ebp)<br>...<br><br>In second case, expression b*c cannot be evaluated neither in preprocessing stage<br>nor in compilation stage. It can be evaluated<br>only in runtime. As compilation result you<br>will get the following assembler code:<br><br>...<br>movl $20, -xxx(%ebp)<br>movl $50, -yyy(%ebp)<br>movl -xxx(%ebp), %eax<br>imul -yyy(%ebp), %eax<br>movl %eax, -zzz(%ebp)<br>...<br><br>Bye. <p>Victor Ustymchuk<br><a href=mailto:snake@jb.rovno.ua>snake@jb.rovno.ua</a><br><a href= > </a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top