0

Read all this to understand my problem: In my collage test there was a question like this:

What is the error in this code:

#include<stdio.h>

int main(){

printf("Hello World");

}

Answer of above code: compiling Error

But This is what I got when I try it practically:enter image description here

As per books we have to return 0; when we use int main()
can any one give perfect or understandable Explanation.

the busybee
  • 10,755
  • 3
  • 13
  • 30
  • `main` is a special case and will return 0 if you don't specify any return value. This won't work for any other method. – Voo Dec 10 '20 at 10:47
  • 4
    Does this answer your question? [Why does the main function work with no return value?](https://stackoverflow.com/questions/19293642/why-does-the-main-function-work-with-no-return-value) – kaios Dec 10 '20 at 10:49
  • 1
    Yeah, it sounds like your college test is wrong - `int main()` is a special case. You actually have to write return statements every function with a non-void return type _except_ `main`. – Useless Dec 10 '20 at 10:53
  • 2
    Your college is marking down incorrectly. `main()` is the *only* non-void function which does not need to explicitly return a value. – Weather Vane Dec 10 '20 at 10:53
  • The error in the code is the function definition. It should be `int main(void)` but it should not cause a compiler error. – Weather Vane Dec 10 '20 at 10:55
  • Even for functions beside main, the actual language specification doesn't deem this uncondionally a violation. And it doesn't deem it diagnosable either. So no error is guaranteed. – StoryTeller - Unslander Monica Dec 10 '20 at 10:55
  • "Answer of above code: compiling Error" We are surprised and disappointed to learn that your college test gives you incorrect answers... no, not really. That's what colleges do. – n. m. could be an AI Dec 10 '20 at 10:56
  • @WeatherVane that's debatable. The C++ standard itself has several examples of `int main()`. They are not normative, but I'm not gonna be holier than the Pope. – n. m. could be an AI Dec 10 '20 at 10:58
  • @n.'pronouns' m. I was looking for anything *else* that could be wrong... I had not looked at the linked screen shot. – Weather Vane Dec 10 '20 at 11:01

1 Answers1

0

Absence of return statement will never raise compiling error. Especially for main function, that handles differently. If it has type int but have doesn't return anything, a program acts like main returns 0.

Theoretically, it can be compiler-dependent, but in you case, it's likely an error in test. You should return 0 but not doing that is not a compiling error.


UPD: as @EricPostpischil mentioned in comments, compilers don't raise an error because they have a mode set by default that allows to compile non-standard code, so question in your test is incomplete. It has to specify compiler you are using etc.

Vlad Havriuk
  • 1,291
  • 17
  • 29
  • Means ***main*** Function acts like return 0; – Nisheet Patel Dec 10 '20 at 11:15
  • @NisheetKumar yes – Vlad Havriuk Dec 10 '20 at 12:09
  • 2
    Re “Absence of return statement will never raise compiling error”: This is not quite true. First, if a compiler detects that the return value of a function is used in a case when the function does not return a value, it can report an error because the C standard does not impose any requirements on behavior in this case, per C 2018 6.9.1 12. – Eric Postpischil Dec 10 '20 at 12:41
  • 2
    Second, in the absence of the preceding situation, a compiler conforming to the C standard may be required to accept a program with an absent `return`. However, compilers commonly have a mode in which they treat warnings as errors, [as shown here](https://godbolt.org/z/shfdrT). In this mode, the compiler is not completely conforming with the C standard, but it is a feature commonly used by good programmers and ought to be mentioned as an option. – Eric Postpischil Dec 10 '20 at 12:42
  • @EricPostpischil thanks for reply, I edited my answer – Vlad Havriuk Dec 10 '20 at 13:44