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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C++ Q, num*num, resulting code

Status
Not open for further replies.

StoneColdCrazy

Programmer
Jun 30, 2000
11
0
0
US
1).&nbsp;&nbsp;int a = 50*20;<br>2).&nbsp;&nbsp;int b=50, c=20, a=c*b;<br><br>Will the compiled code in case 1 multiply 20*50 in order to get the number a, or will that operation be performed at compile time??&nbsp;&nbsp;Same question about case 2??<br><br>NOTE: I using Borland C++, and the resulting code of case 1 from that compiler is, int a = 1000, (or something like that, except the code is in assembly), and the resulting code in case 2, is a = c*b...&nbsp;&nbsp;I just want to know, does all compilers do that, or some compilers will multipy the numbers at run time (even in 1st case)??
 
I think they all do the same thing while compiling. If they find a an '=' followed by a constant value ( can be also a constant result of an expression ) they optimize it.
So also if you have :
int a = 1+2+3+4+5;
the result is :
int a = 15;

In the second case, instead, <a> is a variable, so it's a bit more difficult for the optimizer to understand that you intend to use <a> as a constant. More intelligent optimizer do this and more, others not.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top