12

I was reading this article and they use the following example to explain undefined behaviour:

// PROGRAM 1
#include <stdio.h>
int f1() { printf ("Geeks"); return 1;}
int f2() { printf ("forGeeks"); return 1;}
int main() 
{ 
  int p = f1() + f2();  
  return 0; 
}

However, it seems to be about the order in which subexpressions are evaluated, and according to C standard (Annex J.1), it is an unspecified behaviour and not an undefined behaviour:

Unspecified behavior: The order in which subexpressions are evaluated and the order in which side effects take place, except as specified for the function-call () , &&, || , ? : , and comma operators (6.5)

Since I am very new to reading official specifications, I'm wondering if I did misunderstand the example or the documentation. I know this may seem very pedantic but I'm interested into learning these advanced topics the right way.

  • 1
    "*example to explain undefined behaviour*": The code shown does *not* invoke "Un*defined* Behaviour". – alk Jul 21 '17 at 15:53
  • What is the question again? The above example seems undefined in the C specification. That means in different compilers, you may get different results. – parallel highway Jul 21 '17 at 15:55
  • 8
    @parallelhighway: Wrong. It explicitly is **unspecified**, as OP assumes, not undefined behaviour. – too honest for this site Jul 21 '17 at 15:59
  • @parallelhighway I edited my question. Actually, they use this example to explain undefined behaviour, however according to the specification this should be an unspecified behaviour. –  Jul 21 '17 at 15:59
  • The article links to wikipedia articles but then confuses terms. – coredump Jul 21 '17 at 15:59
  • 1
    This phrase is incorrect, according to @Olaf: "The reason for undefined behavior in PROGRAM 1 is, the operator ‘+’ doesn’t have standard defined order of evaluation for its operands." – anol Jul 21 '17 at 16:00
  • 1
    @Olaf you are correct, I got confused in the article as well. The article confuses the terms. – parallel highway Jul 21 '17 at 16:03
  • 3
    @parallelhighway: Wikipedia is a bad resource for standard interpretation. There is only one authoritative resource for standard behaviour: the standard. – too honest for this site Jul 21 '17 at 16:14

1 Answers1

6

The link you have provided in the question is given wrong example of undefined behavior. Evaluation of f1 and f2 in f1() + f2() will be unspecified. Note that standard says about side effects along with the order of evaluation

The order in which subexpressions are evaluated and the order in which side effects take place [...]

Side effects (output to the stdout) in the evaluation of f1 and f2 are not related and they are not causing any undefined behavior.

This is no different than the example below

int a = 1;
int b = 1, c;

c = a + b;

Order of evaluation of a and b is unspecified in the expression a + b.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • There is a sequence point before and after each function call, so I don't think your modified program has UB either. – zwol Jul 21 '17 at 16:27
  • @zwol; Yes. But which function will be evaluated first? Suggested reading: [Is f()+g() undefined or merely unspecified?](https://stackoverflow.com/q/3951017/2455888) – haccks Jul 21 '17 at 16:28
  • 1
    Merely unspecified, just like in the original. Your link agrees with me. – zwol Jul 21 '17 at 16:29
  • @zwol; I think I did a mistake. You are right actually. – haccks Jul 21 '17 at 16:33
  • 5
    Writing to stdout is a side-effect IMHO, but the fact that there are side effects in `f1` and `f2` does not produce Undefined Behaviour. – rici Jul 21 '17 at 16:57
  • @rici; Yes. My wording was incorrect. Thanks for pointing it out. – haccks Jul 21 '17 at 18:05