Post Reply 
C Evaluation
07-14-2022, 11:44 PM (This post was last modified: 07-14-2022 11:45 PM by cruff.)
Post: #3
RE: C Evaluation
To further clarify what Craig B. is saying, the C language specification talks about "sequence points", and permits the compiler to reorder expressions as long as the meaning is not changed. However there is much room for unspecified behavior. If you need to force a specific evaluation order, you must use statements separated by semicolons with intermediate values. You also need to be wary of statements like the following:
Code:
i = 0;
i = ++i + ++i;

Where you could possibly get answers of 2 or 3. A good compiler will warn you about this, for example from LLVM clang:

Code:
% cc -o t t.c
t.c:8:9: warning: multiple unsequenced modifications to 'i' [-Wunsequenced]
    i = ++i + ++i;
        ^     ~~
1 warning generated.
% ./t
3

Things get even more complex if you are parallel programming and you need to enforce the order of memory accesses as seen by CPUs and DMA devices in a system, but that is a larger subject you probably don't care about at the moment.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
C Evaluation - toml_12953 - 07-14-2022, 08:06 PM
RE: C Evaluation - Craig Bladow - 07-14-2022, 08:34 PM
RE: C Evaluation - cruff - 07-14-2022 11:44 PM
RE: C Evaluation - Sylvain Cote - 07-15-2022, 01:12 AM
RE: C Evaluation - KeithB - 07-15-2022, 11:20 PM
RE: C Evaluation - ijabbott - 07-16-2022, 05:02 PM
RE: C Evaluation - robve - 07-16-2022, 06:43 PM



User(s) browsing this thread: 1 Guest(s)