|
|
|
|
|
by a1369209993
2144 days ago
|
|
TBF, that doesn't actually work - you have to write: int fd; /* fd must be declared in order to be assigned */
if ((fd = open(...)) != -1) {
/* do something with fd */
} else {
perror("open");
}
which would be better written as: int fd = open(...);
if (fd != -1) /* etc */
It would be nice for scoping reasons to be able to write something like: if ((int fd = open(...)) != -1) /* etc */
but if ((int fd == open(...)) != -1) /* etc */
isn't valid code. |
|
Anyways, I am a bit torn about the second option. I like the idea of putting the call inside the if clause as it makes for a very explicit error handling but the uninitialized declaration is ugly. What I do in practice tends to depend on the situation but it is rarely satisfying.
Your last suggestion would be ideal, but as you said, it is invalid code unfortunately.
Maybe this
Just kidding, don't do that.