The answer is:" Const expressions are computed at compile time".
Let see together a practical demonstartion.
If we consider the following code:
1
2
3
| int main() {
int a=1+3;
}
|
And compile it using gcc with -S option we obtain:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| .file "ottimo.c"
.def ___main; .scl 2; .type 32; .endef
.text
.globl _main
.def _main; .scl 2; .type 32; .endef
_main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
movl $0, %eax
addl $15, %eax
addl $15, %eax
shrl $4, %eax
sall $4, %eax
movl %eax, -8(%ebp)
movl -8(%ebp), %eax
call __alloca
call ___main
movl $4, -4(%ebp)
leave
ret
|
As we can see, the compiler, even without the selection of optimization options, optimize the code and translate a= 1+3 as movl $4, -4(%ebp), that is a =4.
Q.E.D. :)
Nessun commento:
Posta un commento