Skip to content
Hub Nexus

No author yetClaim it

Version 1Updated 4 min read

Support

Side effects

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

A function or expression that has no side effects is pure. Pure functions are guaranteed to be deterministic; their output will always be the same for some given input. In the presence of side effects, a program's behaviour may depend on history; that is, the order of evaluation matters. Understanding and debugging a function with side effects is more difficult, requiring knowledge about the context and its possible histories.[1][2]

Sequence points

A sequence point defines any point in a computer program's execution at which it is guaranteed that all side effects of previous evaluations will have been performed, and no side effects from subsequent evaluations have yet been performed. They are often mentioned in reference to C, because they are a core concept for determining the validity and, if valid, the possible results of expressions. Adding more sequence points is sometimes necessary to make an expression defined and to ensure a single valid order of evaluation.

  • An expression's evaluation can be sequenced before that of another expression, or equivalently the other expression's evaluation is sequenced after that of the first.
  • The expressions' evaluation is indeterminately sequenced, meaning one is sequenced before the other, but which is unspecified.
  • The expressions' evaluation is unsequenced.

The execution of unsequenced evaluations can overlap, with catastrophic undefined behavior if they share state. This situation can arise in parallel computations, causing race conditions.

Examples of ambiguity

Consider two functions f() and g(). In C, the + operator is not associated with a sequence point, and therefore in the expression f() + g() it is possible that either f() or g() will be executed first. The comma operator introduces a sequence point, and therefore in the code f(), g() the order of evaluation is defined: first f() is called, and then g() is called.

Sequence points also come into play when the same variable is modified more than once within a single expression. An often-cited example is the C expression i = i++, which apparently both assigns i its previous value and increments i. The final value of i is ambiguous, because, depending on the order of expression evaluation, the increment may occur before, after, or interleaved with the assignment.

Locations

In C[4], sequence points occur in the following places:

  • Between evaluation of the left and right operands of the && (logical AND), || (logical OR) (as part of short-circuit evaluation), and comma operators.
    For example, in the expression *p++ != 0 && *q++ != 0, all side effects of the sub-expression *p++ != 0 are completed before any attempt to access q.
  • Between the evaluation of the first operand of the ternary "question-mark" operator and the second or third operand.

For example, in the expression a = (*p++) ? (*p++) : 0 there is a sequence point after the first *p++, meaning it has already been incremented by the time the second instance is executed.

  • At the end of a full expression. This category includes:
    expression statements (such as the assignment a = b;)
  • return statements
  • the controlling expressions of if, switch, while, or do-while statements
  • all three expressions in a for statement
  • Before a function is entered in a function call. The order in which the arguments are evaluated is not specified, but this sequence point means that all of their side effects are complete before the function is entered.

In the expression f(i++) + g(j++) + h(k++), f is called with a parameter of the original value of i, but i is incremented before entering the body of f. Similarly, j and k are updated before entering g and h respectively. However, it is not specified in which order f(), g(), h() are executed, nor in which order i, j, k are incremented. If the body of f accesses the variables j and k, it might find both, neither, or just one of them to have been incremented.

  • At the end of an initializer.
    for example, after the evaluation of 5 in the declaration int a = 5;.
  • Between each declarator in each declarator sequence.

For example, between the two evaluations of a++ in int x = a++, y = a++. (This is not an example of the comma operator.)

  • After each conversion associated with an input/output format specifier.
    For example, in the expression printf("foo %n %d", &a, 42), there is a sequence point after the %n is evaluated and before printing 42.

References

  • ↑ “Research Topics in Functional Programming” ed. D. Turner, Addison-Wesley, 1990, pp 17–42. Retrieved from: Hughes, John, Why Functional Programming Matters (PDF)
  • ↑ Collberg, CSc 520 Principles of Programming Languages, Department of Computer Science, University of Arizona
  • ↑ Clause 6.5#2 of the C99 specification: "Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored."
  • ↑ Annex C of the C99 specification lists the circumstances under which a sequence point may be assumed.

Where this page came from

This page was imported from Wikibooks. From “C programming” on Wikibooks, by its contributors, under CC BY-SA 4.0. Changed here: set as a page; navigation and edit links left out; each image under its own licence, credited in its caption.

Nobody has written it yet — it is the source material at a new address, which is why search engines are asked to skip it and why no one earns from it. It is up for grabs: take it on, and it is yours to rewrite and to earn from.

Get each new page of

C programming

Following means one email when a page is added, nothing when a page changes.

Following means one email when a page is added, nothing when a page changes.

Comments

Spinner Logo

Next in C programming

CC BY-SA 4.0
In C, you have already considered creating variables for use in the program. You have created some arrays for use, but you may have already noticed some limitations: - the size of the array must be known beforehand - the size of the array cannot be changed in the duration of your program Dynamic mem
CC BY-SA 4.0
C does not provide direct support for error handling (also known as exception handling). By convention, the programmer is expected to prevent errors from occurring in the first place, and test return values from functions. For example, -1 and NULL are used in several functions such as socket() (Unix
CC BY-SA 4.0
FORTRAN II was touted as a machine-independent language in the 1960s, yet it was essentially impossible to move a FORTRAN program between architectures without some change. In FORTRAN II, you named the device you were talking to right in the FORTRAN statement in the middle of your FORTRAN code. So,
CC BY-SA 4.0
A **string** in C is merely an array of characters. The length of a string is determined by a terminating null character: '\0'. So, a string with the contents, say, "abc" has four characters: 'a', 'b', 'c', and the terminating null ('\0') character. The terminating null character has the value zero.
CC BY-SA 4.0
The header contains prototypes for several functions that deal with mathematics. In the 1990 version of the ISO standard, only the double versions of the functions were specified; the 1999 version added the float and long double versions. To use these math functions, you must link your program with
CC BY-SA 4.0
The **C standard library** is a standardized collection of [header files]( and library routines used to implement common operations, such as input/output and character string handling. Unlike other languages (such as COBOL, Fortran, and PL/I) C does not include built in keywords for these tasks, so