Deep Analysis of Const Qualifier in C

前端 未结 9 1004
离开以前
离开以前 2020-11-30 08:26

Where does a const variable gets stored exactly and how does it behaviour change? Say for example:

const int i=10; // stores where ?  
main()  
         


        
9条回答
  •  迷失自我
    2020-11-30 08:55

    Its totally up to the compiler writer what happens to the const, and it will vary according to the optimisation you request.

    In your first example the constants are never used so the compiler probably will just ignore them altogether.

    In your second example as you use "address off" it must actualy store it somewhere -- probably at the begining of the stack.

    As C is designed to replace assembly language instructions, and for writing OS kernal and device driver type code it lets you do all sorts of things and assumes you know what you are doing when you start messing with pointers.

提交回复
热议问题