What you have is syntactically correct... but it does not make easy reading.
You will likely be pulled up on it if your code is being reviewed - because you're making use of an "un-braced, multi-line statement as part of a conditional".
Prefer to be explicit and write it like so:
if (a == 0) {
puts("0");
} else {
switch (a) {
case 1: puts("1"); break;
case 2: puts("2"); break;
default: puts("default"); break;
}
}
It is legal in the same way that the following is legal:
if (a == 0)
puts("0");
else
puts("not0");
Such constructs can lead to mistakes when re-visiting the code... I seem to remember that one of the recent "popular" vulnerabilities was implemented (by mistake... hopefully) in part due to this "un-braced" use of if
.