Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of second value in return statement in C

Tags:

c

I have following c code:

#define ASSERT_ACTIVE(active) do { if (!active) return errno = 6, -1; } while (0);
#define errno (*_errno())

int someCrazyFunc (bool active) {
    ASSERT_INACTIVE (active);
    ...
}

As far as I know a #define will simply place replacement text in place of the specified identifier.

I like to know:

  • What does return errno = 6, -1; means? is that returns two values in one return statement?
  • What is the meaning of replacement code (*_errno()) = 6
like image 203
AaA Avatar asked Oct 24 '12 02:10

AaA


People also ask

What is the meaning of return 2 in C?

The statement. return 2; means the function, in which it is, returns value 2 .

What is the return value in C?

A return statement ends the execution of a function, and returns control to the calling function. Execution resumes in the calling function at the point immediately following the call. A return statement can return a value to the calling function.

How many return types are there in C?

Similarly, a function can return something, otherwise does not return anything. So we can categorize them into four types.

What is the use of return 0 in C?

return 0 in the main function means that the program executed successfully. return 1 in the main function means that the program does not execute successfully and there is some error. return 0 means that the user-defined function is returning false.


2 Answers

There isn't a second value - a return statement returns exactly one value. In the statement:

return errno = 6, -1;

The return value is the result of the expression errno = 6, -1. This is an expression using the comma operator - it is parsed as (errno = 6), -1, which evaluates to -1 and assigns 6 to errno as a side-effect. So this means that it's equivalent to the two statements:

errno = 6;
return -1;

Assuming that _errno() is a function returning a pointer - for example it has a return type of int * - then the expression (*_errno()) = 6 assigns the value 6 to the object pointed to by the return value of the function. It would be equivalent to code similar to:

int *p = _errno();
*p = 6;

errno is often defined like this in order to give each thread in a multi-threaded implementation its own errno. The function _errno() in this case would return a pointer to the current thread's errno variable.

like image 71
caf Avatar answered Nov 14 '22 22:11

caf


That is not actually a second value to the return statement, it's the comma operator. What it does is to evaluate the expressions on both sides of the comma, but "return" only the value of the second expression.

So what return errno = 6, -1; does is evaluate the expressions errno = 6 and -1 separately and then give back the result of the second expression -1, which will then be used by return.

like image 43
Some programmer dude Avatar answered Nov 14 '22 23:11

Some programmer dude