-4

so after

#include <stdio.h>

int main(int argc , char *argv[])
{
    int n;
    n = _strlen_recursion("Corbin Coleman");
    printf("%d\n", n);
}
int _strlen_recursion(char *s)
{
   static int count = 0;    
   count++;  
   return (count), count = 0;
}

where the comma operator is in action
0 can be seen on screen. Why?

So the real question is : does return have () overload as say sizeof does?

3 Answers3

2

In the return statement return (count), count = 0;,

attr(optional) return expression(optional) ;  (1) 

The expression is (count), count = 0, as comma operator, the 1st operand (count) is evaluated and the result is discarded, then the 2nd operand count = 0 is evaluated, its value 0 is returned as the return value of the omma operator, and returned as the return value of _strlen_recursion() later.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
2

return is not similar to sizeof. The operand of sizeof is either a type in parentheses or an expression.

A return statement has only an expression (or nothing). Parentheses are not part of the grammar of a return statement. Parentheses may be present only because they are part of the expression.

In return (count), count = 0;, the expression is (count), count = 0. That is a comma expression. It evaluates (count), discards the resulting value, then evaluates count = 0. The value of that is the value assigned, 0, so that is the value of the comma expression, so 0 is returned.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • >The operand of sizeof is either a type in parentheses or an expression. It's not right ofc because `short a; short c; long long t; a = sizeof (t) , c; printf("%d\n", a);` we have `8` – Алексей Неудачин Feb 25 '21 at 04:28
  • so it's real `()` overload as i said. it's not for type – Алексей Неудачин Feb 25 '21 at 04:37
  • @АлексейНеудачин [`sizeof`](https://en.cppreference.com/w/cpp/language/sizeof) is an operator that has two versions (one for types, the other for expressions). When used with types, the operand of `sizeof` must be enclosed in parentheses. When used with expressions, the operand of `sizeof` need not be enclosed in parentheses, but of course, since a parenthesized expression is still an expression, it works with parentheses as well. On the other hand, [`return`](https://en.cppreference.com/w/cpp/language/return) is not an operator at all; it is a statement. Statements do not have "overloads". – heap underrun Feb 25 '21 at 05:09
  • @heapunderrun And you too? If it'll be true what you're saying then code in comments above will come to `2` , not to `8` . Compiler or whatever pulls out `sizeof()` overload there – Алексей Неудачин Feb 25 '21 at 08:25
  • this page now contains enough info to make it more than clear – Алексей Неудачин Feb 25 '21 at 08:38
  • @АлексейНеудачин When you write `a = sizeof (t) , c;`, the operand of the assignment is only `sizeof (t)`, not the whole `sizeof (t) , c;`, in other words, it goes like `(a = sizeof (t)) , c;`, not like `a = (sizeof (t) , c);`. The same result you'll get if you write `a = sizeof t , c;`. The reason is that `=` operator has higher [precedence](https://en.cppreference.com/w/cpp/language/operator_precedence) than `,` operator. Comma has the lowest precedence, if you look at the table I've linked. – heap underrun Feb 25 '21 at 09:15
  • @heapunderrun looks like you'd wanted to say different thing, but made a mistake.this thing you'd wanted to say is that `=` has higher precedence than sizeof's space (whatever it called). It's not about comma here. I'll check it on return but i've thinkin it has same precedence as square bracket.Which is to say , highest possible priority – Алексей Неудачин Feb 25 '21 at 09:40
  • or may be comma plays some role also nevertheless – Алексей Неудачин Feb 25 '21 at 09:54
  • @АлексейНеудачин No, the `=` operator has *lower* precedence than `sizeof` operator, but higher than `,` operator. The `=` operator has one of the lowest priorities (but still not lower than `,` operator, see that table). – heap underrun Feb 25 '21 at 09:57
  • I've talked about space right after `sizeof` that it has highest precedence, not about `=` operator. So check performed by my code above really shows nothing. And i do not know how to check this. I've found some stuff on that in gcc github repo but it doesn't make it more clear. https://github.com/gcc-mirror/gcc/blob/16e2427f50c208dfe07d07f18009969502c25dc8/gcc/c/ChangeLog#L2618 So may be you're right – Алексей Неудачин Feb 25 '21 at 12:07
1

The comma acts as a binary operator in C, the same way +, -, &, and all the other operators in C do. It takes two operands and acts as a sequence point. The left operand, (count) in your example, is evaluated, the result is then discarded. From here, the right operand, count = 0, is evaluated and its return value is returned. In your example, the left operand has no side-effects, so it is essentially useless. The line return (count), count = 0; is equivalent to the two lines

(void) (count);
return count = 0;

Note that count = 0 is an assignment, and in C, assignments return the value assigned, so in this case 0 is returned form this expression, and ultimately the function.

brenden
  • 574
  • 3
  • 16
  • this means that comma op has higher precedence than a round bracket. thats may be but i didn't see stuff like that before – Алексей Неудачин Feb 25 '21 at 02:25
  • @АлексейНеудачин The comma has lower precedence than parentheses, I didn't mean to imply the opposite, let me know which part of my answer did and I'll clarify. – brenden Feb 25 '21 at 02:28
  • because you're talking that compiler (or preprocessor) pulls `return smth` overload instead of `return (smth)` – Алексей Неудачин Feb 25 '21 at 02:33
  • 1
    Let me break down the return expression exactly. Return expressions evaluate the operand, and then return it from the function, so it has to evaluate the expression `(count), count = 0`, this expression is evaluated via the rules mentioned in my answer, and the result of this expression is 0, thus the return value is 0. Also note this step is in the compiler, not the preprocessor. – brenden Feb 25 '21 at 02:37