Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rationale behind return 0 as default value in C/C++

Tags:

c++

c

Is there a reason why zero is used as a "default" function return value? I noticed that several functions from the stdlib and almost everywhere else, when not returning a proper number (e.g pow(), strcpy()) or an error (negative numbers), simply return zero.

I just became curious after seeing several tests performed with negated logic. Very confusing.

Why not return 1, or 0xff, or any positive number for that matter?

like image 361
Marcelo MD Avatar asked Dec 01 '08 03:12

Marcelo MD


People also ask

What is the purpose of return 0 in C?

return 0: A return 0 means that the program will execute successfully and did what it was intended to do.

Does C return 0 by default?

Master C and Embedded C Programming- Learn as you go The normal exit of program is represented by zero return value. If the code has errors, fault etc., it will be terminated by non-zero value. In C++ language, the main() function can be left without return value. By default, it will return zero.

Why do we return 0 in int main?

Use return 0 within the main function We are returning 0 because the main function produces a numeric value (int main()). The term return is being utilized to return a result with a function. This signifies that the program have been completed accurately, and we can use the return statement to end the main function.

What is the default return value in C?

If a return type isn't specified, the C compiler assumes a default return type of int .


4 Answers

The rationale is that you want to distinguish the set of all the possible (negative) return values corresponding to different errors from the only situation in which all went OK. The simplest, most concise and most C-ish way to pursue such distinction is a logical test, and since in C all integers are "true" except for zero, you want to return zero to mean "the only situation", i.e. you want zero as the "good" value.

The same line of reasoning applies to the return values of Unix programs, but indeed in the tests within Unix shell scripts the logic is inverted: a return value of 0 means "true" (for example, look at the return value of /bin/true).

like image 83
Federico A. Ramponi Avatar answered Oct 20 '22 00:10

Federico A. Ramponi


Originally, C did not have "void". If a function didn't return anything, you just left the return type in the declaration blank. But that meant, that it returned an int.

So, everything returned something, even if it didn't mean anything. And, if you didn't specifically provide a return value, whatever value happened to be in the register the compiler used to return values became the function's return value.

// Perfectly good K&R C code.
NoReturn()
{
   // do stuff;
   return;
}

int unknownValue = NoReturn();

People took to clearing that to zero to avoid problems.

like image 45
James Curran Avatar answered Oct 19 '22 23:10

James Curran


In shell scripting, 0 represents true, where another number typically represents an error code. Returning 0 from a main application means everything went successfully. The same logic may be being applied to the library code.

It could also just be that they return nothing, which is interpreted as 0. (Essentially the same concept.)

like image 26
majelbstoat Avatar answered Oct 20 '22 01:10

majelbstoat


Another (minor) reason has to do with machine-level speed and code size.

In most processors, any operation that results in a zero automatically sets the zero flag, and there is a very cheap operation to jump against the zero flag.

In other words, if the last machine operation (e.g., PUSH) got us to zero, all we need is a jump-on-zero or a jump-not-zero.

On the other hand, if we test things against some other value, then we have to move that value into the register, run a compare operation that essentially subtracts the two numbers, and equality results in our zero.

like image 35
Uri Avatar answered Oct 20 '22 00:10

Uri