I have a program. Somewhere in the program, I have this code:
int
read_n(char *cp, int n)
{
int nread;
if ((nread = read(STDIN_FILENO, cp, n)) != n)
if (nread == -1)
die(DIE_ERROR_FMT, "failed reading input");
else
return nread;
return n;
}
and compiling my program like this:
cc -std=c99 -Wall -Wextra -Wshadow -Wpedantic prog.c -o prog
I get:
le.c:343:5: warning: add explicit braces to avoid dangling else [-Wdangling-else]
Why exactly is this a warning, and necessary? I know that the else
goes to the nearest else
-less if
and prefer to avoid braces when possible (for readability). This is clang
, gcc
gives a similar error.