Deep Analysis of Const Qualifier in C

前端 未结 9 1000
离开以前
离开以前 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:57

    The second code shouldn't compile at all. And the compilers I've here agree: gcc gives an error with -pedantic-errors (whose purpose is to turn into error some mandatory diagnostics that historically gcc hasn't considered as error), xlc gives an error as well.

    If you want a reference: 6.3.16.1 in C90 standard describes when an assignment is possible:

    both operands are pointers to qualified or unqualified versions of compatible types, and the type pointed to by the left has all the qualifiers of the type pointed to by the right

    and c99 as a similar constraint.

提交回复
热议问题