0

I don't understand the output from this code:

#include <stdio.h>
long func(long pass)
{
    long ret;
    long i = pass;

    if (i == 6)
    {
        printf("i=%ld\n",i);
        return i;
    }
    printf("ended\n");
}

void main()
{
    int j;
    long it = 0;
    for(j = 0; j < 12; j++)
    {
        printf("%ld\n",func(it));
        it++;
    }
}

The output shows "ended" and "6" every time except when it reaches i=6, that time it prints i=6 and 6.

WHY? It shouldn't be going inside i == 6 every time right?

4
  • 4
    func doesn't return anything unless pass == 6. That's undefined behavior. Commented Nov 19, 2014 at 2:22
  • 4
    most modern compilers would see there is not every path ends with a return(value) statement and should have raised an error/warning message. Suggest enabling all warnings/errors (for gcc use -Wall) and fixing the problem before trying to run. Commented Nov 19, 2014 at 2:34
  • Speaking to the title of the question, "Function returning wrong value": When i!=6, what would you consider to be the right value? Commented Nov 19, 2014 at 4:19
  • gcc gives the following warnings: warning: control reaches end of non-void and warning: return type of 'main' is not 'int'. Read that first Commented Nov 19, 2014 at 8:00

4 Answers 4

3

When i is not equal to 6, you run off the end of the function without a return statement, which gives you undefined behavior -- anything might happen.

In this case it probably just returns whatever happens to be in the hardware return register at the end of the function, which might just be the constant 6, as the compiler might have put it there for the comparison. A different compiler (or even the same compiler run on a different day) might give you a different result.

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

4 Comments

In C there is no UB if the calling code does not access the returned value (unlike C++)
@MattMcNabb: But OP's code is accessing the returned value.
@AndyG I was responding to first paragraph of Chris's answer
@MattMcNabb: I see. Apologies for the confusion.
2

When i == 6 is not true , then execution reaches the end of func without encountering a return statement. This causes the function to return an indeterminate value.

Then the line printf("%ld\n",func(it)); accesses the indeterminate value, which causes undefined behaviour.

Comments

0

the control reaches the end of long func(long) because return statement is inside the if condition which fails if 'i' is not equal to 6.

Comments

0
When i is not equal to 6 it throws the warning as ::  warning C4715: 'func' : not all control paths return a value
and returns an indeterminate values and printf("%ld\n",func(it)); prints that value like,
ended
1
ended
1
ended
1
ended
1
ended
1
ended
1
i=6
6
ended
6
ended
6
ended
6
ended
6
ended
6

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.