Troubleshooting
Why a WinCC C Action Reports Left Operand Must Be L-Value
Short answer
The compiler rejects the action because a function call on the left of '=' is a value, not writable storage. Assign the project function's return value to a local C variable of the function's return type, then write that variable to the tag with the matching tag function — SetTagBit for a binary tag. Changing the tag's type cannot fix it.
On this page
- Why does the compiler call the left operand an l-value?
- Why doesn't changing the tag's type fix the error?
- How do you write the function result to the tag?
- What do you do when C has no BOOL and the tag needs one bit?
- Example: what happens when a 40000 count meets a signed 16-bit tag?
- The action compiles now, but why does the tag value never change?
- Questions asked
A button action that assigns a project function's return value straight to a tag fails to compile with "Left Operand Must Be l-value", and no tag type will change that. The error appears because the expression left of the equals sign names no storage the script can write into: a function result is a value, not a variable. The fix is a two-step shape — store the return value in a local C variable, then pass that variable to the tag-writing function matched to the tag's type. Below: why the compiler insists, the three-line pattern that compiles, the byte-versus-bit answer for TRUE/FALSE tags, a worked example with the overflow arithmetic, and what to test when the action compiles but the tag never moves.
Why does the compiler call the left operand an l-value? #
In C, an l-value is an expression that names a place in storage your code can modify, and every assignment needs one on the left of the equals sign; a project function's return value is a transient value with no storage, so the compiler rejects the line.
The rule belongs to the C language, not to WinCC: the same message appears in plain C when a statement assigns to an array name or to an arithmetic expression, because neither is a place to store into. WinCC adds one layer — a tag belongs to the runtime tag system, so a tag name is not a writable C object inside the action. Tags are reached only through the read and write functions of the script library, which is why the assignment must be aimed at a real variable.
The check happens at compile time in the action editor, so the action never downloads and never runs: transferring the project again or restarting runtime cannot clear it, and only a changed statement can.
The trap is not unique to C-style scripts either. In structured text the sibling defect is a function whose result is never stored anywhere, and a structured-text function that returns zero because its result was not assigned shows how that one hides until commissioning.
Why doesn't changing the tag's type fix the error? #
Because the compiler rejects the structure of the statement, not the tag's data type, and structure checks come first: whether the tag is binary, a 16-bit word or a 32-bit double word, the left side still names no writable storage, so every type fails with the same message.
The message text never mentions the tag at all — it points at the operand, and that is the tell. When a type mismatch is the problem, the compiler complains about conversion or range; when the complaint is word-for-word identical across binary, word and double-word tags, the parser is rejecting the sentence shape, and no type can repair a sentence.
So stop cycling tag types. The type only becomes meaningful once the statement is legal: after the return value sits in a local variable, the tag's type decides which writer you call and what range check you owe the value — the worked example below carries one through.
How do you write the function result to the tag? #
Assign the project function's return value to a local variable declared with that function's return type, then pass that variable to the tag-writing function matched to the destination tag; WinCC exposes one writer per tag type, and SetTagBit is the documented writer for a binary tag.
- Find the failing line in the action and name what sits left of the equals sign — in this fault it is the project function call itself.
- Open the project function's declaration and read its return type; declare the local variable with that same type, not with whatever the destination tag happens to be.
- Assign the call's result to that variable as its own statement. The line compiles, because a declared local variable is storage the script owns.
- Choose the writer matched to the destination tag's type; for wider numeric tags, take the writer documented for that type in the installed script library.
- Call the writer with the tag's configured name and the variable, and read the writer's own return value the way its documentation defines it — a confirmation code is not a readback.
long result = 0; /* local storage: the legal l-value */
result = GetBatchCount(); /* GetBatchCount stands in for your project function */
SetTagBit("BATCH_DONE", result != 0); /* binary tag write; the tag name is illustrative */
Both names in the listing are illustrative — substitute yours. SetTagBit is the documented call, and the comparison result != 0 normalizes the value to 1 or 0 before it goes out. The proof is two-stage: the error disappears at step 3, before any tag is involved, and then a button press at runtime moves the tag in a value table — that readback, not the writer's return code, is what proves the write path.
One caution belongs here: a script write can change a setpoint the machine acts on, so prove the pattern on a bench project with outputs disabled before it goes near a live line.
What do you do when C has no BOOL and the tag needs one bit? #
C in this scripting environment has no BOOL type and the smallest local variable is one byte, so hold the two states as the values 0 and 1 in that byte; the binary tag still carries one bit at the controller, because SetTagBit writes a bit, not your byte.
Make the state explicit rather than trusting the writer's conversion rules. C treats zero as false and any nonzero value as true, so a raw count of 40 or -3 would both act as "on"; the comparison in the listing — result != 0 — reduces whatever came back to a clean 1 or 0 and writes the state you intended. The byte-versus-bit cost is the trade, not a defect: the variable exists only while the action executes, and the controller stores the single bit the tag maps to, which is the smallest legal shape the compiler offers for a TRUE/FALSE signal.
Example: what happens when a 40000 count meets a signed 16-bit tag? #
With these illustrative numbers the write must not go through a signed 16-bit tag: the count 40000 overflows it and would display as -25536 after wrapping through 65536, so the decision is to widen the tag to 32 bits or clamp above +32767 before writing.
- The local variable follows the function's declaration: long. A long here holds at least 32 bits, with a signed ceiling of 2147483647, so the count fits and the assignment compiles with no conversion at all.
- The tag's range is the next check, and the compiler does not do it for you: a signed 16-bit word stores -32768 through +32767. The count is 7233 above that ceiling, so a direct 16-bit store would wrap to 40000 - 65536 = -25536, and the operator would watch the batch total flip negative on screen.
- Decision: if the value must genuinely reach 40000, widen the tag to the 32-bit type and call the writer documented for that type. If the tag must stay 16-bit, clamp or reject above +32767 in the script and write nothing on overflow. Either way the check is yours: a legal C assignment narrows a value or reinterprets its sign without a word of complaint.
The action compiles now, but why does the tag value never change? #
A green compile proves the statement is legal C, not that the value reached the controller, so test the calculation and the runtime write as two separate steps: confirm the local variable first, then trace the tag write, and only after both pass look at the driver.
If the local variable never receives the expected value, the defect is upstream in the project function, not in the write — instrument the function before touching anything else. If the variable is right and the controller still sees nothing, you have crossed into write-fault territory: Why a WinCC Professional Button Does Not Write to the PLC works exactly that case.
Two checks dominate there. The tag's configured address must point at memory the controller accepts writes into — a write aimed at an input area is silently ignored, the fault behind Why WinCC Start and Stop buttons do not control an S7-300 output. And the connection must be carrying good data at all, which the ordering in Is It the Driver, the Tag or the Screen? Diagnosing HMI Values settles before any driver gets blamed.
Questions asked
Does the l-value rule also apply when I read a tag into a variable?
No. On a read, the tag function sits on the right side of the equals sign, where C expects a value, so the call is legal exactly as written. Use the read function that matches the tag's type, assign its result to a local variable declared with a compatible type, and only the write side needs the tag function. The error fires only when the left side cannot store anything.
Where do I set the automatic logoff idle time for a runtime user?
Not in the C script library; no script parameter exposes it. For WinCC Unified, the documented place is Security settings > Users and roles: open the Roles tab and enter the timeout in the Runtime timeout column of the role the user logs in with. Other releases keep the session timeout in their own runtime user-administration settings for the installed version.
How do I update several tags from one project function call?
Assign the return value to one local variable, then call the tag function once per tag, passing that variable each time. Do not call the project function twice for one logical value, because two evaluations can return different results and the tags can disagree. Chained assignments do not help: every additional equals sign on the line needs its own writable operand on the left.