-1

I was just trying to see what would happen, if I use return 0 statement with void main(). I was expecting that the compiler would give an error but it just gave a warning and successfully executed the program but at last, returned some non zero value, now I have doubt why the program executes without an error and then also return some value when I have used void main()?

#include<stdio.h>

void main()
{
    printf("Hello World\n");
    return 0;
}
Student
  • 769
  • 1
  • 6
  • 11
LocalHost
  • 823
  • 1
  • 6
  • 17
  • 1
    https://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c – jaudo Jul 24 '18 at 19:02
  • This program is not valid C program. Any C compiler that compiles it, does that on "grabage in - garbage out" principles. The behavior is undefined. So, whatever you "successfully executed " has nothing to do with C language. – AnT Jul 24 '18 at 19:06
  • 1
    @AnT: It is not *undefined* behaviour. The C99 specification calls out that conforming implementations are permitted to do this, and requires that it be *implementation defined* behaviour. If you're not clear on the difference, do a web search and all will become clear. – Eric Lippert Jul 24 '18 at 19:11
  • @AnT yeah some compilers allow `void main` and there's nothing wrong with that, it's just a shortcut if you don't care about the return value. Eric is right – Konrad Jul 24 '18 at 19:15
  • @Konrad: In particular, Microsoft compilers allow void main, but I do not know if they allow value returns in a void main. Whether there is something *morally* wrong with it is debatable, but it is *de jure* allowed by the specification. :-) – Eric Lippert Jul 24 '18 at 19:20
  • 1
    @Eric Lippert: Absolutely wrong! "Permitted to do" *what* exactly? `return 0` in a `void` function is a **constraint violation** in all C compilers. No exceptions. Every C compiler is required to issue a diagnostic for this code. After that the behavior is undefined. Not implementation defined, but explicitly *undefined*. – AnT Jul 24 '18 at 19:32
  • @user9121710 "but it just gave a warning" --> Append the warning to the question. That will improve the question. – chux - Reinstate Monica Jul 24 '18 at 19:40
  • As you know, returning a value from a `void` function is against the rules. Now that you've experimented, I suggest these additional experiments: (1) `float a = 1, b = 0, c; c = a / b;` Dividing by 0 is illegal. Do you get a floating point exception? (2) `int a = 80000, b=a, c; c = a * b;` The product does not fit in 32 bits; the behavior is undefined. But what actually happens? (3) Go the controlled street intersection nearest your house at 3:00 tomorrow morning. Wait until the light is red. Cross the street. Do you get hit by a car? Does a policeman give you a citation for jaywalking? – Steve Summit Jul 24 '18 at 19:55

4 Answers4

3

i have doubt why the program executes without a error and then also return some value when i have used void main()

The C standard says that the right thing to do is int main, and that a conforming implementation is permitted to allow other signatures on main if it so chooses.

Apparently your implementation chooses to allow this non-standard usage, and that the behaviour it exhibits is a warning at compile time followed by the observed behaviour at runtime.

But you already knew that. The compiler does what the compiler does because the compiler authors wrote it that way. If you want to know why they made those choices, ask them. Since you have not said what compiler you are using, we have nothing more to go on.

Eric Lippert
  • 612,321
  • 166
  • 1,175
  • 2,033
  • The question is not about `void main`, but rather about `return 0` in a `void` function. – AnT Jul 24 '18 at 19:33
  • @AnT Even if `return 0` in a `void` function is permitted or not, `main()` has additional specifications that may differ. Assuming the question "Using return 0 with void main()?" is about `main()` and not functions in general is reasonable. – chux - Reinstate Monica Jul 24 '18 at 19:37
  • @chux: While `main` does have "additional specifications", none of them allow this. So, no, "additional specifications" of `main` have nothing to do with this. – AnT Jul 24 '18 at 19:39
3

Using return 0; in a function that returns void is a constraint violation:

6.8.6.4 The return statement

Constraints

1 A return statement with an expression shall not appear in a function whose return type is void. A return statement without an expression shall only appear in a function whose return type is void.

C 2011 online draft

The compiler must issue a diagnostic for a constraint violation (the warning you saw during compilation) - however, the compiler may continue to translate the program and generate an executable (the difference between a warning and an error, and whether it halts translation, is up to the implementation).

Secondly, unless your compiler's documentation explicitly lists void as a valid return type for main, using void main results in undefined behavior - at that point, the compiler is not required to handle the situation in any particular way.

Usually, trying to figure out how you got a specific result from undefined behavior is a waste of time, since the behavior may not be repeatable. In this case, you told the compiler main wasn't going to return a value and it generated the machine code accordingly. It's likely that the register used for that return value was overwritten as soon as main returned.

Community
  • 1
  • 1
John Bode
  • 106,204
  • 16
  • 103
  • 178
2

Your code is not a valid C code. C language does not allow you to use return 0 in a void function. It is a constraint violation, i.e. what we colloquially call an error. Every conforming C compiler is required to issue a diagnostic message for this code, after which the behavior of your code is undefined from the point of view of standard C language.

According to your description, your compiler did issue a diagnostic message for this code, as required by the language specification. After that, whatever behavior you observed from the compiled code might (or might not be) defined by your specific compiler, but it is no longer defined by the language itself.

AnT
  • 291,388
  • 39
  • 487
  • 734
1

This return value of main is typically utilized the operating system where the program runs to determine if there was an error during execution or not, typically the value 0 is success and other values are interpreted as an error by convention.

Jay
  • 3,017
  • 1
  • 22
  • 37