2

So I was writing a C function but I had a hard time debugging, and eventually I found the problem was an 'empty' if statement. However, I suspect it isn't empty, as there were no curly braces, and it might just pass newlines until it finds a line of code. If this line is a return, I might think the return is now 'inside' the if. If this is the case, what would happen if the 'if' condition is false? What would the function return? In my original function, it would just return 15 or 16 (when the condition was false) instead of the expected 0 or 1. I leave here a simplified code of what I'm talking about. In this case something stranger happens. It seems it will always return 1 no matter the 'if' condition is satisfied or not. And if we uncomment the 'return 3;' the code works as expected given that the first return would be inside the 'if'. What exactly is happening here?

#include <stdio.h>
int testIf(void){
    if(0)

    return 1;
    //return 3;
}
int main(void){
    printf("%d\n", testIf());
    return 0;
}


M364
  • 23
  • 3
  • 1
    You're correct. It isn't "empty". It's `if(0) return 1;`. It's good style to always use curly braces (even for simple one-line conditionals) ... but it's not *required*. Read this: [Why is it considered a bad practice to omit curly braces in C/C++?](https://www.tutorialspoint.com/why-is-it-considered-a-bad-practice-to-omit-curly-braces-in-c-cplusplus) – paulsm4 Oct 03 '20 at 17:47
  • Does this answer your question? [When can I omit curly braces in C?](https://stackoverflow.com/questions/14901919/when-can-i-omit-curly-braces-in-c) – Ava_Katushka Oct 03 '20 at 17:49
  • @Ava_Katushka I doubt the question you propose is a dupe, because it doesn't cover the fact that this method doesn't actually return anything. – gsamaras Oct 03 '20 at 17:58
  • This is the right answer. https://stackoverflow.com/questions/4260048/c-function-defined-as-int-but-having-no-return-statement-in-the-body-still-compi – Ramy Ibrahim Oct 03 '20 at 18:03
  • I appreciate your suggestions. I will have a look at them. The answer I was looking for was, however, the undefined behaviour because of the function not returning anything. That also explains why I got different results in my original function. Edit: Yes, the one provided by Ramy Malak looks like what I wanted, though I didn't know it was that at first. Thanks. – M364 Oct 03 '20 at 18:04

2 Answers2

2

Note that in the C grammar, a sequence of whitespace, such as several newlines, is equivalent to a single whitespace character.

The code (with the commented return 3) has undefined behaviour because a non-void function does not return a value for all execution paths. (There's an exception for main which you don't want to know about.)

Your compiler should have told you about this. Did you enable all warnings?

Jens
  • 69,818
  • 15
  • 125
  • 179
1

Then the very next line of code is considered to be its body.

So this:

if(0)

    return 1;

has the same effect with this:

if (0) {
    return 1;
}

Now as for your, method, because the condition of the if statement is hardcoded to 0, it will always evaluate to false, and the return statement will not execute.

return 3; won't execute either, since it's commented. So your method will terminate without executing any return statement, despite the fact that it's defined to return an int.

This code invokes Undefined Behavior (UB), because we cannot tell what value it will return for sure, because it lacks an effective return statement. To you it happened to be 1, today. In my laptop it may be garbage. This code is, therefore, wrong.

I strongly recommend that you enable your compiler warnings, e.g. by passing Wall and/or Wextra flags when compiling with GCC, in order to get warned for such logical errors.

Read more in Function returns value without return statement.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • I also asked what happens when the condition is not satisfied, as in this example. Why will it still return 1? – M364 Oct 03 '20 at 17:48
  • 'So your method will terminate without returning anything'. Well, that's not the case, it certainly returns something. – M364 Oct 03 '20 at 17:53
  • 1
    @M364 Nope - it doesn't return anything. The caller just assumes that something was returned and reads whatever there is in the location where the function should have returned a value... but it was **not** returned by the function. We simply can't tell what is present at that location. Therefore the code has undefined behavior and may print anything or even crash – Support Ukraine Oct 03 '20 at 17:55
  • @M364 I updated my post to answer all of your questions, hope this helps. – gsamaras Oct 03 '20 at 17:57