Home:ALL Converter>What is a side effect in C?

What is a side effect in C?

Ask Time:2020-09-27T20:45:01         Author:op ol

Json Formatter

In C99 clause 5.1.2.3 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.

C standard defines side effects like above. But It seems C99 doesn't explain what exactly is accessing a volatile object, modifying an object, modifying a file(It's defined in the clause 3 what is the definition of access, modify, object. But accessing a volatile how? modifying what object? and modifying what of a file?).
There are some examples in C99 as I search with the word side effects. But I can't be sure whether each of examples is classified among accessing a volatile object, modifying an object and modifying a file.
I read What is side effect in c? and Are side effects a good thing? but still am confused.
My question is that does the C standard explicitly describe a meaning of side effects? What they mean?

Author:op ol,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/64088615/what-is-a-side-effect-in-c
Eric Postpischil :

\nMy question is that does the C standard explicitly describe a meaning of side effects?\n\nThe sentence in the C standard you quote (C 1999 5.1.2.3 2, and the same in C 2018) explicitly describes the meaning of side effects. These are further explained below.\nModifying An Object\nModifying an object is understood to include the things that update the stored bytes that represent the object. I believe a complete list of them is:\n\nSimple assignment (=) and compound assignment (*=, /= %=, +=, -=, <<=, >>=, &=, ^=, and |=).\nThe increment and decrement operators (++ and --), both prefix and postfix.\nInitialization of an object included in its definition.\nLibrary routines that are specified to change objects, such as memcpy.\n\nAccessing a Volatile Object\n“Access” is defined in C 2018 3.1 as “⟨execution-time action⟩ to read or modify the value of an object”. If x is a volatile int, then using the value of x in an expression accesses it (when the expression is evaluated), because it reads the value of x. You can follow this more specifically in that 6.3.2.1 2 tells us that the use of x in an expression results in the value of x being taken:\n\nExcept when it is the operand of the sizeof operator, the unary & operator, the ++ operator, the -- operator, or the left operand of the . operator or an assignment operator, an lvalue that does not have array type is converted to the value stored in the designated object (and is no longer an lvalue); this is called lvalue conversion.\n\nSo the x in the expression which is, by itself, just a designation of the object x, is converted to the value stored in x, which means that stored value is read from memory. That is an access of x.\nModifying a volatile object is the same as modifying any object, described above.\nModifying a File\nFiles are modified by way of the routines defined in clause 7.21 (“Input/output <stdio.h>”).",
2020-09-27T13:08:17
yy