3
votes

From ISO/IEC 9899:201x section 5.1.2.3 Program execution paragraph 2:

Accessing a volatile object, modifying an object, modifying a file, or calling a function that does any of those operations are all side effects, which are changes in the state of the execution environment. Evaluation of an expression in general includes both value computations and initiation of side effects. Value computation for an lvalue expression includes determining the identity of the designated object.

The paragraph says that "modifying an object" is a side effect. It means that the following code:

int x; 
x = 1;

has a side effect which is the x = 1 as it is modifies the object x.

However, according to wikibooks on C Programming:

In C and more generally in computer science, a function or expression is said to have a side effect if it modifies a state outside its scope or has an observable interaction with its calling functions or the outside world. By convention, returning a value has an effect on the calling function, but this is usually not considered as a side effect.

Some side effects are:

  • Modification of a global variable or static variable
  • Modification of function arguments
  • Writing data to a display or file
  • Reading data
  • Calling other side-effecting functions

So, who is right? is x = 1 really a side effect? even though it does not change anything outside it's scope? or am I wrongly interpreted the standard?

4
I think the wiki is confused and is using the term "side effects" for unoptimizable/externally-visible side effects. As Erik Postpischil says in the link you've presented, the result of an expression evaluation is a primary effect. All writes, volatile reads, and syscalls that happen during the evaluation are side effects of which, some writes (or function calls that make them) can be theoretically otpimized out under the as-if rule. - PSkocik
Every expression has a value (except void expressions) and 0 or more side-effects. The value of the expression x = 1 is 1; its side-effect is assigning 1 to x. You can use the value in a larger expression (y = 4 - (x = 1)) but you cannot depend on order of execution of side-effects (y = (x = 4) + (x = 2); /* y == 6; x == ?? */) - pmg
@mpg great explanation, makes perfect sense now. - izac89
StoryTeller’s answer is correct, but I will add that the Wikibooks article gives a different definition of side effect, and what it says about those side effects may be correct—but it is a different use of the term. That is, it is not wrong (because people are allowed to use their own definitions for terms; the C standard has no authority to compel otherwise), but it ought to have made clear it was using some non-C definition. Notably, the article is wrong to say “In C.” - Eric Postpischil

4 Answers

8
votes

So, who is right?

When it comes to the definitions in the standard, it's the standard.

is x = 1 really a side effect? even though it does not change anything outside it's scope?

Yes, the standard paragraph you quoted said as much.

or am I wrongly interpreted the standard?

You understood and applied the standard paragraph correctly to x = 1. But you were wrong to try and apply an outside colloquial definition onto the standard text. The C standard is not meant to teach anyone about C. It is a formal document whose sole purpose is to define how the C abstract machine executes a translated program. To that end it defines a bunch of concepts and terms. That's it. When referring to those terms in order to divine the intended behavior of a C program, only the standard's definition applies.

The book on the other hand does aim to teach you C. Its purpose is to give you a "feel" for how a C program behaves. But to that end it may very well use colloquialisms and imprecise language, that's to be expected. You should not disregard the book if it has good reviews, but bear in mind that it is not a normative reference, unlike the standard.

3
votes

These sort of confusions arise when C books adopt terms used in the C standard (e.g. side effect) and redefine them.

A side effect in C is exactly as that paragraph in the standard defines it to be.

And yes x = 1 is therefore a side effect.

Whether or not the as if rule compiles out the assignment is a different matter. Perhaps, in the book, the term observable effect would have been better?

1
votes

Both your quotes include a definition of side effect:

C Standard:

which are changes in the state of the execution environment

C Wikibook:

if it modifies a state outside its scope or has an observable interaction with its calling functions or the outside world.

Those define different things and thus the confusion. While x = 1 is a side effect (C Standard) it is not a side effect (C Wikibook). The C Wikibooks definition is called an observable effect in the C Standard.

-1
votes

The C Standard tends to give vague and incomplete definitions of terms which make no particular effort to exclude everything to which the term does not apply. For example, the C11 draft defines object as "region of data storage in the execution environment, the contents of which can represent values". That would be analogous to defining "automobile" as "a motorized vehicle which can transport people". Such a definition may be good for distinguishing a car from a horse, but not for distinguishing a car from a bus, pickup truck, or a camper van.

The notion of "side-effect" as generally used encompasses the notion of something outside the immediate scope of an operation, and such notion would be consistent with the way the Standard actually uses the term, if one recognizes that for purposes of evaluating an expression or sub-expression, the "scope" of that operation would be limited to the result of that operation or to things whose lifetime is bound to its evaluation. If the Standard were to try to limit the term "side-effect" in that fashion, however, it would have to then define the "scope" to which the side-effects were relative. Rather than doing that, the authors of the Standard simply used a broader definition and relied upon readers to limit application to places where it would make sense.