0

I'm reading Programming in C by Brian Kernighnan an Dennis Ritchie in that(pg.25 bottom)

Here the author quotes:

The value that power computes is returned to main by the return statement. Any expression may follow return: return expression ;

But in the code he provided above:

#include <stdio.h>
int power(int m , int n);

main()
{
    int i;
    for (i = 0; i < 10; ++i)
    printf ( " %d \t %d \t %d \n ", i, power( 2 , i), power( -3 , i));
    return 0;//no. 1
}

int power(int m , int n)
{
    int i, p ;
    p = 1;
    for (i = 1; i <= n; ++i)
    p = p * m;
    return p; //no. 2
}

Here I understood why he used 2. return p; in function power (i.e to get the value of p) but why does he use 1. return 0; ?? I tried removing the line return 0; And it still worked as I had thought, is there something I'm missing??

[UPDATE] I'm sorry for not including this before but I already knew this much:
quoted in the book:

You may have noticed that there is a return statement at the end of main. Since main is a function like any other, it may return a value to its caller, which is in effect the environment in which the program was executed. Typi- cally, a return value of zero implies normal termination; non-zero values signal unusual or erroneous termination conditions. In the interests of simplicity, we have omitted return statements from our main functions up to this point, but we will include them hereafter, as a reminder that programs should return status to their environment.

Thanks to @Pascal I was able to understand the difference in both the return p; and return 0; However my intention was never to know what return 0; was but why return was used also to know the difference between both the return statment......

ArchKudo
  • 123
  • 1
  • 4
  • 11
  • 3
    To indicate that the program ran without any problems. Usually, when a program encountered an error or problem, an integer that is not 0 is returned to indicate to the user that there was an error. – Tim May 30 '14 at 15:17
  • 1
    I do not think that a C function should be closed as a duplicate of a C++ function, especially not in this case where there are nuances between the two languages. – Pascal Cuoq May 30 '14 at 15:38
  • @PascalCuoq I understand C and C++ are not the same, but I think the answer in the 'duplicate' question describes perfectly what the OP wants to know - what the return statement in the main() is for, and therefore I find it appropriate to flag it as duplicate – Tim May 30 '14 at 15:52
  • 1
    @TimCastelijns I don't know what the fuss is about maybe my bad english but I wanted to know what was the difference in return p; and return 0; also I am a beginner I can't understand what my own code means and you expect me to understand other's code?? Have some sympathy... – ArchKudo May 30 '14 at 15:57
  • There is no fuss. I flagged your question for duplicate because (except for the language difference) it is almost an exact duplicate, and the other question has a good answer which also applies to your question. I'm not the bad guy, I'm just trying to help – Tim May 30 '14 at 16:11

4 Answers4

10

In a function other than the entry point of the program, return e; indicates the result that will be sent to the caller.


A return i; statement in the main() function is a way instead for the program to communicate an exit code to the operating system. In POSIX, return 0; indicates success.

The return 0; at the end of function main() is so perfunctory that in C99, it was made optional. The } ending function main() implicitly behaves like return 0; according to the standard:

5.1.2.2.3 Program termination

1 If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument; reaching the } that terminates the main function returns a value of 0[…]

However, in C90, there is no such exception, and it is technically undefined behavior to have an int-returning function such as main() reach the final } without a return statement. The worst that seems to happen in practice is that the program returns with an indeterminate value in the register or stack slot reserved for function results, so that the Operating System may think that the program failed when it actually succeeded.

Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
  • I'm sorry but I really don't get when you say C99 and C90 what are they?? – ArchKudo May 30 '14 at 15:23
  • @ArchKudo they are different versions of the C language – Tim May 30 '14 at 15:23
  • "*Versions*" here somethings also are referred to as "*Dialects*". – alk May 30 '14 at 15:25
  • 1
    @ArchKudo C90 is the oldest standard defining the C language, also nicknamed “ANSI C”. C99 is an updated standard published 15 years ago. – Pascal Cuoq May 30 '14 at 15:26
  • 2
    Actually, C90 is also known ISO C (ISO/IEC 9899:1990). ANSI C was most commonly known as C89, as that is when ANSI passed the standard. They are identical except for formatting, and the ISO standard has since been withdrawn by INCITS and ISO/IEC. – JohnH May 30 '14 at 16:11
  • @JohnH Yes, “ANSI C” is only a cute nickname, since ISO re-published in 1990 the standard published by ANSI in 1989, and since it has been going the other way round after that, with ISO publishing a new C standard in 1999 and ANSI adopting it in 2000. – Pascal Cuoq May 30 '14 at 16:50
4

main()'s return code is conventionally a way for programs to give the operating system an indication on whether they completed successfully or no. Conventionally a return value of zero means success, while other values are used to indicate errors.

OS's like UNIX/Linux and Windows provide ways to check such return code; in sh or bash the $? holds the return code of the last command executed, in Windows' cmd you can obtain it as %ERRORLEVEL%.

Nicola Musatti
  • 17,834
  • 2
  • 46
  • 55
  • 3
    I would add to this that returning zero means that the program executed successfully. – SeeJayBee May 30 '14 at 15:18
  • Doesn't the compiler itself gives error when something is wrong??for eg. if i miss a semi-colon it would say I have a semi-colon missing then what is the use of return 0;?? – ArchKudo May 30 '14 at 15:20
  • @ArchKudo you can for example let your program exit with a return value of 1 to indicate that a file the program was trying to open could not be found (this is just a silly example, normally you would do error handling in the code, but this is just to give you an idea) – Tim May 30 '14 at 15:23
  • @ChrisJ.Breisch: I am not sure but I think that returning zero as to indicate the program ended successfully is a convention and there is no standard about it – Moha the almighty camel May 30 '14 at 15:24
  • Oo @Tim but the author says the return x; is a computed value what gets computed when I type return 0; and where?? – ArchKudo May 30 '14 at 15:29
  • This is a completely different thing from the compiler error code; it is a code which the successfully compiled and executed program returns. It "returns" to the operating system's environment in which the program was executed. – Paul Richter May 30 '14 at 15:30
  • @ArchKudo the author is talking about the pow() function that returns a computed value. The main is a different case – Tim May 30 '14 at 15:32
  • @Mhd.Tahawi The C standard suggests `EXIT_SUCCESS` to indicate success, but without having access to it, I am pretty sure that the POSIX standard would define `EXIT_SUCCESS` to be zero. – Pascal Cuoq May 30 '14 at 15:33
  • @TimCastelijns so what is return statment anyway ?? I mean you said it was to pipe the value of p to main() in power() function but to display error code in main() function......... – ArchKudo May 30 '14 at 15:37
  • @ArchKudo I did not say that. Anyway you have a book on C, just read it and you will find out – Tim May 30 '14 at 15:46
2

The main function of every C program should return 0 if the program worked correctly. It's a convention which you should always follow.

Teo Zec
  • 383
  • 2
  • 11
0

According to the ISO/IEC C++ Standard (14882), section 3.6.1 states that the function main should have one of the two signatures:

int main(void) { .... }
or
int main(int argc, char** argv) { .... }

Most compilers accept variants of this, such as just main() { .... } for backwards comparability, such as your example code. The return at the end of main is the value that is returned to the calling environment (typically the command shell) and it can be used to represent successful completion of the program (by return 0), or return an error code to the calling environment in case of program error.

If you leave the return off at the end of main, a return 0; is assumed.

thurizas
  • 2,473
  • 1
  • 14
  • 15