tags:

views:

98

answers:

2

in c, when I write:

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

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

+5  A: 

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.

Greg Hewgill
+2  A: 

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:)

chollida