开发者

does argument in printf get located in memory?

in c, when I write:

printf("result %d ",72 & 184);

开发者_JAVA百科

Does "72 & 184" get a a block in memory (for example 72 takes 4 bytes, 184 takes 4 bytes?...)


Since 72 & 184 is a constant expression, your compiler is likely to evaluate the answer at compile time, instead of generating code to calculate the same result at runtime.

The answer happens to be 8, so the statement would be the same as:

printf("result %d ", 8);

In this case the compiler is likely to generate what's called an immediate push onto the argument stack, where the value 8 is embedded right into the machine instruction(s). This might be different than pushing a larger value such as 12345678, where it might be too big for an immediate push and then might need to be stored in program memory.

The above is entirely dependent on the CPU architecture for which you're compiling your program, as well as the capabilities and optimisation settings of your compiler.


That can depend on the compiler you use. With optimizations the compiler can fold that instruction into one value. That value would reside in the memory with the rest of your code but during the execution of that line it would reside in a cpu register.

I'm not sure if any compiler would optimize out the %d with the actual value though:)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜