This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

const unsigned int

This are some of my const agreements.
It works.

#define Clock (32768*1275)
const unsigned int Mem_size[8] = {
32,
320,
3200,
3200,
3200,
3200,
3200,
3200
};
// Delay-time * Clock * 1024 / 10 / Mem_size[0~7]
const unsigned int Multiplicator[8] = {
0.0001 * Clock * 1024 / 10 / 32,
0.001  * Clock * 1024 / 10 / 320,
0.01   * Clock * 1024 / 10 / 3200,
0.1    * Clock * 1024 / 10 / 3200,
1.0    * Clock * 1024 / 10 / 3200,
10.0   * Clock * 1024 / 10 / 3200,
100.0  * Clock * 1024 / 10 / 3200,
1000.0 * Clock * 1024 / 10 / 3200
};


But one const can not refer to another const.
For example i can not replace the number 3200 by
Mem_size[2].
Why not? Is there a work around?

Sincerly yours

Parents
  • That will save you code size and processing time.

    I can not agree with your opinion.
    In my opinion const values are computed by the compiler at compiling time.
    The values are not computed by the uC at run time.

    I didn't notice a significant increase of code because of my loquacious const declaration. But maybe i didn't payed enough attention to that?

Reply
  • That will save you code size and processing time.

    I can not agree with your opinion.
    In my opinion const values are computed by the compiler at compiling time.
    The values are not computed by the uC at run time.

    I didn't notice a significant increase of code because of my loquacious const declaration. But maybe i didn't payed enough attention to that?

Children
  • In this case, the compiler will do the evaluation, so it will not affect any code size. Even in expressions in executable statements, the compiler will pre-compute constant expressions. This normally happens even if optimization is turned off.

  • Triple,
    I was making a general statement - indeed, in this case it does not matter.

  • ANSI C89 (emphasis added). Note in particular that a declaration containing "const" is not the same thing as a "constant expression"

      More latitude is permitted for constant expressions in
    initializers.  Such a constant expression shall evaluate to one of the
    following:
    
     * an arithmetic constant expression,
    
     * an address constant, or
    
     * an address constant for an object type plus or minus an integral
       constant expression.
    
       An arithmetic constant expression shall have arithmetic type and
    shall only have operands that are integer constants, floating
    constants, enumeration constants, character constants, and sizeof
    expressions.  Cast operators in an arithmetic constant expression
    shall only convert arithmetic types to arithmetic types, except as
    part of an operand to the sizeof operator.
    
       An address constant is a pointer to an lvalue designating an object
    of static storage duration, or to a function designator; it shall be
    created explicitly, using the unary & operator, or implicitly, by the
    use of an expression of array or function type.  The array-subscript
    [] and member-access .  and -> operators, the address & and
    indirection * unary operators, and pointer casts may be used in the
    creation of an address constant, but the value of an object shall not be
    accessed by use of these operators.
    
    

    Compiler folding of expressions containing literals is a different question.