Wow. This is a spectacularly misleading result.
No, you can not return multiple values like this in C. A function can have at most one single return value. You can't use a comma to return multiple values like this.
But, if you can't do it, then why did it seem to almost work? That's a rather complicated question, that it probably won't be possible to completely answer here.
Your program has two, completely unrelated problems. The first problem, which isn't too serious, is the line
return a, b, c;
at the end of function sum
. In English, this line basically says, "Take the value a
and throw it away, then take the value b
and throw it away, then take the value c
and have it be the actual return value." (See What does the comma operator do? for more about this comma operator.)
And then the second, much more serious problem is the line
printf("%d %d %d", x);
in main
. Your compiler should have warned you about this problem; mine says warning: more '%' conversions than data arguments
. You're telling printf
, "I'm going to give you three int
values to print". But you lied, because you're then giving it just one int
value, x
. So printf
is blindly reaching for the other two values you promised, and getting... what?
By a reasonably incredible coincidence, when printf
reaches out to fetch the values you didn't pass, it is somehow grabbing... one of the values that got thrown away by the poorly-written return
statement back in function sum
. I'm not even sure how this happened, although when I tried your program on my computer, I got a similar result.
This may be too obvious to mention, but: this result is not guaranteed, it's not something to depend on, and it's not even something to learn from. If you want to return multiple values from a function, there are other ways to do that, as this question's other answers have suggested.