'return' for main() termination | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

'return' for main() termination

"return 0; This statement terminates the main() function and returns the value 0 to the calling process. The number 0 generally means that our program has successfully executed. Any other number indicates that the program has failed." Why does the program runs & executes, if we return some random no. like 'return 836' ? In this case, shouldn't we get some error or even a warning??

13th Sep 2020, 1:45 PM
Chinmoy
Chinmoy - avatar
7 Answers
+ 3
You can return anything. The return of a fuction is like a result that the function gives. For example you can make a fuction that takes two numbers and returns their sum int sum (a, b) { return a+b; }
13th Sep 2020, 1:52 PM
Andrei I
Andrei I - avatar
+ 1
Thank You Coder Kitten , your example was really helpful, thanks to you too Andrei I. 😊
13th Sep 2020, 3:31 PM
Chinmoy
Chinmoy - avatar
0
Andrei I But thats for user defined functions, can we do this even for terminating main() function?
13th Sep 2020, 2:10 PM
Chinmoy
Chinmoy - avatar
0
Yes, you can do it with every function including the main one
13th Sep 2020, 2:27 PM
Andrei I
Andrei I - avatar
0
If you're still curious, try accessing a value of a null pointer. E.g: int *p = NULL; printf ("%d\n", *p);
15th Sep 2020, 11:40 PM
LastSecond959
LastSecond959 - avatar
0
LastSecond959 I tried, it shows 'timeout', for this code to work, *p should store some address, if the address is NULL like you mentioned, it cannot find any address and will give a "runtime error" . btw, How is this suppose to help me with what I've asked in question??
16th Sep 2020, 12:18 AM
Chinmoy
Chinmoy - avatar
0
Chinmoy Kumar Hansda It probably depends on where you type the code, I use DevC++ (PC) and it results with runtime error and return value of 3221225477.
16th Sep 2020, 6:23 AM
LastSecond959
LastSecond959 - avatar