Hi,
Can someone elaborate why the following (the if conditional) differs

int main(){
  const char *filename = "test.txt";
  int ifd;
  if((ifd=open(filename,O_RDONLY))<3)
    err(EX_NOINPUT, "%s", filename);
  fprintf(stderr,"ifd is:%d\n",ifd);
  off_t bytes_in = lseek(ifd, (off_t)(-4), SEEK_END);
  return 0;
}

vs

int main(){
  const char *filename = "test.txt";
  int ifd;
  if(ifd=open(filename,O_RDONLY)<3)
    err(EX_NOINPUT, "%s", filename);
  fprintf(stderr,"ifd is:%d\n",ifd);
  off_t bytes_in = lseek(ifd, (off_t)(-4), SEEK_END);
  return 0;
}

Basicly it looks like adding additional paranthesis, makes the conditional test on the evaluation of the rval. And the assignment is a sideeffect?

thanks

assignment is a sideeffect?

Yes.

if((ifd=open(filename,O_RDONLY))<3)

means:
1. assign the value returned by open to ifd
2. compare it to 3
whereas

if(ifd=open(filename,O_RDONLY)<3)

means:
1. compare the value returned by open to 3
2. assign the boolean result of the comparison to ifd

Edited 7 Years Ago by nezachem: n/a

Yes.

if((ifd=open(filename,O_RDONLY))<3)

means:
1. assign the value returned by open to ifd
2. compare it to 3
whereas

if(ifd=open(filename,O_RDONLY)<3)

means:
1. compare the value returned by open to 3
2. assign the boolean result of the comparison to ifd

Thanks
that clarified it.

This question has already been answered. Start a new discussion instead.