Recently I submitted my code for this QUES and this was my Solution.
Why is the return statement inside while loop causing Runtime Error?
Any help will be appreciated.
Recently I submitted my code for this QUES and this was my Solution.
Why is the return statement inside while loop causing Runtime Error?
Any help will be appreciated.
If you don’t return 0
(in C - C++ supplies it automatically), you’ll get a stern compiler warning:
[simon@simon-laptop][06:38:17]
[~/devel/hackerrank/otherpeoples]>gcc hs_4419-TEST.c -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
hs_4419-TEST.c: In function ‘main’:
hs_4419-TEST.c:9:13: warning: ‘return’ with no value, in function returning non-void
return;
^~~~~~
hs_4419-TEST.c:3:5: note: declared here
int main(void) {
^~~~
hs_4419-TEST.c:7:9: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&num);
^~~~~~~~~~~~~~~~
In C or C++, the main function is of ‘int’ type thus it requires you to return an integer value in the block of the main function. It also acts as a signal where when the program exits with 0 code, it means the program has been executed successfully. While any non-zero value tells that an error has occurred.
You can check this StackOverflow link for further clearance.
Chech this Link
What I understood is that if we write return 0 then it indicates that no error has occured whereas if we leave it blank or write something else then it indicates that it’s returning some error. Please correct me if I’m wrong.
Thanks a lot @ssjgz and @karnikkanojia
return;
in a function declared to return non-void
is Undefined Behaviour. Frankly, it shouldn’t even compile. Just don’t do it
While writing code u have to adhere to certain conditions for its successful execution. We cannot bypass these conditions such as function with return type must return response of said return type