2

Preface:

This is probably gonna be marked as a duplicate, and I understand, but I am asking this, because I feel other answers may not be quite as clear. In fact, the closest answers that I have found that make the most sense to me are this one: from 2015 and this one: from 2013, about return 0;

I have very little programming experience--I messed around with Java several years ago, and kinda figured out what I needed to copy and paste to edit the program I was playing with, and I have written a simple program in python, and I played around with Scratch in School. But, I have finally decided to sit down, learn a language, C, and just roll with it.

So, what I would like to ask, is, am I interpreting this right? because, if so, then it can be said there is, for most intents and purposes, no difference between int main(), with no arguments, and int main(void).

Actual Importance:

To summarise my current understanding:

  1. If int main() is used, then you should add at the end return 0;, to signify the programs termination.
  2. Though possible, it is improper to use int main() and not use return 0;, as many compilers may not recognise this.
  3. If int main(void) is used, then it is unnecessary to add return 0;.

In plain English, would this be said as a good, and/or accurate explanation?

Examples:

/*
 * Program example using scanf, printf, and int main() with no variable
 * This program reads a string from the user
 */

#include <stdio.h>

int main() {
        char name[10];                /* number in square brackets is for max characters in name */
        printf("Enter name: ");
        scanf("%s", name);            /* scanf will read input until it sees blank space, then stop */
        printf("Hello, %s!\n", name);
        return 0;
}

vs.

/*
 * Program example using scanf, printf, and int main(void)
 * This program reads a string from the user
 */

#include <stdio.h>

int main(void) {
        char name[10];                /* number in square brackets is for max characters in name */
        printf("Enter name: ");
        scanf("%s", name);            /* scanf will read input until it sees blank space, then stop */
        printf("Hello, %s!\n", name);
}
Code-Apprentice
  • 69,701
  • 17
  • 115
  • 226
  • 5
    `return` is about the return type, not the argument. – Stefan Nov 04 '19 at 22:56
  • 5
    Answer (Yes), though somewhat a matter of semantics, in C a function with empty `()` takes an *indeterminate* number of arguments, while `(void)` specifies *zero* arguments. – David C. Rankin Nov 04 '19 at 22:59
  • 3
    All 3 points in `Actual Importance:` are wrong. 1) You don't need to add `return 0` inside `int main()`. 2) It is ok to use `int main()` without `return 0`. 3) It is not necessary to add `return 0` inside `int main(void)`. `In plain English` In plain English, if `main` returns an `int`, there is no need to write `return 0`. No matter what is inside the parameter list. `int main(/*anything here*/) { /* always returns 0 by default */ }` – KamilCuk Nov 04 '19 at 23:01
  • 1
    A good start would be to compile both sets of code and look at it in assembly. – Xofo Nov 04 '19 at 23:03
  • 1
    You are confused about argument list, but you asking about function return value. You should pick one thing. – StaceyGirl Nov 04 '19 at 23:09
  • @KamilCuk That makes a little more sense, so my understanding is wrong. – Matthew T. Scarbrough Nov 04 '19 at 23:10
  • 3
    Hm.. Maybe the standard will clear things up. The exact rule about this is [C11 5.1.2.2.3p1](https://port70.net/~nsz/c/c11/n1570.html#5.1.2.2.3p1): `If the return type of the main function is a type compatible with int, .... reaching the } that terminates the main function returns a value of 0`. If it returns `int` and the execution reaches the closing `}` in `main`, it acts as `return 0`. – KamilCuk Nov 04 '19 at 23:14
  • 1
    as @Stefan said it's not about the arguments in this situation. Maybe you're confused about `void main()` or `void main(void)` concept. – sushibossftw Nov 04 '19 at 23:25
  • 2
    There are very strong stylistic differences. `int main()` looks like you are either writing C++, or you are living in 1987. – William Pursell Nov 04 '19 at 23:29
  • 2
    @WilliamPursell Taking input in the form of questions like that is very 1987. `WOULD YOU LIKE TO PLAY A GAME? (Y/N)` – tadman Nov 04 '19 at 23:31
  • 1
    I wonder if in 2059 there will still be new threads on this topic – M.M Nov 05 '19 at 00:36
  • I VTC as unclear because the title doesn't match the question body (in reality there is a difference between the two, but that difference has nothing to do with return statements) – M.M Nov 05 '19 at 00:39

3 Answers3

3

If int main() is used, then you should add at the end return 0;, to signify the programs termination.

Mostly true. This is good practice, although not strictly necessary, as we'll see.

Though possible, it is improper to use int main() and not use return 0;, as many compilers may not recognise this.

Mostly false. For some time now, and as a special case unique to a function named main(), if you do not supply a return statement, the compiler is required to arrange that main implicitly return 0.

An old compiler may still require the return 0;, and behave badly if it is omitted -- although the malfunction is typically minor. Also a compiler so old as to not comply with this requirement will certainly have many other, more significant compatibility issues to worry about.

If int main(void) is used, then it is unnecessary to add return 0;.

Correct, but misleading. There is no difference whatsoever between int main() and int main(void) in this regard. It is also true (per the above) that if int main() is used, it is unnecessary to add return 0.

Steve Summit
  • 29,350
  • 5
  • 43
  • 68
  • I would say that it's not good pratice. It's just noise. – klutt Nov 05 '19 at 00:25
  • 1
    to be specific instead of "Mostly" : it was required in C89 and is redundant in C99 and later – M.M Nov 05 '19 at 00:37
  • @M.M And if you ever need to compile your code on a C89 compiler (not very likely) then just activate warnings (which IS good practice) – klutt Nov 05 '19 at 18:15
1

The difference is that int main() doesn't specify a prototyped declaration for main. As a definition of the function, the two are equivalent, and define the function in mutually compatible ways. However, int main() introduces less compile-time information into the scope.

In C (unlike C++), main is allowed to recurse. Thus we can consider the following silly program:

int main(void)
{
   main(42);
}

because main is declared completely, the main(42) call is a constraint violation, requiring a diagnostic. If we change the program to:

int main()
{
   main(42);
}

then the function hasn't changed: it's still one that takes no arguments. But that information is not declared as part of the main identifier's type information, and so the call doesn't require a diagnostic. (If translated and executed, it invokes undefined behavior; but that may happen even if there is a diagnostic.)

This is an old style, before C had function prototype declarations, which were first standardized in 1989's ANSI C and in widespread use before that.

Kaz
  • 48,579
  • 8
  • 85
  • 132
0

The latest standard allows you to ommit the return 0 statement. As for the protype of main(), there are 3 equally variants:

int main()
int main(void)
int main(int argc, char* argv[])

An empty parameter list in C means that you can pass as many parameters as you want but the function will not care about any of them. Specifying a void parameter list means that you explicitly have to pass no arguments or you will get an error.

While the standard only explicitly accepts versions #2 and #3, version #1 also works because the OS passes the arguments for version #3 and your function will not care about them.

Hope my answer was clear enough.

DarkAtom
  • 2,034
  • 6
  • 17
  • So, if I am understanding that right, if you add `void`, then you cannot add arguments when you use gcc, make, etc., that specifically affect the program in some way, shape, or form, presently above my understanding? – Matthew T. Scarbrough Nov 05 '19 at 00:00
  • "An empty parameter list in C means that you can pass as many parameters as you want but the function will not care about any of them." - this is not correct according to the standard – M.M Nov 05 '19 at 00:34
  • @M.M it is UB, but this is what happens in practice. – DarkAtom Nov 07 '19 at 20:02