As noted in comments and in the near duplicate question (Is this undefined behaviour in C? If not, predict the output logically.), the results from both these programs are not fully defined by the C standard — they produce 'unspecified behaviour' because the order of evaluation of the terms in the return
statement of the first and the printf()
call in the second is not specified by the language and is at the whim of the compiler. (Note that it is 'unspecified behaviour' rather than formally 'undefined behaviour' — see the discussion in the comments. It is not 'implementation defined' behaviour either; the implementation is not required to document what it does.)
That means that the results given by any implementation are 'implementation specific' in the terms of the question. Different implementations may legitimately produce different answers.
The first program is:
#include <stdio.h>
int f(int *a,int *b,int c) {
if(c == 0) return 1;
else {
*a = *a + 1;
*b = *b - 1;
c = c - 1;
return (*a + f(a,b,c) + *b);
}
}
int main() {
int a = 3,b = 3,c = 3;
printf("%d\n",f(&a,&b,c));
return 0;
}
The problematic part is return (*a + f(a,b,c) + *b);
because *a
could be evaluated before or after the recursive function call, and f()
modifies the values that a
and b
point at so the order of evaluation matters. Different compilers could legitimately produce different results. The same compiler could produce different results at different times, or under different optimization flags, or as it chooses. Both *a
and *b
could be evaluated before the call, or after the call, or either one could be evaluated before and the other after — and that just names 4 plausible alternatives.
The second program is:
#include <stdio.h>
int f(int *x,int *y,int *z) {
*x = *x + 1;
*y = *y + 1;
*z = *z + 1;
return 0;
}
int main() {
int a=0,b=0,c=0;
printf("%d %d %d\n",a,f(&a,&b,&c),b);;
return 0;
}
The problem is that the sequence in which a
, f(&a, &b, &c)
and b
are evaluated for the call to printf()
is not specified by the standard and you get different results depending on the sequence chosen by the compiler. You showed that GCC and Clang produce different results — both those results are acceptable according to the standard, and so are plenty of others.