1
$\begingroup$

I'm just a beginner, I've started learning programming, CS, math, and Unix. I know how to code in Pascal and also study C. In the university a professional programmer, who teaches coding, said that literally everything in C is a side effect.

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.(from the internet)

And he says that even printf() has a side effect. E.g.

printf('Hello, world\n');

returns 13 and also prints "Hello, world", or that there's no "assignment statement" in C, only "assignment operator/operation", so we can do thing like this:

x = (y = 1) - 3;

which actually causes a side effect. It's not the same as

a := b;

in pascal. Or this:

a[foo(x)] += y;

is not the same as

a[foo(x)] = a[foo(x)] + y;

because foo(x) might return different numbers.

It confuses me. I know that side effects are not usually good because it muddles readability of the code and sometimes makes it behave unexpectedly. Please, clarify what actually happens here.

$\endgroup$
6
  • 1
    $\begingroup$ Reading a non-volatile value: No side effect. Calling a function without side effects: No side effect. (void) (x + y/3): No side effect. $\endgroup$
    – gnasher729
    Commented Sep 15, 2022 at 7:44
  • 1
    $\begingroup$ x = y+3: changing x is a “side effect”. You’d be rather stuffed without side effects. So “not usually good” is total nonsense. $\endgroup$
    – gnasher729
    Commented Sep 15, 2022 at 7:46
  • 1
    $\begingroup$ there's no "assignment operator" in C I beg to differ: 6.5.16 Assignment operators. There is no assignment statement I remember Algol languages including Pascal to have. $\endgroup$
    – greybeard
    Commented Sep 15, 2022 at 7:56
  • 1
    $\begingroup$ clarify what actually happens here What's actually happen in a correct interpretation/execution of a program in a programming language is part of the specification of that language, of its reference/standard document. What happens between lecturer and student is way more complicated. The problems may start with those operations are all side effects, which are changes in the state of the execution environment being intended as a definition, without you quoting the lecturer to explicitly say so. $\endgroup$
    – greybeard
    Commented Sep 15, 2022 at 8:05
  • $\begingroup$ @greybeard Yes, I meant "assignment statement". Because in Russian language literally everyone calls any statement "operator" (and everyone understands what they're talking about). In English it's a different word. So I meant there's no assignment statement like in Pascal, but only operation, or, "operator". Just a little confusion. $\endgroup$ Commented Sep 16, 2022 at 8:23

1 Answer 1

2
$\begingroup$

I found the following definition, which IMO is relevant:

"A side effect is when a function relies on, or modifies, something outside its parameters to do something. For example, a function which reads or writes from a variable outside its own arguments, a database, a file, or the console can be described as having side effects."

The operations listed fall in this category.


As regards the assignment, I would rather say that the C assignment is an operator, as it creates an expression with an rvalue (like c= a + b, c= (a = b) has meaning). Pascal's assignment is an operation. I would not say that assignment in itself has (or is ?) a side effect.

$\endgroup$
4
  • $\begingroup$ That's not the definition that the C Standard or the C++ Standard are using. Since the question is about C, the relevant definition is from 5.1.2.3 of the C Standard: "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". $\endgroup$
    – gnasher729
    Commented Sep 15, 2022 at 10:34
  • $\begingroup$ @gnasher729: the two definitions are perfectly compatible, that's what I meant. $\endgroup$
    – user16034
    Commented Sep 15, 2022 at 10:46
  • $\begingroup$ Yes, I meant "assignment statement". Because in Russian language literally everyone calls any statement "operator" (and everyone understands what they're talking about). In English it's a different word. So I meant there's no assignment statement like in Pascal, but only operation, or, "operator". Just a little confusion. I heard about this translation mistake but just forgot :D So, there's no assignment in $\endgroup$ Commented Sep 16, 2022 at 8:26
  • $\begingroup$ @GeorgeGlebov: misplaced comment I guess. $\endgroup$
    – user16034
    Commented Sep 16, 2022 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.