Dave Nguyen Dave Nguyen - 11 months ago 55
C Question

C: why are extra semicolons OK?

#include <stdio.h>

int main() {
int a = -1, b = -10, c = 5;
if (a > b)
printf("Hello World");
printf("Get out World");;;;;;

Can anybody tell me why line number 8 still works, with multiple semicolons?


An empty statement is legal in C. Since ; is the statement terminator, multiple ; are syntactically valid. Sometimes this is even useful: such as the for (;;) {/*code here*/} idiom.

(Although some compilers will warn you in appropriate instances).

Do note that, conceptually at least, the excess ; in that line are not part of the if block.