Dave Nguyen Dave Nguyen - 1 year ago 98
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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download