-1

I am just starting to learn C and am bit confused about use of return statement and its importance in C language.

Here's my code snippet. Is this syntactically correct or do we have to return any numerical value 0 or 1 while using int main()?

#include<stdio.h>

int main()
{
        int i;


        for(i=0;i<10;i++)
              printf("\n Noooooooooo");

        return ;      
 }
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • 2
    IMO, it is *semantically* wrong.(and: it should *at least* be`int main(void){...}` – wildplasser Apr 29 '18 at 16:11
  • 2
    If the compiler does not complain about syntax errors, then it is syntactically correct C. whether it is semantically correct, or makes any sense is another question. – Paul Ogilvie Apr 29 '18 at 16:12
  • For `main()` it's either all or nothing. Just `return` without argument is none of them. – alk Apr 29 '18 at 16:12
  • @alk and others, that does not make it syntactically incorrect. – Paul Ogilvie Apr 29 '18 at 16:13
  • @PaulOgilvie: I didn't say or meant to imply this. – alk Apr 29 '18 at 16:15
  • For any function you declare as returning an `int`, the `return` statement must return an `int` expression. You can only use a `return` statement without an expression if the function is declared as `void`. – David R Tribble Apr 29 '18 at 16:27
  • @DavidRTribble: The expression in the `return` does not need to be an `int` expression. It needs to be assignable to an `int`. 6.8.6.4 3 says the value is converted as if by assignment. For example, the expression in the `return` statement could be a `double`. – Eric Postpischil Apr 29 '18 at 17:47

2 Answers2

3

Per the C 2011 standard (draft N1570), clause 6.8.6.4, paragraph 1:

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

Your return statement does not have an expression and appears in the main function whose return type is int, not void. Hence your program violates the constraint.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
0

First of all let's clarify that because you refer to the main function, obviously (but not for all seems) this answer assume that you are referring to an hosted environment, because for a freestanding environment (see ISO/IEC 9899:2011 "§5.1.2.1 Freestanding environment") there are no requirements at all and the name and type of the function called at program startup are implementation-defined.

Going back to the hosted environment, the standard that defines C language, in its more recent version ISO/IEC 9899:2011, for C11, describe the requirements for the program startup function in an enough strict way (§5.1.2.2.1 Program startup).

The points to be respected are essentially:

  • The main function must return an int.
  • Only 2 variation of it are allowed:
    1. Without parameters as in : int main(void) { /* ... */ }
    2. With 2 parameters a count of argument elements and the array of arguments as in : int main(int argc, char *argv[]) { /* ... */ }

Some implementation variation are allowed, but only as different representation of same parameters (i.e. instead of char *argv[] the equivalent char **argv, or some typedef for specific types resolving in an int type for argc).

The very common definition void main() { /* ... */ } allowed in some major compiler producers (most often MS) are not strictly C99-C11 compliant.

In fact the use of empty parenthesis in function declarators is an obsolescent feature that could be removed in future standard revision as reported in "§6.11 Future language directions" (thanks to have reported me the imprecision). In fact many compilers C99-C11 compliant issue a warning.

So considering the before mentioned obsolescence would be a good habit to use a more standard declarations.

In conclusion your declaration is wrong because:

  1. return; You return no value
  2. Optionally consider that main() miss the void (strict C99-C11)

What you return depends on what you want return. Let only say that a general convention commonly accepted is to return from main the value 0 (zero) for normal termination (no errors), any other value denotes an execution error.

Anyway for the sake of precision, for those who want go inside specs, I report below the exact text from specific paragraph

5.1.2.2.1 Program startup

The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int (emphasis mine, please see below the definition of the term shall in ISO document) and with no parameters:

int main(void) { /* ... */ } or with two parameters

(referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

int main(int argc, char *argv[]) { /* ... */ }

or equivalent;(see note below) or in some other implementation-defined manner.

The note for equivalent type says:

NOTE: Thus, int can be replaced by a typedef name defined as int, or the type of argv can be written as char ** argv, and so on

Last to remove any doubt about the meaning of the term "shall" the ISO/IEC 9899:2011 specification clearly define it on paragraph §4.0 Conformance at subpar. 1:

4.0 Conformance

1 In this International Standard, ‘‘shall’’ is to be interpreted as a requirement on an implementation or on a program; conversely, ‘‘shall not’’ is to be interpreted as a prohibition.

Frankie_C
  • 4,764
  • 1
  • 13
  • 30
  • "The main function must return an int" - stated that absolutely is plain wrong as the rest about the signature. It is not even exactly correct for a hosted environment. For a freestanding, it completely depends on the environment. Additioinally, a function definition may use empty parenteses for an empty parameter list. It is not disallowed, but just an obsolescent feature (see 6.11.6). – too honest for this site Apr 29 '18 at 18:35