-1

I have read many discussions of what constitutes a "side effect" in C, and many seem to indicate that it must involve changing something that is not local to the function causing the change. Changing an external variable or a file are the typical types of things the discussions say have to be changed to qualify as a side effect. The discussions also commonly imply that merely changing the value of a local automatic variable in the same block in which it is declared is not a side effect. However, 5.1.2.3.2 of the C17 standard states the following:

Accessing a volatile object, modifying an object, modifying a file, or calling a function that does any of those operations are all side effects,12) which are changes in the state of the execution environment.

My understanding from C17 6.2.4.1 is that all variables (among other things) represent objects. If that is the case, wouldn't changing the value of any variable regardless of scope or storage class qualify as a side effect?

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
BenevolentDeity
  • 693
  • 1
  • 4
  • 18
  • An expression without side-effects can be removed from the program and replaced with final value without changing observable behavior of the program. Assignments to objects cannot be removed from the code. – tstanisl Jan 28 '23 at 11:16
  • Re “many seem to indicate that it must involve changing something that is not local to the function causing the change”: When relating information from other sources, you should give a proper citation for the source, at least a URL, but preferably the bibliographic information you were taught in school: Title, author, and other identifying information, and the chapter, page, and other location in the work. This will help readers find the context for what you are referring to. – Eric Postpischil Jan 28 '23 at 11:18
  • 2
    I do not know why your question is voted down, but a possible reason is that it relates false premises: “many seem to indicate that it must involve changing something that is not local to the function causing the change” and “ imply that merely changing the value of a local automatic variable in the same block in which it is declared is not a side effect”. If you cited sources for these, the errors could be blamed on their authors. Without that, the voters may be attributing the errors to you, as you may have misinterpreted and misrepresented what you read. – Eric Postpischil Jan 28 '23 at 11:25
  • Thanks for taking the time to elaborate on this Eric. I was just looking for an opinion of whether what I perceived some others to be saying was actually somehow correct. There were so many different instances that it seemed counterproductive to try to provide references, but I'll certainly keep that in mind in future postings :-) – BenevolentDeity Jan 28 '23 at 11:41
  • Another class of side effects is reading a volatile variable, as discussed in [this recent question](https://stackoverflow.com/questions/75247233). – Steve Summit Jan 28 '23 at 13:35

1 Answers1

1

If that is the case, wouldn't changing the value of any variable regardless of scope or storage class qualify as a side effect?

Yes.

Consider the code a = 3; printf("%d\n", a++);. This prints “3”, because the main effect of a++ is to produce the value of a for the expression, and the value of a is 3. The code also changes the value of a, and that is a side effect.

Consider printf("%d\n", (a = 3) * 4);. The main effect of the assignment expression, a = 3, is to produce the new value of the assigned object, so its value is 3. Then that is multiplied by 4, so printf prints “12”. As a side effect, the stored value of a is changed to 3.

Also, printf writes to standard output, and that is also a side effect.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312