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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.