for example code:

1) #define PRIMERP 315 2) #define PRIMERF(V1 V2)(V1+2)*(V2+2) int i = PRIMERP; int j = PRIMERF(2,3); 

I would like to clarify 1 is a kind of constant and 2 is a function of the same kind of constant and after compiling the cpp file into an exe file, these constants will not be included in the executable file, that is, if the following is decompiled

 int i = 315; int j = 20; 

I understand correctly

  • Preprocessor , in si - Wootiae
  • 2
    The preprocessor directive #define replaces one piece of text with another. In this example, in the first case, after the replacement, it will really be int i = 315; however, int j = (2+2)*(3+2); in the second. - VTT
  • You should at least try to correctly reproduce the code. What you wrote is just a mistake (typos). - AnT
  • If the compiler (after the preprocessor) calculates constant expressions (usually this is the case), then in principle (up to the choice of a decoupler of names), that's right - avp

1 answer 1

The directive #define in itself carries out only text substitution, but not prediction of expressions with constants. That is, if you correct the PRIMERF declaration to the correct one (you have a comma missing):

 #define PRIMERF(V1,V2) (V1+2)*(V2+2) 

then at the stage of processing by the preprocessor PRIMERF(2,3) will turn into (2+2)*(3+2) .

Predicting a constant expression can be done at compile time, but this is not guaranteed. Let's say Visual C ++ performs it, including when optimization is disabled:

 #include <stdio.h> #include <stdlib.h> #define PRIMERP 315 #define PRIMERF(V1,V2)(V1+2)*(V2+2) int main(int argc, char **argv) { int i = PRIMERP; int j = PRIMERF(2,3); printf("%d %d",i,j); ; getchar(); return 0; } /* Disassembly: _main: push ebp mov ebp,esp sub esp,8 push esi mov dword ptr [j],0CCCCCCCCh mov dword ptr [i],0CCCCCCCCh mov dword ptr [i],13Bh // int i = PRIMERP; mov dword ptr [j],14h // int j = PRIMERF(2,3); mov esi,esp mov eax,dword ptr [j] push eax mov ecx,dword ptr [i] push ecx push 0F05858h call dword ptr ds:[0F092C0h] // printf("%d %d",i,j); add esp,0Ch cmp esi,esp call _RTC_CheckEsp (0F01220h) mov esi,esp call dword ptr ds:[0F092C8h] // getchar(); cmp esi,esp call _RTC_CheckEsp (0F01220h) xor eax,eax pop esi add esp,8 cmp ebp,esp call _RTC_CheckEsp (0F01220h) mov esp,ebp pop ebp ret */ 

14h is the precomputed value of the expression (20 in hexadecimal)