Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

comma separated arguments in if statement condition

Tags:

c

if-statement

Whats the significance of first argument in an if statement if it's just going to be ignored? For instance, in:

#include<stdio.h>
main()
{
  if(1,0)
    printf("abc");
  else
    printf("qwe");
}
like image 277
jayesh hathila Avatar asked Jan 14 '23 02:01

jayesh hathila


1 Answers

That's not an argument list, it's the comma operator.

If you have a statement like foo(), bar(), then foo() will be called and its result discarded, then bar() will be called and the entire statement's result will be bar()'s result. Something like if(foo(),bar()) might be used if calling foo() has some side effect that needs to happen for some reason.

For something like 1,0, that is exactly the same as just saying 0 and there is no significance to the 1.

like image 119
Eric Finn Avatar answered Jan 22 '23 17:01

Eric Finn