P4008

Code

P4008

Message

Variable is constant but does not define value

This error occurs when a variable is declared as constant but does not define an initial value.

Example

The following code will generate error P4008:

PROGRAM main
    VAR CONSTANT
        ResetCounterValue : INT;  (* Error: CONSTANT must have initializer *)
    END_VAR
END_PROGRAM

The variable ResetCounterValue is declared as CONSTANT but does not have an initial value assigned.

To fix this error, provide an initial value for the constant variable:

PROGRAM main
    VAR CONSTANT
        ResetCounterValue : INT := 17;
    END_VAR
END_PROGRAM

The same rule applies to VAR_GLOBAL CONSTANT declarations.

See Also

  • Initial Values — initializer syntax

  • Variable Scope — variable sections that accept CONSTANT

  • P4009CONSTANT qualifier on function block instances

  • P4031 — constant referenced in a type parameter is not an integer type

  • P4032 — constant referenced in a type parameter has no initializer

References

Think IronPLC is wrong about this?

If you believe this diagnostic is incorrect, open an issue on GitHub with a small sample that demonstrates the problem.