3

I am learning C and as an exercise, I'm trying to write a simple program without any semicolons. I've had some trouble with replacing return 0 statement but I've found that this is (the only?) way to do it in C: if(exit(0),0){}.

How exactly does this statement work?

I know that exit() from stdlib is a void function but I don't understand what the ,0 part in the if works (the rest is clear to me).

syntagma
  • 23,346
  • 16
  • 78
  • 134

5 Answers5

6

The , operator in C evaluates both its arguments, and returns the value of the second one. So the expression

exit(0), 0

calls exit(0) and returns 0. So the code you posted is effectively equivalent to:

exit(0);
if (0) {}

However, exit() should terminate the process, so this should never actually return. The idiom is just being used to prevent spurious compiler warnings.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 3
    The comma operator is one of the few where the order of evaluation of the operands is specified. There's a sequence point after the evaluation of the first argument, even. – Daniel Fischer Mar 08 '13 at 20:42
  • 3
    `,` evaluates its operands in a *specified* order: first left, then right. – ouah Mar 08 '13 at 20:42
  • `exit(0),0` does not *return* 0. It evaluates to 0. Perhaps semantic niggling, but there is no return here. – William Pursell Mar 09 '13 at 05:36
  • It's not just to "prevent warnings" but to avoid a constraint violation. Since `exit` has return type `void`, it cannot be used as the controlling expression for an `if` statement. The `,0` has nothing to do with the *value* of the expression, which won't be used anyway. It's to give the expression the right *type*. – R.. GitHub STOP HELPING ICE Apr 26 '13 at 16:35
  • @R.. The code I said it's equivalent to doesn't have `exit(0)` as the condition expression of the `if`, it's before the `if`. – Barmar Apr 26 '13 at 18:53
5

While learning C, there is no value to figuring out how to write a program without semi-colons. Sounds like you've deviated from learning C into playing with useless tricks.

To answer your question though, when you have multiple statements separated by commas, the "result" is the final statement. if needs some statement with a value to evaluate, and since exit() is void it has no value. the 0 following that comma provides a value for if.

mah
  • 39,056
  • 9
  • 76
  • 93
2

When you writes

int i = (5, 7);

i assigned 7 not 5

In parenthesis , separated expressions executes from LHS to RHS.

Similarly if(exit(0), 0) == if(0) , but exit(0) executes first. (not optimize to blank)

My following example and its output will help you upto some extent to understand its behavior:

#include<stdio.h>
int fun(char* c){
 printf("%s\n", c);
 return 0;
}
int main(){
 int i = (fun("1"),fun("2"));
 if(fun("3"),7){
   printf("ONE %d", i);
 }
 else{
  printf("TWO %d", i);
 }
}

its Output:

1
2
3
ONE 0

Notice specially last of output ONE 0 printed because in if(fun("3"),7) == if(7). Otherwise fun() returns 0.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
1

It's just the comma operator.

It means that the right value of the operator is returned, and the left part is evaluated.

In this case it is used so the exit() can be put in the if statement, and the 0 is passed as parameter to be checked in the if statement (it can't work on a void value).

Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185
0

Comma operator may be treated as a give same effect as; in normal programming. Actually in this case every expression inside if will be evaluated. But whenever exit() is called the program will terminate. For details about comma operatorFollow this Link

Community
  • 1
  • 1
deeiip
  • 3,319
  • 2
  • 22
  • 33