when do we need of return o; in C

Thread Starter

vead

Joined Nov 24, 2011
629
Hello
why do we use return 0; at end of program and when do we need of return 0; in program.?
while searching I found that main function of type int therefore it should be return integer value. but I don't understand why it should be 0, it may be any value 1, 2, 3...etc
I have compiled both programs with return 0, and without return 0; but result is same . it display same message .
Program with return 0;
Code:
#include <stdio.h>            // header file //
int main(void)                //start main program//  
{
printf("All about circuit forum"); // print message//
return 0;
}
Result All about circuit forum
program without return 0;
Code:
#include <stdio.h>            // header file //
int main(void)                //start main program//  
{
printf("All about circuit forum"); // print message//
}
Result All about circuit forum
 

joeyd999

Joined Jun 6, 2011
5,234
Hello
why do we use return 0; at end of program and when do we need of return 0; in program.?
while searching I found that main function of type int therefore it should be return integer value. but I don't understand why it should be 0, it may be any value 1, 2, 3...etc
I have compiled both programs with return 0, and without return 0; but result is same . it display same message .
Program with return 0;
Code:
#include <stdio.h>            // header file //
int main(void)                //start main program// 
{
printf("All about circuit forum"); // print message//
return 0;
}
Result All about circuit forum
program without return 0;
Code:
#include <stdio.h>            // header file //
int main(void)                //start main program// 
{
printf("All about circuit forum"); // print message//
}
Result All about circuit forum
http://bfy.tw/BqDU
 

eetech00

Joined Jun 8, 2013
3,856
Hi

Function main() can return any integer value supported by the operating system. Best practice for C is to return 0 (zero) or -1 when the function executes without error.

However, your second example is improperly coded. It defines main() as returning and integer value yet no where in the code block does it actually return an integer value. Some compilers might complain. The proper way to define main in this case would be:

void main(void)

Indicating the function does not return a value and does not require an argument.

eT
 

nsaspook

Joined Aug 27, 2009
13,079
READ!
https://www.gnu.org/software/libc/manual/html_node/Exit-Status.html

The default return value is set in the C runtime startup code. You may omit the return statement from main. If you do, and main finished, there is an implicit return 0.

That's what you see on a system with a OS usually. For simple embedded systems there is usually no default program return code from main in the runtime startup code or the return code is discarded in a loop.

C18 runtime startup code:
C:
/* $Id: c018.c,v 1.6 2006/11/15 22:53:12 moshtaa Exp $ */

/* Copyright (c)1999 Microchip Technology */

/* MPLAB-C18 startup code */

/* external reference to __init() function */
extern void __init (void);
/* external reference to the user's main routine */
extern void main (void);
/* prototype for the startup function */
void _entry (void);
void _startup (void);
extern near char __FPFLAGS;
#define RND 6

#pragma code _entry_scn=0x000000
void
_entry (void)
{
_asm goto _startup _endasm

}

#pragma code _startup_scn
void
_startup (void)
{
_asm
// Initialize the stack pointer
lfsr 1, _stack
lfsr 2, _stack

clrf TBLPTRU, 0 // 1st silicon doesn't do this on POR

bcf __FPFLAGS,RND,0 // Initalize rounding flag for floating point libs
_endasm loop:

// If user defined __init is not found, the one in clib.lib will be used
__init ();

// Call the user's main routine
main ();

goto loop;
} /* end _startup() */
 

MaxHeadRoom

Joined Jul 18, 2013
28,617
I don't use C but wouldn't that be the same as the RETLW x in assembly?
If so, look up the RETLW instruction in the manual and it explains it there.
Max.
 

MrSoftware

Joined Oct 29, 2013
2,188
I didn't dig up anything official for reference, but typically a process returns 0 to mean OK, or generally no error. A positive or negative exit value can be used to indicate a specific result or specific error. Reference the docs for that process to know for sure. Think less about GUI processes, and more along the lines of command line processes used by shell scripts, etc.. :)
 

vpoko

Joined Jan 5, 2012
267
Hello
why do we use return 0; at end of program and when do we need of return 0; in program.?
while searching I found that main function of type int therefore it should be return integer value. but I don't understand why it should be 0, it may be any value 1, 2, 3...etc
I have compiled both programs with return 0, and without return 0; but result is same . it display same message .
Program with return 0;
Code:
#include <stdio.h>            // header file //
int main(void)                //start main program//
{
printf("All about circuit forum"); // print message//
return 0;
}
Result All about circuit forum
program without return 0;
Code:
#include <stdio.h>            // header file //
int main(void)                //start main program//
{
printf("All about circuit forum"); // print message//
}
Result All about circuit forum
If your program is being executed from the OS shell, then it doesn't matter which integer you return, because the OS doesn't care. If your program, A, is being executed by another program, B, (which could also be a batch script) then the return value would only matter to program B. By convention, 0 was used to signal that the program completed normally, while other values would indicate various errors. But again, unless you're calling your program from another program, and that other program does something with the return value, then it doesn't matter. The OS ignores it.
 
Top