> Yes, that's what blackrat said :P You don't need to use gotos, and they make code slower and harder to read.
It does not make it necessarily slower. It is also possible to produce very slow code without using even one goto in your code. But it can make your code hard to read, debug and maintain if you use a lot of goto's in your code. As already said, it is not necessary to use goto in C.
> The following is quite readable for most C programmers and does not rely on a goto...
Personally I'd prefer to have only one place in a function where a return is done, at the end of the function. In smaller functions it may probably be not a big problem to have more than one return, but I found that especially in large functions it is hard to read a function with lots of returns. Often in such situation the number of returns can be reduced by breaking the function op in smaller functions. It may be a bit more inefficient in run-time, but if the function is not called very often, then the inefficiency is very small and can be ignored in the total process.
Code:
int create_client_socket(struct sockaddr_in *addr)
{
int fd = -1;
int retval = -1;
fd = socket (AF_INET, SOCK_STREAM, 0);
if (fd != -1)
{
retval = connect (fd, addr, sizeof (*addr);
}
if (-1 == retval)
{
fprintf (stderr, "%s: %s", __func__, strerror (errno));
}
return retval;
}