Comparison of Const and #define

xiaoxiao2021-03-06  43

I believe that as long as I have seen an animal about C optimization, I will always mention it in the optimization clause: I have been doing it with CONST. I have been doing this, and I have been done, but today I found a constant shortcomings. Where:

I believe that as long as I have seen an animal about C optimization, I will always mention it in the optimization clause: I have been doing it with CONST. I have been doing this, and I have been done, but today I found a constant shortcomings. From: const I = 0; void main () {printf ("Address of const i:% 08x / n", & i);} For the above code, I want to compile the prompt error, but it is a pity, no Any error taken again, const I = 0; void main () {INT * P = (int *) & i; * p = 33;} Compile, pass !! But the program is immediately unmanned Illegal operation of love ..-_-

From the 2 code from above, we can see that (1) Const also needs to take up memory, and you need to take up file space, this is undoubtedly a cruel torture, when you encounter this variable time The compiler will use constants instead of memory addressing.

(2) In a certain extent, Const will lead to serious consequences, one but the code is more, this error is often difficult to find

So, I still recommend using a constant friend with #define (see it comes with it. H. Which one is not using #define?), If you don't think it is, then you can try ENUM

(Test environment: vs6.0, win98se)

转载请注明原文地址:https://www.9cbs.com/read-78882.html

New Post(0)