1

why the following code is returning the output even if I am not returning the output of variable a to the previous function.

int fact(int n) { int a; if (n <= 1) return 1; else a = n*fact(n-1); } int main() { int c=fact(5); printf("%d",c); } 
1
  • 3
    Welcome to the wonderful world of undefined behavior. In short, if you declare a function to return a value, you must return a value, anything else is wrong. Commented Jul 24, 2018 at 5:32

1 Answer 1

2

Quoting C11, chapter §6.9.1

If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.

So, your program exhibits undefined behavior.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.